vDPA shadow virtqueue
Subsystems (3)
Eugenio Pérez
maintainerPATCH 2/31 vhost: Add VhostShadowVirtqueue
In reply to Eugenio PérezVhost shadow virtqueue (SVQ) is an intermediate jump for virtqueue notifications and buffers, allowing qemu to track them. While qemu is forwarding the buffers and virtqueue changes, it is able to commit the memory it's being dirtied, the same way regular qemu's VirtIO devices do.
This commit only exposes basic SVQ allocation and free. Next patches of the series add functionality like notifications and buffers forwarding.
--- hw/virtio/vhost-shadow-virtqueue.h | 21 ++++++++++ hw/virtio/vhost-shadow-virtqueue.c | 64 ++++++++++++++++++++++++++++++ hw/virtio/meson.build | 2 +- 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 hw/virtio/vhost-shadow-virtqueue.h create mode 100644 hw/virtio/vhost-shadow-virtqueue.c
hw/virtio/vhost-shadow-virtqueue.h new file +21 -0
@@ -0,0 +1,21 @@ +/* +* vhost shadow virtqueue +* +* SPDX-FileCopyrightText: Red Hat, Inc. 2021 +* SPDX-FileContributor: Author: Eugenio Pérez <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef VHOST_SHADOW_VIRTQUEUE_H +#define VHOST_SHADOW_VIRTQUEUE_H + +#include "hw/virtio/vhost.h" + +typedef struct VhostShadowVirtqueue VhostShadowVirtqueue ; + +VhostShadowVirtqueue *vhost_svq_new (void ); + +void vhost_svq_free (VhostShadowVirtqueue *vq); + +#endif
hw/virtio/vhost-shadow-virtqueue.c new file +64 -0
64 lines not on this page · raw message
hw/virtio/meson.build +1 -1
@@ -11,7 +11,7 @@ softmmu_ss.add(when: 'CONFIG_ALL', if_true: files('vhost-stub.c')) virtio_ss = ss.source_set() virtio_ss.add(files('virtio.c')) -virtio_ss.add(when: 'CONFIG_VHOST', if_true: files('vhost.c', 'vhost-backend.c')) +virtio_ss.add(when: 'CONFIG_VHOST', if_true: files('vhost.c', 'vhost-backend.c', 'vhost-shadow-virtqueue.c')) virtio_ss.add(when: 'CONFIG_VHOST_USER', if_true: files('vhost-user.c')) virtio_ss.add(when: 'CONFIG_VHOST_VDPA', if_true: files('vhost-vdpa.c')) virtio_ss.add(when: 'CONFIG_VIRTIO_BALLOON', if_true: files('virtio-balloon.c'))
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 5/31 vhost: Add Shadow VirtQueue kick forwarding capabilities
In reply to Eugenio PérezAt this mode no buffer forwarding will be performed in SVQ mode: Qemu will just forward the guest's kicks to the device.
Also, host notifiers must be disabled at SVQ start, and they will not start if SVQ has been enabled when the device is stopped. This will be addressed in next patches.
--- hw/virtio/vhost-shadow-virtqueue.h | 2 ++ hw/virtio/vhost-shadow-virtqueue.c | 27 ++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-)
hw/virtio/vhost-shadow-virtqueue.h +2 -0
@@ -19,6 +19,8 @@ void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd);const EventNotifier *vhost_svq_get_dev_kick_notifier (const VhostShadowVirtqueue *svq); +void vhost_svq_stop (VhostShadowVirtqueue *svq); +VhostShadowVirtqueue *vhost_svq_new (void );void vhost_svq_free (VhostShadowVirtqueue *vq);
hw/virtio/vhost-shadow-virtqueue.c +26 -1
@@ -42,11 +42,26 @@ const EventNotifier *vhost_svq_get_dev_kick_notifier(return &svq->hdev_kick; } +/* Forward guest notifications */ +static void vhost_handle_guest_kick (EventNotifier *n) +{ +VhostShadowVirtqueue *svq =container_of (n, VhostShadowVirtqueue, + svq_kick); + +if (unlikely (!event_notifier_test_and_clear (n))) { +return ; + } + +event_notifier_set (&svq->hdev_kick); +} +/** * Set a new file descriptor for the guest to kick SVQ and notify for avail * * @svq The svq -* @svq_kick_fd The new svq kick fd +* @svq_kick_fd The svq kick fd +* +* Note that SVQ will never close the old file descriptor. */ void vhost_svq_set_svq_kick_fd (VhostShadowVirtqueue *svq,int svq_kick_fd) {
@@ -65,12 +80,22 @@ void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd)* need to explicitely check for them. */ event_notifier_init_fd (&svq->svq_kick, svq_kick_fd); +event_notifier_set_handler (&svq->svq_kick, vhost_handle_guest_kick);if (!check_old ||event_notifier_test_and_clear (&tmp)) {event_notifier_set (&svq->hdev_kick); } } +/** +* Stop shadow virtqueue operation. +* @svq Shadow Virtqueue +*/ +void vhost_svq_stop (VhostShadowVirtqueue *svq) +{ +event_notifier_set_handler (&svq->svq_kick,NULL ); +} +/** * Creates vhost shadow virtqueue, and instruct vhost device to use the shadow * methods and file descriptors.
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 3/31 vdpa: Add vhost_svq_get_dev_kick_notifier
In reply to Eugenio PérezIs needed so vhost-vdpa knows the device's kick event fd.
--- hw/virtio/vhost-shadow-virtqueue.h | 4 ++++ hw/virtio/vhost-shadow-virtqueue.c | 10 +++++++++- 2 files changed, 13 insertions(+), 1 deletion(-)
hw/virtio/vhost-shadow-virtqueue.h +4 -0
@@ -11,9 +11,13 @@#define VHOST_SHADOW_VIRTQUEUE_H #include "hw/virtio/vhost.h" +#include "qemu/event_notifier.h" typedef struct VhostShadowVirtqueue VhostShadowVirtqueue ; +const EventNotifier *vhost_svq_get_dev_kick_notifier ( +const VhostShadowVirtqueue *svq); +VhostShadowVirtqueue *vhost_svq_new (void );void vhost_svq_free (VhostShadowVirtqueue *vq);
hw/virtio/vhost-shadow-virtqueue.c +9 -1
@@ -11,7 +11,6 @@#include "hw/virtio/vhost-shadow-virtqueue.h" #include "qemu/error-report.h" -#include "qemu/event_notifier.h" /* Shadow virtqueue to relay notifications */ typedef struct VhostShadowVirtqueue {
@@ -21,6 +20,15 @@ typedef struct VhostShadowVirtqueue {EventNotifier hdev_call; } VhostShadowVirtqueue; +/** +* The notifier that SVQ will use to notify the device. +*/ +const EventNotifier *vhost_svq_get_dev_kick_notifier ( +const VhostShadowVirtqueue *svq) +{ +return &svq->hdev_kick; +} +/** * Creates vhost shadow virtqueue, and instruct vhost device to use the shadow * methods and file descriptors.
Signature · 1 line
2.27.0
Eugenio Pérez
maintainercover vDPA shadow virtqueue
This series enables shadow virtqueue (SVQ) for vhost-vdpa devices. This is intended as a new method of tracking the memory the devices touch during a migration process: Instead of relay on vhost device's dirty logging capability, SVQ intercepts the VQ dataplane forwarding the descriptors between VM and device. This way qemu is the effective writer of guests memory, like in qemu's emulated virtio device operation.
When SVQ is enabled qemu offers a new virtual address space to the device to read and write into, and it maps new vrings and the guest memory in it. SVQ also intercepts kicks and calls between the device and the guest. Used buffers relay would cause dirty memory being tracked, but at this RFC SVQ is not enabled on migration automatically.
Thanks of being a buffers relay system, SVQ can be used also to communicate devices and drivers with different capabilities, like devices that only support packed vring and not split and old guests with no driver packed support.
It is based on the ideas of DPDK SW assisted LM, in the series of DPDK's https://patchwork.dpdk.org/cover/48370/ . However, these does not map the shadow vq in guest's VA, but in qemu's.
This version of SVQ is limited in the amount of features it can use with guest and device, because this series is already very big otherwise. Features like indirect or event_idx will be addressed in future series.
SVQ needs to be enabled with cmdline parameter x-svq, like:
-netdev type=vhost-vdpa,vhostdev=/dev/vhost-vdpa-0,id=vhost-vdpa0,x-svq=true
In this version it cannot be enabled or disabled in runtime. Further series will remove this limitation and will enable it only for migration time.
Some patches are intentionally very small to ease review, but they can be squashed if preferred.
Patches 1-10 prepares the SVQ and QEMU to support both guest to device and device to guest notifications forwarding, with the extra qemu hop. That part can be tested in isolation if cmdline change is reproduced.
Patches from 11 to 18 implement the actual buffer forwarding, but with no IOMMU support. It requires a vdpa device capable of addressing all qemu vaddr.
Patches 19 to 23 adds the iommu support, so the device with address range limitations can access SVQ through this new virtual address space created.
The rest of the series add the last pieces needed for migration.
Comments are welcome.
TODO:
* Event, indirect, packed, and other features of virtio.
* To separate buffers forwarding in its own AIO context, so we can throw more threads to that task and we don't need to stop the main event loop.
* Support virtio-net control vq.
* Proper documentation.
Changes from v5 RFC:
* Remove dynamic enablement of SVQ, making less dependent of the device.
* Enable live migration if SVQ is enabled.
* Fix SVQ when driver reset.
* Comments addressed, specially in the iova area.
* Rebase on latest master, adding multiqueue support (but no networking control vq processing).
v5 link:
https://lists.gnu.org/archive/html/qemu-devel/2021-10/msg07250.html
Changes from v4 RFC:
* Support of allocating / freeing iova ranges in IOVA tree. Extending already present iova-tree for that.
* Proper validation of guest features. Now SVQ can negotiate a different set of features with the device when enabled.
* Support of host notifiers memory regions
* Handling of SVQ full queue in case guest's descriptors span to different memory regions (qemu's VA chunks).
* Flush pending used buffers at end of SVQ operation.
* QMP command now looks by NetClientState name. Other devices will need to implement it's way to enable vdpa.
* Rename QMP command to set, so it looks more like a way of working
* Better use of qemu error system
* Make a few assertions proper error-handling paths.
* Add more documentation
* Less coupling of virtio / vhost, that could cause friction on changes
* Addressed many other small comments and small fixes.
Changes from v3 RFC:
* Move everything to vhost-vdpa backend. A big change, this allowed some cleanup but more code has been added in other places.
* More use of glib utilities, especially to manage memory.
v3 link:
https://lists.nongnu.org/archive/html/qemu-devel/2021-05/msg06032.html
Changes from v2 RFC:
* Adding vhost-vdpa devices support
* Fixed some memory leaks pointed by different comments
v2 link:
https://lists.nongnu.org/archive/html/qemu-devel/2021-03/msg05600.html
Changes from v1 RFC:
* Use QMP instead of migration to start SVQ mode.
* Only accepting IOMMU devices, closer behavior with target devices
(vDPA)
* Fix invalid masking/unmasking of vhost call fd.
* Use of proper methods for synchronization.
* No need to modify VirtIO device code, all of the changes are contained in vhost code.
* Delete superfluous code.
* An intermediate RFC was sent with only the notifications forwarding changes. It can be seen in
https://patchew.org/QEMU/[email protected]/
v1 link:
https://lists.gnu.org/archive/html/qemu-devel/2020-11/msg05372.html
Eugenio Pérez (20):
virtio: Add VIRTIO_F_QUEUE_STATE
virtio-net: Honor VIRTIO_CONFIG_S_DEVICE_STOPPED
virtio: Add virtio_queue_is_host_notifier_enabled
vhost: Make vhost_virtqueue_{start,stop} public
vhost: Add x-vhost-enable-shadow-vq qmp
vhost: Add VhostShadowVirtqueue
vdpa: Register vdpa devices in a list
vhost: Route guest->host notification through shadow virtqueue Add vhost_svq_get_svq_call_notifier
Add vhost_svq_set_guest_call_notifier
vdpa: Save call_fd in vhost-vdpa
vhost-vdpa: Take into account SVQ in vhost_vdpa_set_vring_call
vhost: Route host->guest notification through shadow virtqueue
virtio: Add vhost_shadow_vq_get_vring_addr
vdpa: Save host and guest features
vhost: Add vhost_svq_valid_device_features to shadow vq
vhost: Shadow virtqueue buffers forwarding
vhost: Add VhostIOVATree
vhost: Use a tree to store memory mappings
vdpa: Add custom IOTLB translations to SVQ
Eugenio Pérez (31):
vdpa: Reorder virtio/vhost-vdpa.c functions
vhost: Add VhostShadowVirtqueue
vdpa: Add vhost_svq_get_dev_kick_notifier
vdpa: Add vhost_svq_set_svq_kick_fd
vhost: Add Shadow VirtQueue kick forwarding capabilities
vhost: Route guest->host notification through shadow virtqueue
vhost: dd vhost_svq_get_svq_call_notifier
vhost: Add vhost_svq_set_guest_call_notifier
vhost-vdpa: Take into account SVQ in vhost_vdpa_set_vring_call
vhost: Route host->guest notification through shadow virtqueue
vhost: Add vhost_svq_valid_device_features to shadow vq
vhost: Add vhost_svq_valid_guest_features to shadow vq
vhost: Add vhost_svq_ack_guest_features to shadow vq
virtio: Add vhost_shadow_vq_get_vring_addr
vdpa: Add vhost_svq_get_num
vhost: pass queue index to vhost_vq_get_addr
vdpa: adapt vhost_ops callbacks to svq
vhost: Shadow virtqueue buffers forwarding
utils: Add internal DMAMap to iova-tree
util: Store DMA entries in a list
util: Add iova_tree_alloc
vhost: Add VhostIOVATree
vdpa: Add custom IOTLB translations to SVQ
vhost: Add vhost_svq_get_last_used_idx
vdpa: Adapt vhost_vdpa_get_vring_base to SVQ
vdpa: Clear VHOST_VRING_F_LOG at vhost_vdpa_set_vring_addr in SVQ
vdpa: Never set log_base addr if SVQ is enabled
vdpa: Expose VHOST_F_LOG_ALL on SVQ
vdpa: Make ncs autofree
vdpa: Move vhost_vdpa_get_iova_range to net/vhost-vdpa.c
vdpa: Add x-svq to NetdevVhostVDPAOptions
qapi/net.json | 5 +- hw/virtio/vhost-iova-tree.h | 27 + hw/virtio/vhost-shadow-virtqueue.h | 46 ++ include/hw/virtio/vhost-vdpa.h | 7 + include/qemu/iova-tree.h | 17 + hw/virtio/vhost-iova-tree.c | 157 ++++++ hw/virtio/vhost-shadow-virtqueue.c | 761 +++++++++++++++++++++++++++++ hw/virtio/vhost-vdpa.c | 740 ++++++++++++++++++++++++---- hw/virtio/vhost.c | 6 +- net/vhost-vdpa.c | 58 ++- util/iova-tree.c | 161 +++++- hw/virtio/meson.build | 2 +- 12 files changed, 1852 insertions(+), 135 deletions(-) create mode 100644 hw/virtio/vhost-iova-tree.h create mode 100644 hw/virtio/vhost-shadow-virtqueue.h create mode 100644 hw/virtio/vhost-iova-tree.c create mode 100644 hw/virtio/vhost-shadow-virtqueue.c
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 17/31 vdpa: adapt vhost_ops callbacks to svq
First half of the buffers forwarding part, preparing vhost-vdpa callbacks to SVQ to offer it. QEMU cannot enable it at this moment, so this is effectively dead code at the moment, but it helps to reduce patch size.
--- hw/virtio/vhost-shadow-virtqueue.h | 2 +- hw/virtio/vhost-shadow-virtqueue.c | 21 ++++- hw/virtio/vhost-vdpa.c | 133 ++++++++++++++++++++++++++--- 3 files changed, 143 insertions(+), 13 deletions(-)
hw/virtio/vhost-shadow-virtqueue.h +1 -1
@@ -35,7 +35,7 @@ size_t vhost_svq_device_area_size(const VhostShadowVirtqueue *svq);void vhost_svq_stop (VhostShadowVirtqueue *svq); -VhostShadowVirtqueue *vhost_svq_new (void ); +VhostShadowVirtqueue *vhost_svq_new (uint16_t qsize);void vhost_svq_free (VhostShadowVirtqueue *vq);
hw/virtio/vhost-shadow-virtqueue.c +20 -1
@@ -277,9 +277,17 @@ void vhost_svq_stop(VhostShadowVirtqueue *svq)/** * Creates vhost shadow virtqueue, and instruct vhost device to use the shadow * methods and file descriptors. +* +* @qsize Shadow VirtQueue size +* +* Returns the new virtqueue or NULL. +* +* In case of error, reason is reported through error_report. */ -VhostShadowVirtqueue *vhost_svq_new (void ) +VhostShadowVirtqueue *vhost_svq_new (uint16_t qsize) { +size_t desc_size =sizeof (vring_desc_t) * qsize; +size_t device_size, driver_size; g_autofree VhostShadowVirtqueue *svq = g_new0(VhostShadowVirtqueue, 1);int r;
@@ -300,6 +308,15 @@ VhostShadowVirtqueue *vhost_svq_new(void)/* Placeholder descriptor, it should be deleted at set_kick_fd */ event_notifier_init_fd (&svq->svq_kick,INVALID_SVQ_KICK_FD ); + svq->vring.num = qsize; + driver_size =vhost_svq_driver_area_size (svq); + device_size =vhost_svq_device_area_size (svq); + svq->vring.desc =qemu_memalign (qemu_real_host_page_size, driver_size); + svq->vring.avail = (void *)((char *)svq->vring.desc + desc_size); +memset (svq->vring.desc,0 , driver_size); + svq->vring.used =qemu_memalign (qemu_real_host_page_size, device_size); +memset (svq->vring.used,0 , device_size); +event_notifier_set_handler (&svq->hdev_call, vhost_svq_handle_call);return g_steal_pointer (&svq);
@@ -318,5 +335,7 @@ void vhost_svq_free(VhostShadowVirtqueue *vq)event_notifier_cleanup (&vq->hdev_kick);event_notifier_set_handler (&vq->hdev_call,NULL );event_notifier_cleanup (&vq->hdev_call); +qemu_vfree (vq->vring.desc); +qemu_vfree (vq->vring.used);g_free (vq); }
hw/virtio/vhost-vdpa.c +122 -11
206 lines not on this page · raw message
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 9/31 vhost-vdpa: Take into account SVQ in vhost_vdpa_set_vring_call
In reply to Eugenio Pérez--- hw/virtio/vhost-vdpa.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)
hw/virtio/vhost-vdpa.c +18 -2
@@ -687,13 +687,29 @@ static int vhost_vdpa_set_vring_kick(struct vhost_dev *dev, } } -static int vhost_vdpa_set_vring_call (struct vhost_dev *dev, -struct vhost_vring_file *file) +static int vhost_vdpa_set_vring_dev_call (struct vhost_dev *dev, +struct vhost_vring_file *file) {trace_vhost_vdpa_set_vring_call (dev, file->index, file->fd);return vhost_vdpa_call (dev,VHOST_SET_VRING_CALL , file); } +static int vhost_vdpa_set_vring_call (struct vhost_dev *dev, +struct vhost_vring_file *file) +{ +struct vhost_vdpa *v = dev->opaque; + +if (v->shadow_vqs_enabled) { +int vdpa_idx =vhost_vdpa_get_vq_index (dev, file->index); +VhostShadowVirtqueue *svq =g_ptr_array_index (v->shadow_vqs, vdpa_idx); + +vhost_svq_set_guest_call_notifier (svq, file->fd); +return 0 ; + }else { +return vhost_vdpa_set_vring_dev_call (dev, file); + } +} +/** * Set shadow virtqueue descriptors to the device *
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 1/31 vdpa: Reorder virtio/vhost-vdpa.c functions
In reply to Eugenio Pérezvhost_vdpa_set_features and vhost_vdpa_init need to use vhost_vdpa_get_features in svq mode.
vhost_vdpa_dev_start needs to use almost all _set_ functions:vhost_vdpa_set_vring_dev_kick, vhost_vdpa_set_vring_dev_call, vhost_vdpa_set_dev_vring_base and vhost_vdpa_set_dev_vring_num.
No functional change intended.
--- hw/virtio/vhost-vdpa.c | 164 ++++++++++++++++++++--------------------- 1 file changed, 82 insertions(+), 82 deletions(-)
hw/virtio/vhost-vdpa.c +82 -82
@@ -342,41 +342,6 @@ static bool vhost_vdpa_one_time_request(struct vhost_dev *dev)return v->index !=0 ; } -static int vhost_vdpa_init (struct vhost_dev *dev,void *opaque,Error **errp) -{ -struct vhost_vdpa *v; -assert (dev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_VDPA ); -trace_vhost_vdpa_init (dev, opaque); -int ret; - -/* -* Similar to VFIO, we end up pinning all guest memory and have to -* disable discarding of RAM. -*/ - ret =ram_block_discard_disable (true); -if (ret) { -error_report ("Cannot set discarding of RAM broken" ); -return ret; - } - - v = opaque; - v->dev = dev; - dev->opaque = opaque ; - v->listener = vhost_vdpa_memory_listener; - v->msg_type =VHOST_IOTLB_MSG_V2 ; - -vhost_vdpa_get_iova_range (v); - -if (vhost_vdpa_one_time_request (dev)) { -return 0 ; - } - -vhost_vdpa_add_status (dev,VIRTIO_CONFIG_S_ACKNOWLEDGE | -VIRTIO_CONFIG_S_DRIVER ); - -return 0 ; -} -static void vhost_vdpa_host_notifier_uninit (struct vhost_dev *dev,int queue_index) {
@@ -506,24 +471,6 @@ static int vhost_vdpa_set_mem_table(struct vhost_dev *dev,return 0 ; } -static int vhost_vdpa_set_features (struct vhost_dev *dev, -uint64_t features) -{ -int ret; - -if (vhost_vdpa_one_time_request (dev)) { -return 0 ; - } - -trace_vhost_vdpa_set_features (dev, features); - ret =vhost_vdpa_call (dev,VHOST_SET_FEATURES , &features); -if (ret) { -return ret; - } - -return vhost_vdpa_add_status (dev,VIRTIO_CONFIG_S_FEATURES_OK ); -} -static int vhost_vdpa_set_backend_cap (struct vhost_dev *dev) {uint64_t features;
@@ -646,35 +593,6 @@ static int vhost_vdpa_get_config(struct vhost_dev *dev, uint8_t *config,return ret; } -static int vhost_vdpa_dev_start (struct vhost_dev *dev,bool started) -{ -struct vhost_vdpa *v = dev->opaque; -trace_vhost_vdpa_dev_start (dev, started); - -if (started) { -vhost_vdpa_host_notifiers_init (dev); -vhost_vdpa_set_vring_ready (dev); - }else { -vhost_vdpa_host_notifiers_uninit (dev, dev->nvqs); - } - -if (dev->vq_index + dev->nvqs != dev->vq_index_end) { -return 0 ; - } - -if (started) { -memory_listener_register (&v->listener, &address_space_memory); -return vhost_vdpa_add_status (dev,VIRTIO_CONFIG_S_DRIVER_OK ); - }else { -vhost_vdpa_reset_device (dev); -vhost_vdpa_add_status (dev,VIRTIO_CONFIG_S_ACKNOWLEDGE | -VIRTIO_CONFIG_S_DRIVER ); -memory_listener_unregister (&v->listener); - -return 0 ; - } -} -static int vhost_vdpa_set_log_base (struct vhost_dev *dev,uint64_t base,struct vhost_log *log) {
@@ -735,6 +653,35 @@ static int vhost_vdpa_set_vring_call(struct vhost_dev *dev,return vhost_vdpa_call (dev,VHOST_SET_VRING_CALL , file); } +static int vhost_vdpa_dev_start (struct vhost_dev *dev,bool started) +{ +struct vhost_vdpa *v = dev->opaque; +trace_vhost_vdpa_dev_start (dev, started); + +if (started) { +vhost_vdpa_host_notifiers_init (dev); +vhost_vdpa_set_vring_ready (dev); + }else { +vhost_vdpa_host_notifiers_uninit (dev, dev->nvqs); + } + +if (dev->vq_index + dev->nvqs != dev->vq_index_end) { +return 0 ; + } + +if (started) { +memory_listener_register (&v->listener, &address_space_memory); +return vhost_vdpa_add_status (dev,VIRTIO_CONFIG_S_DRIVER_OK ); + }else { +vhost_vdpa_reset_device (dev); +vhost_vdpa_add_status (dev,VIRTIO_CONFIG_S_ACKNOWLEDGE | +VIRTIO_CONFIG_S_DRIVER ); +memory_listener_unregister (&v->listener); + +return 0 ; + } +} +static int vhost_vdpa_get_features (struct vhost_dev *dev,uint64_t *features) {
@@ -745,6 +692,24 @@ static int vhost_vdpa_get_features(struct vhost_dev *dev,return ret; } +static int vhost_vdpa_set_features (struct vhost_dev *dev, +uint64_t features) +{ +int ret; + +if (vhost_vdpa_one_time_request (dev)) { +return 0 ; + } + +trace_vhost_vdpa_set_features (dev, features); + ret =vhost_vdpa_call (dev,VHOST_SET_FEATURES , &features); +if (ret) { +return ret; + } + +return vhost_vdpa_add_status (dev,VIRTIO_CONFIG_S_FEATURES_OK ); +} +static int vhost_vdpa_set_owner (struct vhost_dev *dev) {if (vhost_vdpa_one_time_request (dev)) {
@@ -772,6 +737,41 @@ static bool vhost_vdpa_force_iommu(struct vhost_dev *dev)return true; } +static int vhost_vdpa_init (struct vhost_dev *dev,void *opaque,Error **errp) +{ +struct vhost_vdpa *v; +assert (dev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_VDPA ); +trace_vhost_vdpa_init (dev, opaque); +int ret; + +/* +* Similar to VFIO, we end up pinning all guest memory and have to +* disable discarding of RAM. +*/ + ret =ram_block_discard_disable (true); +if (ret) { +error_report ("Cannot set discarding of RAM broken" ); +return ret; + } + + v = opaque; + v->dev = dev; + dev->opaque = opaque ; + v->listener = vhost_vdpa_memory_listener; + v->msg_type =VHOST_IOTLB_MSG_V2 ; + +vhost_vdpa_get_iova_range (v); + +if (vhost_vdpa_one_time_request (dev)) { +return 0 ; + } + +vhost_vdpa_add_status (dev,VIRTIO_CONFIG_S_ACKNOWLEDGE | +VIRTIO_CONFIG_S_DRIVER ); + +return 0 ; +} +const VhostOps vdpa_ops = { .backend_type =VHOST_BACKEND_TYPE_VDPA , .vhost_backend_init = vhost_vdpa_init,
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 23/31 vdpa: Add custom IOTLB translations to SVQ
In reply to Eugenio PérezUse translations added in VhostIOVATree in SVQ.
Only introduce usage here, not allocation and deallocation. As with previous patches, we use the dead code paths of shadow_vqs_enabled to avoid commiting too many changes at once. These are impossible to take at the moment.
--- hw/virtio/vhost-shadow-virtqueue.h | 3 +- include/hw/virtio/vhost-vdpa.h | 3 + hw/virtio/vhost-shadow-virtqueue.c | 111 ++++++++++++++++---- hw/virtio/vhost-vdpa.c | 161 +++++++++++++++++++++++++---- 4 files changed, 238 insertions(+), 40 deletions(-)
hw/virtio/vhost-shadow-virtqueue.h +2 -1
@@ -12,6 +12,7 @@#include "hw/virtio/vhost.h" #include "qemu/event_notifier.h" +#include "hw/virtio/vhost-iova-tree.h" typedef struct VhostShadowVirtqueue VhostShadowVirtqueue ;
@@ -37,7 +38,7 @@ void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIODevice *vdev, VirtQueue *vq);void vhost_svq_stop (VhostShadowVirtqueue *svq); -VhostShadowVirtqueue *vhost_svq_new (uint16_t qsize); +VhostShadowVirtqueue *vhost_svq_new (uint16_t qsize,VhostIOVATree *iova_map);void vhost_svq_free (VhostShadowVirtqueue *vq);
include/hw/virtio/vhost-vdpa.h +3 -0
@@ -14,6 +14,7 @@#include <gmodule.h> +#include "hw/virtio/vhost-iova-tree.h" #include "hw/virtio/virtio.h" #include "standard-headers/linux/vhost_types.h"
@@ -30,6 +31,8 @@ typedef struct vhost_vdpa {MemoryListener listener;struct vhost_vdpa_iova_range iova_range;bool shadow_vqs_enabled; +/* IOVA mapping used by Shadow Virtqueue */ +VhostIOVATree *iova_tree;GPtrArray *shadow_vqs;struct vhost_dev *dev;VhostVDPAHostNotifier notifier[VIRTIO_QUEUE_MAX ];
hw/virtio/vhost-shadow-virtqueue.c +91 -20
203 lines not on this page · raw message
hw/virtio/vhost-vdpa.c +142 -19
239 lines not on this page · raw message
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 10/31 vhost: Route host->guest notification through shadow virtqueue
In reply to Eugenio PérezThis will make qemu aware of the device used buffers, allowing it to write the guest memory with its contents if needed.
--- hw/virtio/vhost-shadow-virtqueue.c | 15 +++++++++++++++ hw/virtio/vhost-vdpa.c | 11 +++++++++++ 2 files changed, 26 insertions(+)
hw/virtio/vhost-shadow-virtqueue.c +15 -0
@@ -58,6 +58,19 @@ static void vhost_handle_guest_kick(EventNotifier *n)event_notifier_set (&svq->hdev_kick); } +/* Forward vhost notifications */ +static void vhost_svq_handle_call (EventNotifier *n) +{ +VhostShadowVirtqueue *svq =container_of (n, VhostShadowVirtqueue, + hdev_call); + +if (unlikely (!event_notifier_test_and_clear (n))) { +return ; + } + +event_notifier_set (&svq->svq_call); +} +/** * Obtain the SVQ call notifier, where vhost device notifies SVQ that there * exists pending used buffers.
@@ -150,6 +163,7 @@ VhostShadowVirtqueue *vhost_svq_new(void)/* Placeholder descriptor, it should be deleted at set_kick_fd */ event_notifier_init_fd (&svq->svq_kick,INVALID_SVQ_KICK_FD ); +event_notifier_set_handler (&svq->hdev_call, vhost_svq_handle_call);return g_steal_pointer (&svq); err_init_hdev_call:
@@ -165,6 +179,7 @@ err_init_hdev_kick:void vhost_svq_free (VhostShadowVirtqueue *vq) {event_notifier_cleanup (&vq->hdev_kick); +event_notifier_set_handler (&vq->hdev_call,NULL );event_notifier_cleanup (&vq->hdev_call);g_free (vq); }
hw/virtio/vhost-vdpa.c +11 -0
@@ -716,6 +716,9 @@ static int vhost_vdpa_set_vring_call(struct vhost_dev *dev,* @dev The vhost device model * @svq The shadow virtqueue * @idx The index of the virtqueue in the vhost device +* +* Note that this function does not rewind kick file descriptor if cannot set +* call one. */ static bool vhost_vdpa_svq_setup(struct vhost_dev *dev, VhostShadowVirtqueue *svq,
@@ -732,6 +735,14 @@ static bool vhost_vdpa_svq_setup(struct vhost_dev *dev, r =vhost_vdpa_set_vring_dev_kick (dev, &file);if (unlikely (r !=0 )) {error_report ("Can't set device kick fd (%d)" , -r); +return false; + } + + event_notifier =vhost_svq_get_svq_call_notifier (svq); + file.fd =event_notifier_get_fd (event_notifier); + r =vhost_vdpa_set_vring_dev_call (dev, &file); +if (unlikely (r !=0 )) { +error_report ("Can't set device call fd (%d)" , -r); }return r ==0 ;
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 7/31 vhost: dd vhost_svq_get_svq_call_notifier
In reply to Eugenio PérezThis allows vhost-vdpa device to retrieve device -> svq call eventfd.
--- hw/virtio/vhost-shadow-virtqueue.h | 2 ++ hw/virtio/vhost-shadow-virtqueue.c | 12 ++++++++++++ 2 files changed, 14 insertions(+)
hw/virtio/vhost-shadow-virtqueue.h +2 -0
@@ -18,6 +18,8 @@ typedef struct VhostShadowVirtqueue VhostShadowVirtqueue;void vhost_svq_set_svq_kick_fd (VhostShadowVirtqueue *svq,int svq_kick_fd);const EventNotifier *vhost_svq_get_dev_kick_notifier (const VhostShadowVirtqueue *svq); +const EventNotifier *vhost_svq_get_svq_call_notifier ( +const VhostShadowVirtqueue *svq);void vhost_svq_stop (VhostShadowVirtqueue *svq);
hw/virtio/vhost-shadow-virtqueue.c +12 -0
@@ -55,6 +55,18 @@ static void vhost_handle_guest_kick(EventNotifier *n)event_notifier_set (&svq->hdev_kick); } +/** +* Obtain the SVQ call notifier, where vhost device notifies SVQ that there +* exists pending used buffers. +* +* @svq Shadow Virtqueue +*/ +const EventNotifier *vhost_svq_get_svq_call_notifier ( +const VhostShadowVirtqueue *svq) +{ +return &svq->hdev_call; +} +/** * Set a new file descriptor for the guest to kick SVQ and notify for avail *
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 4/31 vdpa: Add vhost_svq_set_svq_kick_fd
In reply to Eugenio PérezThis function allows the vhost-vdpa backend to override kick_fd.
--- hw/virtio/vhost-shadow-virtqueue.h | 1 + hw/virtio/vhost-shadow-virtqueue.c | 45 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+)
hw/virtio/vhost-shadow-virtqueue.h +1 -0
@@ -15,6 +15,7 @@typedef struct VhostShadowVirtqueue VhostShadowVirtqueue ; +void vhost_svq_set_svq_kick_fd (VhostShadowVirtqueue *svq,int svq_kick_fd);const EventNotifier *vhost_svq_get_dev_kick_notifier (const VhostShadowVirtqueue *svq);
hw/virtio/vhost-shadow-virtqueue.c +45 -0
71 lines not on this page · raw message
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 26/31 vdpa: Clear VHOST_VRING_F_LOG at vhost_vdpa_set_vring_addr in SVQ
In reply to Eugenio PérezDoing so would cause the device to export writes to SVQ addresses, which are not part of the guest's IOVA.
Like the previous patch, this is currently not possible since SVQ does not run if the device exports VHOST_VRING_F_LOG. But it's needed to enable migration with SVQ.
--- hw/virtio/vhost-vdpa.c | 6 ++++++ 1 file changed, 6 insertions(+)
hw/virtio/vhost-vdpa.c +6 -0
@@ -660,10 +660,16 @@ static int vhost_vdpa_set_log_base(struct vhost_dev *dev, uint64_t base,static int vhost_vdpa_set_vring_addr (struct vhost_dev *dev,struct vhost_vring_addr *addr) { +struct vhost_vdpa *v = dev->opaque; +trace_vhost_vdpa_set_vring_addr (dev, addr->index, addr->flags, addr->desc_user_addr, addr->used_user_addr, addr->avail_user_addr, addr->log_guest_addr); + +if (v->shadow_vqs_enabled) { + addr->flags &= ~BIT_ULL (VHOST_VRING_F_LOG ); + }return vhost_vdpa_call (dev,VHOST_SET_VRING_ADDR , addr); }
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 11/31 vhost: Add vhost_svq_valid_device_features to shadow vq
In reply to Eugenio PérezThis allows SVQ to negotiate features with the device. For the device, SVQ is a driver. While this function needs to bypass all non-transport features, it needs to disable the features that SVQ does not support when forwarding buffers. This includes packed vq layout, indirect descriptors or event idx.
--- hw/virtio/vhost-shadow-virtqueue.h | 2 ++ hw/virtio/vhost-shadow-virtqueue.c | 44 ++++++++++++++++++++++++++++++ hw/virtio/vhost-vdpa.c | 21 ++++++++++++++ 3 files changed, 67 insertions(+)
hw/virtio/vhost-shadow-virtqueue.h +2 -0
@@ -15,6 +15,8 @@typedef struct VhostShadowVirtqueue VhostShadowVirtqueue ; +bool vhost_svq_valid_device_features (uint64_t *features); +void vhost_svq_set_svq_kick_fd (VhostShadowVirtqueue *svq,int svq_kick_fd);void vhost_svq_set_guest_call_notifier (VhostShadowVirtqueue *svq,int call_fd); const EventNotifier *vhost_svq_get_dev_kick_notifier(
hw/virtio/vhost-shadow-virtqueue.c +44 -0
50 lines not on this page · raw message
hw/virtio/vhost-vdpa.c +21 -0
@@ -855,10 +855,31 @@ static int vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v,size_t n_svqs = v->shadow_vqs_enabled ? hdev->nvqs :0 ;g_autoptr (GPtrArray) shadow_vqs =g_ptr_array_new_full (n_svqs, vhost_psvq_free); +uint64_t dev_features; +uint64_t svq_features; +int r; +bool ok; +if (!v->shadow_vqs_enabled) { goto out; } + r =vhost_vdpa_get_features (hdev, &dev_features); +if (r !=0 ) { +error_setg (errp,"Can't get vdpa device features, got (%d)" , r); +return r; + } + + svq_features = dev_features; + ok =vhost_svq_valid_device_features (&svq_features); +if (unlikely (!ok)) { +error_setg (errp, +"SVQ Invalid device feature flags, offer: 0x%" PRIx64", ok: 0x%" PRIx64, + hdev->features, svq_features); +return -1 ; + } + + shadow_vqs =g_ptr_array_new_full (hdev->nvqs, vhost_psvq_free);for (unsigned n =0 ; n < hdev->nvqs; ++n) {VhostShadowVirtqueue *svq =vhost_svq_new ();
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 6/31 vhost: Route guest->host notification through shadow virtqueue
In reply to Eugenio PérezAt this moment no buffer forwarding will be performed in SVQ mode: Qemu just forward the guest's kicks to the device. This commit also set up SVQs in the vhost device.
Host memory notifiers regions are left out for simplicity, and they will not be addressed in this series.
--- include/hw/virtio/vhost-vdpa.h | 4 ++ hw/virtio/vhost-vdpa.c | 122 ++++++++++++++++++++++++++++++++- 2 files changed, 124 insertions(+), 2 deletions(-)
include/hw/virtio/vhost-vdpa.h +4 -0
@@ -12,6 +12,8 @@#ifndef HW_VIRTIO_VHOST_VDPA_H #define HW_VIRTIO_VHOST_VDPA_H +#include <gmodule.h> +#include "hw/virtio/virtio.h" #include "standard-headers/linux/vhost_types.h"
@@ -27,6 +29,8 @@ typedef struct vhost_vdpa {bool iotlb_batch_begin_sent;MemoryListener listener;struct vhost_vdpa_iova_range iova_range; +bool shadow_vqs_enabled; +GPtrArray *shadow_vqs;struct vhost_dev *dev;VhostVDPAHostNotifier notifier[VIRTIO_QUEUE_MAX ]; } VhostVDPA;
hw/virtio/vhost-vdpa.c +120 -2
204 lines not on this page · raw message
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 27/31 vdpa: Never set log_base addr if SVQ is enabled
In reply to Eugenio PérezSetting the log address would make the device start reporting invalid dirty memory because the SVQ vrings are located in qemu's memory.
--- hw/virtio/vhost-vdpa.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
hw/virtio/vhost-vdpa.c +2 -1
@@ -648,7 +648,8 @@ static int vhost_vdpa_get_config(struct vhost_dev *dev, uint8_t *config,static int vhost_vdpa_set_log_base (struct vhost_dev *dev,uint64_t base,struct vhost_log *log) { -if (vhost_vdpa_one_time_request (dev)) { +struct vhost_vdpa *v = dev->opaque; +if (v->shadow_vqs_enabled ||vhost_vdpa_one_time_request (dev)) {return 0 ; }
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 14/31 virtio: Add vhost_shadow_vq_get_vring_addr
In reply to Eugenio PérezIt reports the shadow virtqueue address from qemu virtual address space.
Since this will be different from the guest's vaddr, but the device can access it, SVQ takes special care about its alignment & lack of garbage data. It assumes that IOMMU will work in host_page_size ranges for that.
--- hw/virtio/vhost-shadow-virtqueue.h | 4 ++++ hw/virtio/vhost-shadow-virtqueue.c | 33 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+)
hw/virtio/vhost-shadow-virtqueue.h +4 -0
@@ -27,6 +27,10 @@ const EventNotifier *vhost_svq_get_dev_kick_notifier( const VhostShadowVirtqueue *svq);const EventNotifier *vhost_svq_get_svq_call_notifier (const VhostShadowVirtqueue *svq); +void vhost_svq_get_vring_addr (const VhostShadowVirtqueue *svq, +struct vhost_vring_addr *addr); +size_t vhost_svq_driver_area_size (const VhostShadowVirtqueue *svq); +size_t vhost_svq_device_area_size (const VhostShadowVirtqueue *svq);void vhost_svq_stop (VhostShadowVirtqueue *svq);
hw/virtio/vhost-shadow-virtqueue.c +33 -0
@@ -9,12 +9,16 @@#include "qemu/osdep.h" #include "hw/virtio/vhost-shadow-virtqueue.h" +#include "standard-headers/linux/vhost_types.h" #include "qemu/error-report.h" #include "qemu/main-loop.h" /* Shadow virtqueue to relay notifications */ typedef struct VhostShadowVirtqueue { + /* Shadow vring */ + struct vring vring; + /* Shadow kick notifier, sent to vhost */ EventNotifier hdev_kick; /* Shadow call notifier, sent to vhost */
@@ -195,6 +199,35 @@ void vhost_svq_set_guest_call_notifier(VhostShadowVirtqueue *svq, int call_fd)event_notifier_init_fd (&svq->svq_call, call_fd); } +/* +* Get the shadow vq vring address. +* @svq Shadow virtqueue +* @addr Destination to store address +*/ +void vhost_svq_get_vring_addr (const VhostShadowVirtqueue *svq, +struct vhost_vring_addr *addr) +{ + addr->desc_user_addr = (uint64_t )svq->vring.desc; + addr->avail_user_addr = (uint64_t )svq->vring.avail; + addr->used_user_addr = (uint64_t )svq->vring.used; +} + +size_t vhost_svq_driver_area_size (const VhostShadowVirtqueue *svq) +{ +size_t desc_size =sizeof (vring_desc_t) * svq->vring.num; +size_t avail_size = offsetof(vring_avail_t , ring) + +sizeof (uint16_t ) * svq->vring.num; + +return ROUND_UP (desc_size + avail_size, qemu_real_host_page_size); +} + +size_t vhost_svq_device_area_size (const VhostShadowVirtqueue *svq) +{ +size_t used_size = offsetof(vring_used_t , ring) + +sizeof (vring_used_elem_t) * svq->vring.num; +return ROUND_UP (used_size, qemu_real_host_page_size); +} +/** * Set a new file descriptor for the guest to kick SVQ and notify for avail *
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 19/31 utils: Add internal DMAMap to iova-tree
In reply to Eugenio PérezSo we can store private data not accessible from outside.
In this case, we will add intrusive linked list members so we can transverse it for allocation.
--- util/iova-tree.c | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-)
util/iova-tree.c +26 -11
@@ -12,13 +12,18 @@#include "qemu/osdep.h" #include "qemu/iova-tree.h" +typedef struct DMAMapInternal { +DMAMap map; +}DMAMapInternal ; +struct IOVATree {GTree *tree; };static int iova_tree_compare (gconstpointer a,gconstpointer b,gpointer data) { -const DMAMap *m1 = a, *m2 = b; +const DMAMapInternal *i1 = a, *i2 = b; +const DMAMap *m1 = &i1->map, *m2 = &i2->map;if (m1->iova > m2->iova + m2->size) {return 1 ;
@@ -42,9 +47,18 @@ IOVATree *iova_tree_new(void)return iova_tree; } +static DMAMapInternal *iova_tree_find_internal (const IOVATree *tree, +const DMAMap *map) +{ +const DMAMapInternal map_internal = { .map = *map }; + +return g_tree_lookup (tree->tree, &map_internal); +} +const DMAMap *iova_tree_find (const IOVATree *tree,const DMAMap *map) { -return g_tree_lookup (tree->tree, map); +const DMAMapInternal *ret =iova_tree_find_internal (tree, map); +return &ret->map; }const DMAMap *iova_tree_find_address (const IOVATree *tree,hwaddr iova)
@@ -54,7 +68,8 @@ const DMAMap *iova_tree_find_address(const IOVATree *tree, hwaddr iova)return iova_tree_find (tree, &map); } -static inline void iova_tree_insert_internal (GTree *gtree,DMAMap *range) +static inline void iova_tree_insert_internal (GTree *gtree, +DMAMapInternal *range) {/* Key and value are sharing the same range data */ g_tree_insert (gtree, range, range);
@@ -62,7 +77,7 @@ static inline void iova_tree_insert_internal(GTree *gtree, DMAMap *range)int iova_tree_insert (IOVATree *tree,const DMAMap *map) { -DMAMap *new; +DMAMapInternal *new;if (map->iova + map->size < map->iova || map->perm ==IOMMU_NONE ) {return IOVA_ERR_INVALID ;
@@ -73,8 +88,8 @@ int iova_tree_insert(IOVATree *tree, const DMAMap *map)return IOVA_ERR_OVERLAP ; } - new =g_new0 (DMAMap,1 ); -memcpy (new, map,sizeof (*new)); + new =g_new0 (DMAMapInternal,1 ); +memcpy (&new->map, map,sizeof (new->map));iova_tree_insert_internal (tree->tree, new);return IOVA_OK ;
@@ -84,11 +99,11 @@ static gboolean iova_tree_traverse(gpointer key, gpointer value, gpointer data) {iova_tree_iterator iterator = data; -DMAMap *map = key; +DMAMapInternal *map = key;g_assert (key == value); -return iterator (map); +return iterator (&map->map); }void iova_tree_foreach (IOVATree *tree,iova_tree_iterator iterator)
@@ -98,10 +113,10 @@ void iova_tree_foreach(IOVATree *tree, iova_tree_iterator iterator)int iova_tree_remove (IOVATree *tree,const DMAMap *map) { -const DMAMap *overlap; +DMAMapInternal *overlap_internal; -while ((overlap =iova_tree_find (tree, map))) { -g_tree_remove (tree->tree, overlap); +while ((overlap_internal =iova_tree_find_internal (tree, map))) { +g_tree_remove (tree->tree, overlap_internal); }return IOVA_OK ;
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 8/31 vhost: Add vhost_svq_set_guest_call_notifier
In reply to Eugenio PérezThis allows the vhost-vdpa device to set SVQ -> guest notifier to SVQ.
--- hw/virtio/vhost-shadow-virtqueue.h | 1 + hw/virtio/vhost-shadow-virtqueue.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+)
hw/virtio/vhost-shadow-virtqueue.h +1 -0
@@ -16,6 +16,7 @@typedef struct VhostShadowVirtqueue VhostShadowVirtqueue ;void vhost_svq_set_svq_kick_fd (VhostShadowVirtqueue *svq,int svq_kick_fd); +void vhost_svq_set_guest_call_notifier (VhostShadowVirtqueue *svq,int call_fd);const EventNotifier *vhost_svq_get_dev_kick_notifier (const VhostShadowVirtqueue *svq); const EventNotifier *vhost_svq_get_svq_call_notifier(
hw/virtio/vhost-shadow-virtqueue.c +16 -0
@@ -29,6 +29,9 @@ typedef struct VhostShadowVirtqueue {* So shadow virtqueue must not clean it, or we would lose VirtQueue one. */ EventNotifier svq_kick; + +/* Guest's call notifier, where SVQ calls guest. */ +EventNotifier svq_call; } VhostShadowVirtqueue;#define INVALID_SVQ_KICK_FD -1
@@ -67,6 +70,19 @@ const EventNotifier *vhost_svq_get_svq_call_notifier(return &svq->hdev_call; } +/** +* Set the call notifier for the SVQ to call the guest +* +* @svq Shadow virtqueue +* @call_fd call notifier +* +* Called on BQL context. +*/ +void vhost_svq_set_guest_call_notifier (VhostShadowVirtqueue *svq,int call_fd) +{ +event_notifier_init_fd (&svq->svq_call, call_fd); +} +/** * Set a new file descriptor for the guest to kick SVQ and notify for avail *
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 16/31 vhost: pass queue index to vhost_vq_get_addr
In reply to Eugenio PérezDoing that way allows vhost backend to know what address to return.
--- hw/virtio/vhost.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
hw/virtio/vhost.c +3 -3
@@ -798,9 +798,10 @@ static int vhost_virtqueue_set_addr(struct vhost_dev *dev,struct vhost_virtqueue *vq, unsigned idx, bool enable_log) { - struct vhost_vring_addr addr; + struct vhost_vring_addr addr = { + .index = idx, + };int r; -memset (&addr,0 ,sizeof (struct vhost_vring_addr ));if (dev->vhost_ops->vhost_vq_get_addr) { r = dev->vhost_ops->vhost_vq_get_addr (dev, &addr, vq);
@@ -813,7 +814,6 @@ static int vhost_virtqueue_set_addr(struct vhost_dev *dev, addr.avail_user_addr = (uint64_t )(unsigned long )vq->avail; addr.used_user_addr = (uint64_t )(unsigned long )vq->used; } - addr.index = idx; addr.log_guest_addr = vq->used_phys; addr.flags = enable_log ? (1 <<VHOST_VRING_F_LOG ) :0 ; r = dev->vhost_ops->vhost_set_vring_addr (dev, &addr);
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 22/31 vhost: Add VhostIOVATree
In reply to Eugenio PérezThis tree is able to look for a translated address from an IOVA address.
At first glance it is similar to util/iova-tree. However, SVQ working on devices with limited IOVA space need more capabilities, like allocating IOVA chunks or performing reverse translations (qemu addresses to iova).
The allocation capability, as "assign a free IOVA address to this chunk of memory in qemu's address space" allows shadow virtqueue to create a new address space that is not restricted by guest's addressable one, so we can allocate shadow vqs vrings outside of it.
It duplicates the tree so it can search efficiently both directions, and it will signal overlap if iova or the translated address is present in any tree.
--- hw/virtio/vhost-iova-tree.h | 27 +++++++ hw/virtio/vhost-iova-tree.c | 157 ++++++++++++++++++++++++++++++++++++ hw/virtio/meson.build | 2 +- 3 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 hw/virtio/vhost-iova-tree.h create mode 100644 hw/virtio/vhost-iova-tree.c
hw/virtio/vhost-iova-tree.h new file +27 -0
@@ -0,0 +1,27 @@ +/* +* vhost software live migration ring +* +* SPDX-FileCopyrightText: Red Hat, Inc. 2021 +* SPDX-FileContributor: Author: Eugenio Pérez <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef HW_VIRTIO_VHOST_IOVA_TREE_H +#define HW_VIRTIO_VHOST_IOVA_TREE_H + +#include "qemu/iova-tree.h" +#include "exec/memory.h" + +typedef struct VhostIOVATree VhostIOVATree ; + +VhostIOVATree *vhost_iova_tree_new (uint64_t iova_first,uint64_t iova_last); +void vhost_iova_tree_delete (VhostIOVATree *iova_tree); +G_DEFINE_AUTOPTR_CLEANUP_FUNC (VhostIOVATree, vhost_iova_tree_delete); + +const DMAMap *vhost_iova_tree_find_iova (const VhostIOVATree *iova_tree, +const DMAMap *map); +int vhost_iova_tree_map_alloc (VhostIOVATree *iova_tree,DMAMap *map); +void vhost_iova_tree_remove (VhostIOVATree *iova_tree,const DMAMap *map); + +#endif
hw/virtio/vhost-iova-tree.c new file +157 -0
157 lines not on this page · raw message
hw/virtio/meson.build +1 -1
@@ -11,7 +11,7 @@ softmmu_ss.add(when: 'CONFIG_ALL', if_true: files('vhost-stub.c')) virtio_ss = ss.source_set() virtio_ss.add(files('virtio.c')) -virtio_ss.add(when: 'CONFIG_VHOST', if_true: files('vhost.c', 'vhost-backend.c', 'vhost-shadow-virtqueue.c')) +virtio_ss.add(when: 'CONFIG_VHOST', if_true: files('vhost.c', 'vhost-backend.c', 'vhost-shadow-virtqueue.c', 'vhost-iova-tree.c')) virtio_ss.add(when: 'CONFIG_VHOST_USER', if_true: files('vhost-user.c')) virtio_ss.add(when: 'CONFIG_VHOST_VDPA', if_true: files('vhost-vdpa.c')) virtio_ss.add(when: 'CONFIG_VIRTIO_BALLOON', if_true: files('virtio-balloon.c'))
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 12/31 vhost: Add vhost_svq_valid_guest_features to shadow vq
In reply to Eugenio PérezThis allows it to test if the guest has acknowledged an invalid transport feature for SVQ. This will include packed vq layout or event_idx, where the VirtIO device needs help from SVQ.
It is not needed at this moment, but since SVQ will not re-negotiate features again with the guest, a failure to acknowledge them is fatal for SVQ.
--- hw/virtio/vhost-shadow-virtqueue.h | 1 + hw/virtio/vhost-shadow-virtqueue.c | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+)
hw/virtio/vhost-shadow-virtqueue.h +1 -0
@@ -16,6 +16,7 @@typedef struct VhostShadowVirtqueue VhostShadowVirtqueue ;bool vhost_svq_valid_device_features (uint64_t *features); +bool vhost_svq_valid_guest_features (uint64_t *features);void vhost_svq_set_svq_kick_fd (VhostShadowVirtqueue *svq,int svq_kick_fd);void vhost_svq_set_guest_call_notifier (VhostShadowVirtqueue *svq,int call_fd);
hw/virtio/vhost-shadow-virtqueue.c +24 -0
@@ -89,6 +89,30 @@ bool vhost_svq_valid_device_features(uint64_t *dev_features)return r; } +/** +* Offers SVQ valid transport features to the guest. +* +* @guest_features The device's supported features. Return SVQ's if success. +* +* Returns true if SVQ can handle them, false otherwise. +*/ +bool vhost_svq_valid_guest_features (uint64_t *guest_features) +{ +static const uint64_t transport =MAKE_64BIT_MASK (VIRTIO_TRANSPORT_F_START , +VIRTIO_TRANSPORT_F_END -VIRTIO_TRANSPORT_F_START ); + +/* These transport features are handled by VirtQueue */ +static const uint64_t valid =BIT_ULL (VIRTIO_RING_F_INDIRECT_DESC ) | +BIT_ULL (VIRTIO_F_VERSION_1 ) | +BIT_ULL (VIRTIO_F_IOMMU_PLATFORM ); + +/* We are only interested in transport-related feature bits */ +uint64_t guest_transport_features = (*guest_features) & transport; + + *guest_features &= (valid | ~transport); +return !(guest_transport_features & (transport ^ valid)); +} +/* Forward guest notifications */ static void vhost_handle_guest_kick (EventNotifier *n) {
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 18/31 vhost: Shadow virtqueue buffers forwarding
In reply to Eugenio PérezInitial version of shadow virtqueue that actually forward buffers. There is no iommu support at the moment, and that will be addressed in future patches of this series. Since all vhost-vdpa devices use forced IOMMU, this means that SVQ is not usable at this point of the series on any device.
For simplicity it only supports modern devices, that expects vring in little endian, with split ring and no event idx or indirect descriptors. Support for them will not be added in this series.
It reuses the VirtQueue code for the device part. The driver part is based on Linux's virtio_ring driver, but with stripped functionality and optimizations so it's easier to review.
However, forwarding buffers have some particular pieces: One of the most unexpected ones is that a guest's buffer can expand through more than one descriptor in SVQ. While this is handled gracefully by qemu's emulated virtio devices, it may cause unexpected SVQ queue full. This patch also solves it by checking for this condition at both guest's kicks and device's calls. The code may be more elegant in the future if SVQ code runs in its own iocontext.
--- hw/virtio/vhost-shadow-virtqueue.h | 2 + hw/virtio/vhost-shadow-virtqueue.c | 365 ++++++++++++++++++++++++++++- hw/virtio/vhost-vdpa.c | 111 ++++++++- 3 files changed, 462 insertions(+), 16 deletions(-)
hw/virtio/vhost-shadow-virtqueue.h +2 -0
@@ -33,6 +33,8 @@ uint16_t vhost_svq_get_num(const VhostShadowVirtqueue *svq);size_t vhost_svq_driver_area_size (const VhostShadowVirtqueue *svq);size_t vhost_svq_device_area_size (const VhostShadowVirtqueue *svq); +void vhost_svq_start (VhostShadowVirtqueue *svq,VirtIODevice *vdev, +VirtQueue *vq);void vhost_svq_stop (VhostShadowVirtqueue *svq);VhostShadowVirtqueue *vhost_svq_new (uint16_t qsize);
hw/virtio/vhost-shadow-virtqueue.c +353 -12
427 lines not on this page · raw message
hw/virtio/vhost-vdpa.c +107 -4
143 lines not on this page · raw message
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 30/31 vdpa: Move vhost_vdpa_get_iova_range to net/vhost-vdpa.c
In reply to Eugenio PérezSince it's a device property, it can be done in net/. This helps SVQ to allocate the rings in vdpa device initialization, rather than delay that.
--- hw/virtio/vhost-vdpa.c | 15 --------------- net/vhost-vdpa.c | 32 ++++++++++++++++++++++++-------- 2 files changed, 24 insertions(+), 23 deletions(-)
hw/virtio/vhost-vdpa.c +0 -15
@@ -350,19 +350,6 @@ static int vhost_vdpa_add_status(struct vhost_dev *dev, uint8_t status)return 0 ; } -static void vhost_vdpa_get_iova_range (struct vhost_vdpa *v) -{ -int ret =vhost_vdpa_call (v->dev,VHOST_VDPA_GET_IOVA_RANGE , - &v->iova_range); -if (ret !=0 ) { - v->iova_range.first =0 ; - v->iova_range.last =UINT64_MAX ; - } - -trace_vhost_vdpa_get_iova_range (v->dev, v->iova_range.first, - v->iova_range.last); -} -static bool vhost_vdpa_one_time_request (struct vhost_dev *dev) {struct vhost_vdpa *v = dev->opaque;
@@ -1295,8 +1282,6 @@ static int vhost_vdpa_init(struct vhost_dev *dev, void *opaque, Error **errp) goto err; } -vhost_vdpa_get_iova_range (v); -if (vhost_vdpa_one_time_request (dev)) {return 0 ; }
net/vhost-vdpa.c +24 -8
@@ -22,6 +22,7 @@#include <sys/ioctl.h> #include <err.h> #include "standard-headers/linux/virtio_net.h" +#include "standard-headers/linux/vhost_types.h" #include "monitor/monitor.h" #include "hw/virtio/vhost.h"
@@ -187,13 +188,25 @@ static NetClientInfo net_vhost_vdpa_info = { .check_peer_type = vhost_vdpa_check_peer_type, }; +static void vhost_vdpa_get_iova_range (int fd, +struct vhost_vdpa_iova_range *iova_range) +{ +int ret =ioctl (fd,VHOST_VDPA_GET_IOVA_RANGE , iova_range); + +if (ret <0 ) { + iova_range->first =0 ; + iova_range->last =UINT64_MAX ; + } +} +static NetClientState *net_vhost_vdpa_init (NetClientState *peer, -const char *device, -const char *name, -int vdpa_device_fd, -int queue_pair_index, -int nvqs, -bool is_datapath) +const char *device, +const char *name, +int vdpa_device_fd, +int queue_pair_index, +int nvqs, +bool is_datapath, +struct vhost_vdpa_iova_range iova_range) {NetClientState *nc =NULL ;VhostVDPAState *s;
@@ -211,6 +224,7 @@ static NetClientState *net_vhost_vdpa_init(NetClientState *peer, s->vhost_vdpa.device_fd = vdpa_device_fd; s->vhost_vdpa.index = queue_pair_index; + s->vhost_vdpa.iova_range = iova_range; ret =vhost_vdpa_add (nc, (void *)&s->vhost_vdpa, queue_pair_index, nvqs);if (ret) {qemu_del_net_client (nc);
@@ -267,6 +281,7 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name, g_autofree NetClientState **ncs = NULL;NetClientState *nc;int queue_pairs, i, has_cvq =0 ; +struct vhost_vdpa_iova_range iova_range;assert (netdev->type ==NET_CLIENT_DRIVER_VHOST_VDPA ); opts = &netdev->u.vhost_vdpa;
@@ -286,19 +301,20 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,qemu_close (vdpa_device_fd);return queue_pairs; } +vhost_vdpa_get_iova_range (vdpa_device_fd, &iova_range); ncs =g_malloc0 (sizeof (*ncs) * queue_pairs);for (i =0 ; i < queue_pairs; i++) { ncs[i] =net_vhost_vdpa_init (peer,TYPE_VHOST_VDPA , name, - vdpa_device_fd, i,2 , true); + vdpa_device_fd, i,2 , true, iova_range);if (!ncs[i]) goto err; }if (has_cvq) { nc =net_vhost_vdpa_init (peer,TYPE_VHOST_VDPA , name, - vdpa_device_fd, i,1 , false); + vdpa_device_fd, i,1 , false, iova_range);if (!nc) goto err; }
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 13/31 vhost: Add vhost_svq_ack_guest_features to shadow vq
In reply to Eugenio PérezThis combines the previous two feature functions, forwarding the guest ones to the device and setting the transport ones that the SVQ supports with the device.
--- hw/virtio/vhost-shadow-virtqueue.h | 3 +++ hw/virtio/vhost-shadow-virtqueue.c | 31 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+)
hw/virtio/vhost-shadow-virtqueue.h +3 -0
@@ -17,6 +17,9 @@ typedef struct VhostShadowVirtqueue VhostShadowVirtqueue;bool vhost_svq_valid_device_features (uint64_t *features);bool vhost_svq_valid_guest_features (uint64_t *features); +bool vhost_svq_ack_guest_features (uint64_t dev_features, +uint64_t guest_features, +uint64_t *acked_features);void vhost_svq_set_svq_kick_fd (VhostShadowVirtqueue *svq,int svq_kick_fd);void vhost_svq_set_guest_call_notifier (VhostShadowVirtqueue *svq,int call_fd);
hw/virtio/vhost-shadow-virtqueue.c +31 -0
@@ -113,6 +113,37 @@ bool vhost_svq_valid_guest_features(uint64_t *guest_features)return !(guest_transport_features & (transport ^ valid)); } +/** +* VirtIO features that SVQ must acknowledge to device. +* +* It combines the SVQ transport compatible features with the guest's device +* features. +* +* @dev_features The device offered features +* @guest_features The guest acknowledge features +* @acked_features The guest acknowledge features in the device side plus SVQ +* transport ones. +* +* Returns true if SVQ can work with this features, false otherwise +*/ +bool vhost_svq_ack_guest_features (uint64_t dev_features, +uint64_t guest_features, +uint64_t *acked_features) +{ +static const uint64_t transport =MAKE_64BIT_MASK (VIRTIO_TRANSPORT_F_START , +VIRTIO_TRANSPORT_F_END -VIRTIO_TRANSPORT_F_START ); + +bool ok =vhost_svq_valid_device_features (&dev_features) && +vhost_svq_valid_guest_features (&guest_features); +if (unlikely (!ok)) { +return false; + } + + *acked_features = (dev_features & transport) | + (guest_features & ~transport); +return true; +} +/* Forward guest notifications */ static void vhost_handle_guest_kick (EventNotifier *n) {
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 20/31 util: Store DMA entries in a list
In reply to Eugenio PérezSVQ need to allocate iova entries, traversing the list looking for holes.
GLib offers methods to both transverse the list and look for entries upper than a key since version 2.68. However qemu may need to compile with earlier versions, so we replicate both methods here.
--- util/iova-tree.c | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-)
util/iova-tree.c +41 -1
@@ -11,15 +11,44 @@#include "qemu/osdep.h" #include "qemu/iova-tree.h" +#include "qemu/queue.h" typedef struct DMAMapInternal {DMAMap map; +QTAILQ_ENTRY (DMAMapInternal) entry; }DMAMapInternal ;struct IOVATree {GTree *tree; + QTAILQ_HEAD(, DMAMapInternal) list; }; +/** +* Search function for the upper bound of a given needle. +* +* The upper bound is the first node that has its key strictly greater than the +* searched key. +* +* TODO: A specialized function is available in GTree since Glib 2.68. Replace +* when Glib minimal version is raised. +*/ +static int iova_tree_compare_upper_bound (gconstpointer a,gconstpointer b) +{ +const DMAMapInternal *haystack = a, *needle = b, *prev; + +if (needle->map.iova >= haystack->map.iova) { +return 1 ; + } + + prev =QTAILQ_PREV (haystack, entry); +if (!prev || prev->map.iova < needle->map.iova) { +return 0 ; + } + +/* More data to the left or end of data */ +return -1 ; +} +static int iova_tree_compare (gconstpointer a,gconstpointer b,gpointer data) {const DMAMapInternal *i1 = a, *i2 = b;
@@ -43,6 +72,7 @@ IOVATree *iova_tree_new(void)/* We don't have values actually, no need to free */ iova_tree->tree =g_tree_new_full (iova_tree_compare,NULL , g_free,NULL ); +QTAILQ_INIT (&iova_tree->list);return iova_tree; }
@@ -77,7 +107,7 @@ static inline void iova_tree_insert_internal(GTree *gtree,int iova_tree_insert (IOVATree *tree,const DMAMap *map) { -DMAMapInternal *new; +DMAMapInternal *new, *right;if (map->iova + map->size < map->iova || map->perm ==IOMMU_NONE ) {return IOVA_ERR_INVALID ;
@@ -92,6 +122,15 @@ int iova_tree_insert(IOVATree *tree, const DMAMap *map)memcpy (&new->map, map,sizeof (new->map));iova_tree_insert_internal (tree->tree, new); +/* Ordered insertion */ + right =g_tree_search (tree->tree, iova_tree_compare_upper_bound, new); +if (!right) { +/* Empty or bigger than any other entry */ +QTAILQ_INSERT_TAIL (&tree->list, new, entry); + }else { +QTAILQ_INSERT_BEFORE (right, new, entry); + } +return IOVA_OK ; }
@@ -116,6 +155,7 @@ int iova_tree_remove(IOVATree *tree, const DMAMap *map)DMAMapInternal *overlap_internal;while ((overlap_internal =iova_tree_find_internal (tree, map))) { +QTAILQ_REMOVE (&tree->list, overlap_internal, entry);g_tree_remove (tree->tree, overlap_internal); }
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 31/31 vdpa: Add x-svq to NetdevVhostVDPAOptions
In reply to Eugenio PérezFinally offering the possibility to enable SVQ from the command line.
--- qapi/net.json | 5 ++++- net/vhost-vdpa.c | 27 ++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-)
qapi/net.json +4 -1
@@ -445,12 +445,15 @@ # @queues: number of queues to be created for multiqueue vhost-vdpa # (default: 1) # +# @x-svq: Start device with (experimental) shadow virtqueue. (Since 7.0) +# # Since: 5.1 ## { 'struct': 'NetdevVhostVDPAOptions', 'data': { '*vhostdev': 'str', - '*queues': 'int' } } + '*queues': 'int', + '*x-svq': 'bool' } } ## # @NetClientDriver:
net/vhost-vdpa.c +24 -3
@@ -128,7 +128,11 @@ err_init:static void vhost_vdpa_cleanup (NetClientState *nc) {VhostVDPAState *s =DO_UPCAST (VhostVDPAState, nc, nc); +struct vhost_dev *dev = s->vhost_vdpa.dev; +if (dev && dev->vq_index + dev->nvqs == dev->vq_index_end) { +g_clear_pointer (&s->vhost_vdpa.iova_tree, vhost_iova_tree_delete); + }if (s->vhost_net) {vhost_net_cleanup (s->vhost_net);g_free (s->vhost_net);
@@ -206,7 +210,9 @@ static NetClientState *net_vhost_vdpa_init(NetClientState *peer,int queue_pair_index, int nvqs, bool is_datapath, - struct vhost_vdpa_iova_range iova_range) + bool svq, + struct vhost_vdpa_iova_range iova_range, + VhostIOVATree *iova_tree) { NetClientState *nc = NULL;VhostVDPAState *s;
@@ -225,6 +231,8 @@ static NetClientState *net_vhost_vdpa_init(NetClientState *peer, s->vhost_vdpa.device_fd = vdpa_device_fd; s->vhost_vdpa.index = queue_pair_index; s->vhost_vdpa.iova_range = iova_range; + s->vhost_vdpa.shadow_vqs_enabled = svq; + s->vhost_vdpa.iova_tree = iova_tree; ret =vhost_vdpa_add (nc, (void *)&s->vhost_vdpa, queue_pair_index, nvqs);if (ret) {qemu_del_net_client (nc);
@@ -281,6 +289,7 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name, g_autofree NetClientState **ncs = NULL;NetClientState *nc;int queue_pairs, i, has_cvq =0 ; +g_autoptr (VhostIOVATree) iova_tree =NULL ;struct vhost_vdpa_iova_range iova_range;assert (netdev->type ==NET_CLIENT_DRIVER_VHOST_VDPA );
@@ -302,29 +311,41 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,return queue_pairs; }vhost_vdpa_get_iova_range (vdpa_device_fd, &iova_range); +if (opts->x_svq) { +if (has_cvq) { +error_setg (errp,"vdpa svq does not work with cvq" ); + goto err_svq; + } + iova_tree =vhost_iova_tree_new (iova_range.first, iova_range.last); + } ncs =g_malloc0 (sizeof (*ncs) * queue_pairs);for (i =0 ; i < queue_pairs; i++) { ncs[i] =net_vhost_vdpa_init (peer,TYPE_VHOST_VDPA , name, - vdpa_device_fd, i,2 , true, iova_range); + vdpa_device_fd, i,2 , true, opts->x_svq, + iova_range, iova_tree);if (!ncs[i]) goto err; }if (has_cvq) { nc =net_vhost_vdpa_init (peer,TYPE_VHOST_VDPA , name, - vdpa_device_fd, i,1 , false, iova_range); + vdpa_device_fd, i,1 , false, opts->x_svq, + iova_range, iova_tree);if (!nc) goto err; } + iova_tree =NULL ;return 0 ; err:if (i) {qemu_del_net_client (ncs[0 ]); } + +err_svq:qemu_close (vdpa_device_fd);return -1 ;
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 15/31 vdpa: Add vhost_svq_get_num
In reply to Eugenio PérezThis reports the guest's visible SVQ effective length, not the device's one.
--- hw/virtio/vhost-shadow-virtqueue.h | 1 + hw/virtio/vhost-shadow-virtqueue.c | 5 +++++ 2 files changed, 6 insertions(+)
hw/virtio/vhost-shadow-virtqueue.h +1 -0
@@ -29,6 +29,7 @@ const EventNotifier *vhost_svq_get_svq_call_notifier( const VhostShadowVirtqueue *svq);void vhost_svq_get_vring_addr (const VhostShadowVirtqueue *svq,struct vhost_vring_addr *addr); +uint16_t vhost_svq_get_num (const VhostShadowVirtqueue *svq);size_t vhost_svq_driver_area_size (const VhostShadowVirtqueue *svq);size_t vhost_svq_device_area_size (const VhostShadowVirtqueue *svq);
hw/virtio/vhost-shadow-virtqueue.c +5 -0
@@ -212,6 +212,11 @@ void vhost_svq_get_vring_addr(const VhostShadowVirtqueue *svq, addr->used_user_addr = (uint64_t )svq->vring.used; } +uint16_t vhost_svq_get_num (const VhostShadowVirtqueue *svq) +{ +return svq->vring.num; +} +size_t vhost_svq_driver_area_size (const VhostShadowVirtqueue *svq) {size_t desc_size =sizeof (vring_desc_t) * svq->vring.num;
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 24/31 vhost: Add vhost_svq_get_last_used_idx
In reply to Eugenio PérezThis way SVQ queues can be migrated.
--- hw/virtio/vhost-shadow-virtqueue.h | 1 + hw/virtio/vhost-shadow-virtqueue.c | 8 ++++++++ 2 files changed, 9 insertions(+)
hw/virtio/vhost-shadow-virtqueue.h +1 -0
@@ -30,6 +30,7 @@ const EventNotifier *vhost_svq_get_svq_call_notifier( const VhostShadowVirtqueue *svq);void vhost_svq_get_vring_addr (const VhostShadowVirtqueue *svq,struct vhost_vring_addr *addr); +uint16_t vhost_svq_get_last_used_idx (const VhostShadowVirtqueue *svq);uint16_t vhost_svq_get_num (const VhostShadowVirtqueue *svq);size_t vhost_svq_driver_area_size (const VhostShadowVirtqueue *svq);size_t vhost_svq_device_area_size (const VhostShadowVirtqueue *svq);
hw/virtio/vhost-shadow-virtqueue.c +8 -0
@@ -574,6 +574,14 @@ void vhost_svq_get_vring_addr(const VhostShadowVirtqueue *svq, addr->used_user_addr = (uint64_t )svq->vring.used; } +/** +* Get the next index that SVQ is going to consume from SVQ used ring. +*/ +uint16_t vhost_svq_get_last_used_idx (const VhostShadowVirtqueue *svq) +{ +return svq->last_used_idx; +} +uint16_t vhost_svq_get_num (const VhostShadowVirtqueue *svq) {return svq->vring.num;
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 21/31 util: Add iova_tree_alloc
In reply to Eugenio PérezThis iova tree function allows it to look for a hole in allocated regions and return a totally new translation for a given translated address.
It's usage is mainly to allow devices to access qemu address space, remapping guest's one into a new iova space where qemu can add chunks of addresses.
--- include/qemu/iova-tree.h | 17 ++++++++ util/iova-tree.c | 86 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 102 insertions(+), 1 deletion(-)
include/qemu/iova-tree.h +17 -0
@@ -29,6 +29,7 @@#define IOVA_OK (0)#define IOVA_ERR_INVALID (-1)/* Invalid parameters */ #define IOVA_ERR_OVERLAP (-2)/* IOVA range overlapped */ +#define IOVA_ERR_NOMEM (-3)/* Cannot allocate */ typedef struct IOVATree IOVATree ; typedef struct DMAMap {
@@ -119,6 +120,22 @@ const DMAMap *iova_tree_find_address(const IOVATree *tree, hwaddr iova);*/ void iova_tree_foreach (IOVATree *tree,iova_tree_iterator iterator); +/** +* iova_tree_alloc: +* +* @tree: the iova tree to allocate from +* @map: the new map (as translated addr & size) to allocate in iova region +* @iova_begin: the minimum address of the allocation +* @iova_end: the maximum addressable direction of the allocation +* +* Allocates a new region of a given size, between iova_min and iova_max. +* +* Return: Same as iova_tree_insert, but cannot overlap and can be out of +* free contiguous range. Caller can get the assigned iova in map->iova. +*/ +int iova_tree_alloc (IOVATree *tree,DMAMap *map,hwaddr iova_begin, +hwaddr iova_end); +/** * iova_tree_destroy: *
util/iova-tree.c +85 -1
98 lines not on this page · raw message
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 28/31 vdpa: Expose VHOST_F_LOG_ALL on SVQ
In reply to Eugenio PérezSVQ is able to log the dirty bits by itself, so let's use it to not block migration.
Also, ignore set and clear of VHOST_F_LOG_ALL on set_features if SVQ is enabled. Even if the device supports it, the reports would be nonsense because SVQ memory is in the qemu region.
The log region is still allocated. Future changes might skip that, but this series is already long enough.
--- hw/virtio/vhost-vdpa.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
hw/virtio/vhost-vdpa.c +20 -0
@@ -1022,6 +1022,9 @@ static int vhost_vdpa_get_features(struct vhost_dev *dev, uint64_t *features)if (ret ==0 && v->shadow_vqs_enabled) {/* Filter only features that SVQ can offer to guest */ vhost_svq_valid_guest_features (features); + +/* Add SVQ logging capabilities */ + *features |=BIT_ULL (VHOST_F_LOG_ALL ); }return ret;
@@ -1039,8 +1042,25 @@ static int vhost_vdpa_set_features(struct vhost_dev *dev,if (v->shadow_vqs_enabled) {uint64_t dev_features, svq_features, acked_features; +uint8_t status =0 ;bool ok; + ret =vhost_vdpa_call (dev,VHOST_VDPA_GET_STATUS , &status); +if (unlikely (ret)) { +return ret; + } + +if (status &VIRTIO_CONFIG_S_DRIVER_OK ) { +/* +* vhost is trying to enable or disable _F_LOG, and the device +* would report wrong dirty pages. SVQ handles it. +*/ +return 0 ; + } + +/* We must not ack _F_LOG if SVQ is enabled */ + features &= ~BIT_ULL (VHOST_F_LOG_ALL ); + ret =vhost_vdpa_get_dev_features (dev, &dev_features);if (ret !=0 ) {error_report ("Can't get vdpa device features, got (%d)" , ret);
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 25/31 vdpa: Adapt vhost_vdpa_get_vring_base to SVQ
In reply to Eugenio PérezThis is needed to achieve migration, so destination can restore its index.
--- hw/virtio/vhost-vdpa.c | 9 +++++++++ 1 file changed, 9 insertions(+)
hw/virtio/vhost-vdpa.c +9 -0
@@ -716,8 +716,17 @@ static int vhost_vdpa_set_vring_base(struct vhost_dev *dev,static int vhost_vdpa_get_vring_base (struct vhost_dev *dev,struct vhost_vring_state *ring) { +struct vhost_vdpa *v = dev->opaque;int ret; +if (v->shadow_vqs_enabled) { +VhostShadowVirtqueue *svq =g_ptr_array_index (v->shadow_vqs, + ring->index); + + ring->num =vhost_svq_get_last_used_idx (svq); +return 0 ; + } + ret =vhost_vdpa_call (dev,VHOST_GET_VRING_BASE , ring);trace_vhost_vdpa_get_vring_base (dev, ring->index, ring->num);return ret;
Signature · 1 line
2.27.0
Eugenio Pérez
maintainerPATCH 29/31 vdpa: Make ncs autofree
In reply to Eugenio PérezSimplifying memory management.
--- net/vhost-vdpa.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
net/vhost-vdpa.c +2 -3
@@ -264,7 +264,8 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name, {const NetdevVhostVDPAOptions *opts;int vdpa_device_fd; -NetClientState **ncs, *nc; + g_autofree NetClientState **ncs = NULL; +NetClientState *nc;int queue_pairs, i, has_cvq =0 ;assert (netdev->type ==NET_CLIENT_DRIVER_VHOST_VDPA );
@@ -302,7 +303,6 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name, goto err; } -g_free (ncs);return 0 ; err:
@@ -310,7 +310,6 @@ err:qemu_del_net_client (ncs[0 ]); }qemu_close (vdpa_device_fd); -g_free (ncs);return -1 ; }
Signature · 1 line
2.27.0
Re: [PATCH 21/31] util: Add iova_tree_alloc
In reply to Eugenio PérezQuoted 27 lines · Eugenio Pérez
util/iova-tree.c +27
-0+int iova_tree_alloc (IOVATree *tree,DMAMap *map,hwaddr iova_begin, +hwaddr iova_last) +{ +const DMAMapInternal *last, *i; + +assert (iova_begin < iova_last); + +/* +* Find a valid hole for the mapping +* +* TODO: Replace all this with g_tree_node_first/next/last when available +* (from glib since 2.68). Using a sepparated QTAILQ complicates code. +* +* Try to allocate first at the end of the list. +*/ + last =QTAILQ_LAST (&tree->list); +if (iova_tree_alloc_map_in_hole (last,NULL , iova_begin, iova_last, + map->size)) { + goto alloc; + } + +/* Look for inner hole */ + last =NULL ; +for (i =QTAILQ_FIRST (&tree->list); i; + last = i, i =QTAILQ_NEXT (i, entry)) { +if (iova_tree_alloc_map_in_hole (last, i, iova_begin, iova_last, + map->size)) {
util/iova-tree.c int iova_tree_remove(IOVATree *tree, const DMAMap *map) · line 247
Quoted 10 lines
util/iova-tree.c +10
-0+ goto alloc; + } + } + +return IOVA_ERR_NOMEM ; + +alloc: + map->iova = last ? last->map.iova + last->map.size +1 : iova_begin; +return iova_tree_insert (tree, map); +}
Hi, Eugenio,
Have you tried with what Jason suggested previously?
That solution still sounds very sensible to me even without the newly introduced list in previous two patches.
IMHO we could move "DMAMap *previous, *this" into the IOVATreeAllocArgs* stucture that was passed into the traverse func though, so it'll naturally work with threading.
Or is there any blocker for it?
Thanks,
Signature · 6 lines
Peter Xu _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Eugenio Perez Martin
maintainerRe: [PATCH 21/31] util: Add iova_tree_alloc
Quoted 3 lines · Peter Xu
Quoted 1 line · Eugenio Pérez
+int iova_tree_alloc(IOVATree *tree, DMAMap *map, hwaddr
iova_begin,
I forgot to s/iova_tree_alloc/iova_tree_alloc_map/ here.
Quoted 43 lines
Quoted 26 lines
util/iova-tree.c +26
-0+ hwaddr iova_last) +{ +const DMAMapInternal *last, *i; + +assert (iova_begin < iova_last); + +/* +* Find a valid hole for the mapping +* +* TODO: Replace all this with g_tree_node_first/next/last when available +* (from glib since 2.68). Using a sepparated QTAILQ complicates code. +* +* Try to allocate first at the end of the list. +*/ + last =QTAILQ_LAST (&tree->list); +if (iova_tree_alloc_map_in_hole (last,NULL , iova_begin, iova_last, + map->size)) { + goto alloc; + } + +/* Look for inner hole */ + last =NULL ; +for (i =QTAILQ_FIRST (&tree->list); i; + last = i, i =QTAILQ_NEXT (i, entry)) { +if (iova_tree_alloc_map_in_hole (last, i, iova_begin, iova_last, + map->size)) {util/iova-tree.c int iova_tree_remove(IOVATree *tree, const DMAMap *map) · line 247
Quoted 10 lines
util/iova-tree.c +10
-0+ goto alloc; + } + } + +return IOVA_ERR_NOMEM ; + +alloc: + map->iova = last ? last->map.iova + last->map.size +1 : iova_begin; +return iova_tree_insert (tree, map); +}Hi, Eugenio,
Have you tried with what Jason suggested previously?
Quoted 9 lines
That solution still sounds very sensible to me even without the newly introduced list in previous two patches.
IMHO we could move "DMAMap *previous, *this" into the IOVATreeAllocArgs* stucture that was passed into the traverse func though, so it'll naturally work with threading.
Or is there any blocker for it?
Hi Peter,
I can try that solution again, but the main problem was the special cases of the beginning and ending.
For the function to locate a hole, DMAMap first = {.iova = 0, .size = 0} means that it cannot account 0 for the hole.
In other words, with that algorithm, if the only valid hole is [0, N) and we try to allocate a block of size N, it would fail.
Same happens with iova_end, although in practice it seems that IOMMU hardware iova upper limit is never UINT64_MAX.
Maybe we could treat .size = 0 as a special case? I see cleaner either to build the list (but insert needs to take the list into account) or to explicitly tell that prev == NULL means to use iova_first.
Another solution that comes to my mind: to add both exceptions outside of transverse function, and skip the first iteration with something like:
if (prev == NULL) {
prev = this;
return false /* continue */
}
So the transverse callback has way less code paths. Would it work for you if I send a separate RFC from SVQ only to validate this?
Thanks!
Quoted 6 lines
Thanks,
--
Peter Xu
Re: [PATCH 21/31] util: Add iova_tree_alloc
Quoted 71 lines · Eugenio Perez Martin
Quoted 3 lines · Peter Xu
Quoted 1 line · Eugenio Pérez
+int iova_tree_alloc(IOVATree *tree, DMAMap *map, hwaddr
iova_begin,I forgot to s/iova_tree_alloc/iova_tree_alloc_map/ here.
Quoted 43 lines
Quoted 26 lines
util/iova-tree.c +26
-0+ hwaddr iova_last) +{ +const DMAMapInternal *last, *i; + +assert (iova_begin < iova_last); + +/* +* Find a valid hole for the mapping +* +* TODO: Replace all this with g_tree_node_first/next/last when available +* (from glib since 2.68). Using a sepparated QTAILQ complicates code. +* +* Try to allocate first at the end of the list. +*/ + last =QTAILQ_LAST (&tree->list); +if (iova_tree_alloc_map_in_hole (last,NULL , iova_begin, iova_last, + map->size)) { + goto alloc; + } + +/* Look for inner hole */ + last =NULL ; +for (i =QTAILQ_FIRST (&tree->list); i; + last = i, i =QTAILQ_NEXT (i, entry)) { +if (iova_tree_alloc_map_in_hole (last, i, iova_begin, iova_last, + map->size)) {util/iova-tree.c int iova_tree_remove(IOVATree *tree, const DMAMap *map) · line 247
Quoted 10 lines
util/iova-tree.c +10
-0+ goto alloc; + } + } + +return IOVA_ERR_NOMEM ; + +alloc: + map->iova = last ? last->map.iova + last->map.size +1 : iova_begin; +return iova_tree_insert (tree, map); +}Hi, Eugenio,
Have you tried with what Jason suggested previously?
Quoted 9 lines
That solution still sounds very sensible to me even without the newly introduced list in previous two patches.
IMHO we could move "DMAMap *previous, *this" into the IOVATreeAllocArgs* stucture that was passed into the traverse func though, so it'll naturally work with threading.
Or is there any blocker for it?
Hi Peter,
I can try that solution again, but the main problem was the special cases of the beginning and ending.
For the function to locate a hole, DMAMap first = {.iova = 0, .size = 0} means that it cannot account 0 for the hole.
In other words, with that algorithm, if the only valid hole is [0, N) and we try to allocate a block of size N, it would fail.
Quoted 6 lines
Same happens with
iova_end, although in practice it seems that IOMMU hardware iova upper limit is neverUINT64_MAX.Maybe we could treat .size = 0 as a special case? I see cleaner either to build the list (but insert needs to take the list into account) or to explicitly tell that prev == NULL means to use
iova_first.
Sounds good to me. I didn't mean to copy-paste Jason's code, but IMHO what Jason wanted to show is the general concept - IOW, the fundamental idea (to me) is that the tree will be traversed in order, hence maintaining another list structure is redundant.
Quoted 5 lines
Another solution that comes to my mind: to add both exceptions outside of transverse function, and skip the first iteration with something like:
Quoted 7 lines
if (prev == NULL) {
prev = this;
return false /* continue */
}So the transverse callback has way less code paths. Would it work for you if I send a separate RFC from SVQ only to validate this?
Sure. :-)
If you want, imho you can also attach the patch when reply, then the discussion context won't be lost too.
Signature · 6 lines
Peter Xu _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Eugenio Perez Martin
maintainerRe: [PATCH 21/31] util: Add iova_tree_alloc
Quoted 76 lines · Peter Xu
Quoted 74 lines · Eugenio Perez Martin
Quoted 3 lines · Peter Xu
Quoted 1 line · Eugenio Pérez
+int iova_tree_alloc(IOVATree *tree, DMAMap *map, hwaddr
iova_begin,I forgot to s/iova_tree_alloc/iova_tree_alloc_map/ here.
Quoted 43 lines
Quoted 26 lines
util/iova-tree.c +26
-0+ hwaddr iova_last) +{ +const DMAMapInternal *last, *i; + +assert (iova_begin < iova_last); + +/* +* Find a valid hole for the mapping +* +* TODO: Replace all this with g_tree_node_first/next/last when available +* (from glib since 2.68). Using a sepparated QTAILQ complicates code. +* +* Try to allocate first at the end of the list. +*/ + last =QTAILQ_LAST (&tree->list); +if (iova_tree_alloc_map_in_hole (last,NULL , iova_begin, iova_last, + map->size)) { + goto alloc; + } + +/* Look for inner hole */ + last =NULL ; +for (i =QTAILQ_FIRST (&tree->list); i; + last = i, i =QTAILQ_NEXT (i, entry)) { +if (iova_tree_alloc_map_in_hole (last, i, iova_begin, iova_last, + map->size)) {util/iova-tree.c int iova_tree_remove(IOVATree *tree, const DMAMap *map) · line 247
Quoted 10 lines
util/iova-tree.c +10
-0+ goto alloc; + } + } + +return IOVA_ERR_NOMEM ; + +alloc: + map->iova = last ? last->map.iova + last->map.size +1 : iova_begin; +return iova_tree_insert (tree, map); +}Hi, Eugenio,
Have you tried with what Jason suggested previously?
Quoted 9 lines
That solution still sounds very sensible to me even without the newly introduced list in previous two patches.
IMHO we could move "DMAMap *previous, *this" into the IOVATreeAllocArgs* stucture that was passed into the traverse func though, so it'll naturally work with threading.
Or is there any blocker for it?
Hi Peter,
I can try that solution again, but the main problem was the special cases of the beginning and ending.
For the function to locate a hole, DMAMap first = {.iova = 0, .size = 0} means that it cannot account 0 for the hole.
In other words, with that algorithm, if the only valid hole is [0, N) and we try to allocate a block of size N, it would fail.
Same happens with
iova_end, although in practice it seems that IOMMU hardware iova upper limit is neverUINT64_MAX.
Quoted 9 lines
Quoted 3 lines
Maybe we could treat .size = 0 as a special case? I see cleaner either to build the list (but insert needs to take the list into account) or to explicitly tell that prev == NULL means to use
iova_first.Sounds good to me. I didn't mean to copy-paste Jason's code, but IMHO what Jason wanted to show is the general concept - IOW, the fundamental idea (to me) is that the tree will be traversed in order, hence maintaining another list structure is redundant.
I agree.
My idea with this version was to easily delete all the custom code once we have GTree with proper first/next/last, or _node functions. That's why it's simply reimplementing GTree functions in the current Glib version. I find old code way too complicated, and this one easier to handle although way more verbose, but let's see if we can improve the old one.
Quoted 10 lines
Quoted 10 lines
Another solution that comes to my mind: to add both exceptions outside of transverse function, and skip the first iteration with something like:
if (prev == NULL) {
prev = this;
return false /* continue */
}
Quoted 8 lines
Quoted 2 lines
So the transverse callback has way less code paths. Would it work for you if I send a separate RFC from SVQ only to validate this?
Sure. :-)
If you want, imho you can also attach the patch when reply, then the discussion context won't be lost too.
Sure,
So I think that the first step to remove complexity from the old one is to remove iova_begin and iova_end.
As Jason points out, removing iova_end is easier. It has the drawback of having to traverse all the list beyond iova_end, but a well formed iova tree should contain none. If the guest can manipulate it, it's only hurting itself adding nodes to it.
It's possible to extract the check for hole_right (or this in Jason's proposal) as a special case too.
But removing the iova_begin parameter is more complicated. We cannot know if it's a valid hole without knowing iova_begin, and we cannot resume traversing. Could we assume iova_begin will always be 0? I think not, the vdpa device can return anything through syscall.
Thanks!
Quoted 3 lines
--
Peter Xu
Eugenio Perez Martin
maintainerRe: [PATCH 02/31] vhost: Add VhostShadowVirtqueue
In reply to Eugenio PérezQuoted 59 lines · Eugenio Pérez
Vhost shadow virtqueue (SVQ) is an intermediate jump for virtqueue notifications and buffers, allowing qemu to track them. While qemu is forwarding the buffers and virtqueue changes, it is able to commit the memory it's being dirtied, the same way regular qemu's VirtIO devices do.
This commit only exposes basic SVQ allocation and free. Next patches of the series add functionality like notifications and buffers forwarding.
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-shadow-virtqueue.h | 21 ++++++++++ hw/virtio/vhost-shadow-virtqueue.c | 64 ++++++++++++++++++++++++++++++ hw/virtio/meson.build | 2 +- 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 hw/virtio/vhost-shadow-virtqueue.h create mode 100644 hw/virtio/vhost-shadow-virtqueue.chw/virtio/vhost-shadow-virtqueue.h new file +21
-0@@ -0,0 +1,21 @@ +/* +* vhost shadow virtqueue +* +* SPDX-FileCopyrightText: Red Hat, Inc. 2021 +* SPDX-FileContributor: Author: Eugenio Pérez <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef VHOST_SHADOW_VIRTQUEUE_H +#define VHOST_SHADOW_VIRTQUEUE_H + +#include "hw/virtio/vhost.h" + +typedef struct VhostShadowVirtqueue VhostShadowVirtqueue ; + +VhostShadowVirtqueue *vhost_svq_new (void ); + +void vhost_svq_free (VhostShadowVirtqueue *vq); + +#endif hw/virtio/vhost-shadow-virtqueue.c new file +7
-0@@ -0,0 +1,64 @@ +/* +* vhost shadow virtqueue +* +* SPDX-FileCopyrightText: Red Hat, Inc. 2021 +* SPDX-FileContributor: Author: Eugenio Pérez <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later
hw/virtio/vhost-shadow-virtqueue.c line 17
Quoted 10 lines
hw/virtio/vhost-shadow-virtqueue.c new file +10
-0+*/ + +#include "qemu/osdep.h" +#include "hw/virtio/vhost-shadow-virtqueue.h" + +#include "qemu/error-report.h" +#include "qemu/event_notifier.h" + +/* Shadow virtqueue to relay notifications */ +typedef struct VhostShadowVirtqueue {
This is already typedef as VhostShadowVirtqueue in the header, so I will remove it here for the next version.
Quoted 64 lines
hw/virtio/vhost-shadow-virtqueue.c new file +47
-0+/* Shadow kick notifier, sent to vhost */ +EventNotifier hdev_kick; +/* Shadow call notifier, sent to vhost */ +EventNotifier hdev_call; +} VhostShadowVirtqueue; + +/** +* Creates vhost shadow virtqueue, and instruct vhost device to use the shadow +* methods and file descriptors. +*/ +VhostShadowVirtqueue *vhost_svq_new (void ) +{ + g_autofree VhostShadowVirtqueue *svq = g_new0(VhostShadowVirtqueue, 1); +int r; + + r =event_notifier_init (&svq->hdev_kick,0 ); +if (r !=0 ) { +error_report ("Couldn't create kick event notifier: %s" , +strerror (errno)); + goto err_init_hdev_kick; + } + + r =event_notifier_init (&svq->hdev_call,0 ); +if (r !=0 ) { +error_report ("Couldn't create call event notifier: %s" , +strerror (errno)); + goto err_init_hdev_call; + } + +return g_steal_pointer (&svq); + +err_init_hdev_call: +event_notifier_cleanup (&svq->hdev_kick); + +err_init_hdev_kick: +return NULL ; +} + +/** +* Free the resources of the shadow virtqueue. +*/ +void vhost_svq_free (VhostShadowVirtqueue *vq) +{ +event_notifier_cleanup (&vq->hdev_kick); +event_notifier_cleanup (&vq->hdev_call); +g_free (vq); +}hw/virtio/meson.build +1
-2@@ -11,7 +11,7 @@ softmmu_ss.add(when: 'CONFIG_ALL', if_true: files('vhost-stub.c')) virtio_ss = ss.source_set() virtio_ss.add(files('virtio.c')) -virtio_ss.add(when: 'CONFIG_VHOST', if_true: files('vhost.c', 'vhost-backend.c')) +virtio_ss.add(when: 'CONFIG_VHOST', if_true: files('vhost.c', 'vhost-backend.c', 'vhost-shadow-virtqueue.c')) virtio_ss.add(when: 'CONFIG_VHOST_USER', if_true: files('vhost-user.c')) virtio_ss.add(when: 'CONFIG_VHOST_VDPA', if_true: files('vhost-vdpa.c')) virtio_ss.add(when: 'CONFIG_VIRTIO_BALLOON', if_true: files('virtio-balloon.c')) --2.27.0
Re: [PATCH 21/31] util: Add iova_tree_alloc
In reply to Eugenio Perez MartinQuoted 8 lines · Eugenio Perez Martin
So I think that the first step to remove complexity from the old one is to remove
iova_beginandiova_end.As Jason points out, removing
iova_endis easier. It has the drawback of having to traverse all the list beyondiova_end, but a well formed iova tree should contain none. If the guest can manipulate it, it's only hurting itself adding nodes to it.
Quoted 7 lines
It's possible to extract the check for
hole_right(or this in Jason's proposal) as a special case too.But removing the
iova_beginparameter is more complicated. We cannot know if it's a valid hole without knowingiova_begin, and we cannot resume traversing. Could we assumeiova_beginwill always be 0? I think not, the vdpa device can return anything through syscall.
Frankly I don't know what's the syscall you're talking about, but after a 2nd thought and after I went back and re-read your previous version more carefully (the one without the list) I think it seems working to me in general. I should have tried harder when reviewing the first time!
I mean this one:
https://lore.kernel.org/qemu-devel/[email protected]/
Though this time I have some comments on the details.
Personally I like that one (probably with some amendment upon the old version) more than the current list-based approach. But I'd like to know your thoughts too (including Jason). I'll further comment in that thread soon.
Thanks,
Signature · 6 lines
Peter Xu _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Eugenio Perez Martin
maintainerRe: [PATCH 21/31] util: Add iova_tree_alloc
Quoted 13 lines · Peter Xu
Quoted 11 lines · Eugenio Perez Martin
So I think that the first step to remove complexity from the old one is to remove
iova_beginandiova_end.As Jason points out, removing
iova_endis easier. It has the drawback of having to traverse all the list beyondiova_end, but a well formed iova tree should contain none. If the guest can manipulate it, it's only hurting itself adding nodes to it.It's possible to extract the check for
hole_right(or this in Jason's proposal) as a special case too.
Quoted 6 lines
Quoted 4 lines
But removing the
iova_beginparameter is more complicated. We cannot know if it's a valid hole without knowingiova_begin, and we cannot resume traversing. Could we assumeiova_beginwill always be 0? I think not, the vdpa device can return anything through syscall.Frankly I don't know what's the syscall you're talking about,
I meant VHOST_VDPA_GET_IOVA_RANGE, which allows qemu to know the valid range of iova addresses. We get a pair of uint64_t from it, that indicates the minimum and maximum iova address the device (or iommu) supports.
We must allocate iova ranges within that address range, which complicates this algorithm a little bit. Since the SVQ iova addresses are not GPA, qemu needs extra code to be able to allocate and free them, creating a new custom iova as.
Please let me know if you want more details or if you prefer me to give more context in the patch message.
Quoted 5 lines
but after a 2nd
thought and after I went back and re-read your previous version more carefully (the one without the list) I think it seems working to me in general. I should have tried harder when reviewing the first time!
I guess I should have added more context so this particular change can be better understood in isolation.
Quoted 10 lines
I mean this one:
https://lore.kernel.org/qemu-devel/[email protected]/
Though this time I have some comments on the details.
Personally I like that one (probably with some amendment upon the old version) more than the current list-based approach. But I'd like to know your thoughts too (including Jason). I'll further comment in that thread soon.
Sure, I'm fine with whatever solution we choose, but I'm just running out of ideas to simplify it. Reading your suggestions on old RFC now.
Overall I feel list-based one is both more convenient and easy to delete when qemu raises the minimal glib version, but it adds a lot more code.
It could add less code with this less elegant changes:
* If we just put the list entry in the DMAMap itself, although it exposes unneeded implementation details.
* We force the iova tree either to be an allocation-based or an insertion-based, but not both. In other words, you can only either use iova_tree_alloc or iova_tree_insert on the same tree.
I have a few tests to check the algorithms, but they are not in the qemu test format. I will post them so we all can understand better what is expected from this.
Thanks!
Thanks!
Quoted 5 lines
Thanks,
--
Peter Xu
Re: [PATCH 21/31] util: Add iova_tree_alloc
Quoted 26 lines · Eugenio Perez Martin
Quoted 13 lines · Peter Xu
Quoted 11 lines · Eugenio Perez Martin
So I think that the first step to remove complexity from the old one is to remove
iova_beginandiova_end.As Jason points out, removing
iova_endis easier. It has the drawback of having to traverse all the list beyondiova_end, but a well formed iova tree should contain none. If the guest can manipulate it, it's only hurting itself adding nodes to it.It's possible to extract the check for
hole_right(or this in Jason's proposal) as a special case too.Quoted 6 lines
Quoted 4 lines
But removing the
iova_beginparameter is more complicated. We cannot know if it's a valid hole without knowingiova_begin, and we cannot resume traversing. Could we assumeiova_beginwill always be 0? I think not, the vdpa device can return anything through syscall.Frankly I don't know what's the syscall you're talking about,
I meant
VHOST_VDPA_GET_IOVA_RANGE, which allows qemu to know the valid range of iova addresses. We get a pair ofuint64_tfrom it, that indicates the minimum and maximum iova address the device (or iommu) supports.
Quoted 7 lines
We must allocate iova ranges within that address range, which complicates this algorithm a little bit. Since the SVQ iova addresses are not GPA, qemu needs extra code to be able to allocate and free them, creating a new custom iova as.
Please let me know if you want more details or if you prefer me to give more context in the patch message.
That's good enough, thanks.
Quoted 19 lines
Quoted 11 lines
I mean this one:
https://lore.kernel.org/qemu-devel/[email protected]/
Though this time I have some comments on the details.
Personally I like that one (probably with some amendment upon the old version) more than the current list-based approach. But I'd like to know your thoughts too (including Jason). I'll further comment in that thread soon.
Sure, I'm fine with whatever solution we choose, but I'm just running out of ideas to simplify it. Reading your suggestions on old RFC now.
Overall I feel list-based one is both more convenient and easy to delete when qemu raises the minimal glib version, but it adds a lot more code.
Quoted 6 lines
It could add less code with this less elegant changes:
* If we just put the list entry in the DMAMap itself, although it exposes unneeded implementation details.
* We force the iova tree either to be an allocation-based or an insertion-based, but not both. In other words, you can only either useiova_tree_allocoriova_tree_inserton the same tree.
Yeah, I just noticed it yesterday that there's no easy choice on it. Let's go with either way; it shouldn't block the rest of the code. It'll be good if Jason or Michael share their preferences too.
Quoted 4 lines
I have a few tests to check the algorithms, but they are not in the qemu test format. I will post them so we all can understand better what is expected from this.
Sure. Thanks.
Signature · 6 lines
Peter Xu _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH 21/31] util: Add iova_tree_alloc
Quoted 24 lines · Peter Xu
Quoted 23 lines · Eugenio Perez Martin
Quoted 9 lines · Peter Xu
Quoted 8 lines · Eugenio Perez Martin
So I think that the first step to remove complexity from the old one is to remove
iova_beginandiova_end.As Jason points out, removing
iova_endis easier. It has the drawback of having to traverse all the list beyondiova_end, but a well formed iova tree should contain none. If the guest can manipulate it, it's only hurting itself adding nodes to it.Quoted 8 lines
Quoted 7 lines
It's possible to extract the check for
hole_right(or this in Jason's proposal) as a special case too.But removing the
iova_beginparameter is more complicated. We cannot know if it's a valid hole without knowingiova_begin, and we cannot resume traversing. Could we assumeiova_beginwill always be 0? I think not, the vdpa device can return anything through syscall.Frankly I don't know what's the syscall you're talking about,
I meant
VHOST_VDPA_GET_IOVA_RANGE, which allows qemu to know the valid range of iova addresses. We get a pair ofuint64_tfrom it, that indicates the minimum and maximum iova address the device (or iommu) supports.
Quoted 32 lines
Quoted 7 lines
We must allocate iova ranges within that address range, which complicates this algorithm a little bit. Since the SVQ iova addresses are not GPA, qemu needs extra code to be able to allocate and free them, creating a new custom iova as.
Please let me know if you want more details or if you prefer me to give more context in the patch message.
That's good enough, thanks.
Quoted 23 lines
Quoted 10 lines
I mean this one:
https://lore.kernel.org/qemu-devel/[email protected]/
Though this time I have some comments on the details.
Personally I like that one (probably with some amendment upon the old version) more than the current list-based approach. But I'd like to know your thoughts too (including Jason). I'll further comment in that thread soon.
Sure, I'm fine with whatever solution we choose, but I'm just running out of ideas to simplify it. Reading your suggestions on old RFC now.
Overall I feel list-based one is both more convenient and easy to delete when qemu raises the minimal glib version, but it adds a lot more code.
It could add less code with this less elegant changes:
* If we just put the list entry in the DMAMap itself, although it exposes unneeded implementation details.
* We force the iova tree either to be an allocation-based or an insertion-based, but not both. In other words, you can only either useiova_tree_allocoriova_tree_inserton the same tree.
This seems an odd API I must say :(
Quoted 3 lines
Yeah, I just noticed it yesterday that there's no easy choice on it. Let's go with either way; it shouldn't block the rest of the code. It'll be good if Jason or Michael share their preferences too.
(Havne't gone through the code deeply)
I wonder how about just copy-paste gtree_node_first|last()? A quick google told me it's not complicated.
Thanks
Quoted 6 lines
Quoted 4 lines
I have a few tests to check the algorithms, but they are not in the qemu test format. I will post them so we all can understand better what is expected from this.
Sure. Thanks.
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH 01/31] vdpa: Reorder virtio/vhost-vdpa.c functions
In reply to Eugenio PérezQuoted 8 lines · Eugenio Pérez
vhost_vdpa_set_featuresandvhost_vdpa_initneed to usevhost_vdpa_get_featuresin svq mode.
vhost_vdpa_dev_startneeds to use almost all _set_ functions:vhost_vdpa_set_vring_dev_kick,vhost_vdpa_set_vring_dev_call,vhost_vdpa_set_dev_vring_baseandvhost_vdpa_set_dev_vring_num.No functional change intended.
Is it related (a must) to the SVQ code?
Thanks
Quoted 206 lines
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-vdpa.c | 164 ++++++++++++++++++++--------------------- 1 file changed, 82 insertions(+), 82 deletions(-)hw/virtio/vhost-vdpa.c +75
-82@@ -342,41 +342,6 @@ static bool vhost_vdpa_one_time_request(struct vhost_dev *dev)return v->index !=0 ; } -static int vhost_vdpa_init (struct vhost_dev *dev,void *opaque,Error **errp) -{ -struct vhost_vdpa *v; -assert (dev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_VDPA ); -trace_vhost_vdpa_init (dev, opaque); -int ret; - -/* -* Similar to VFIO, we end up pinning all guest memory and have to -* disable discarding of RAM. -*/ - ret =ram_block_discard_disable (true); -if (ret) { -error_report ("Cannot set discarding of RAM broken" ); -return ret; - } - - v = opaque; - v->dev = dev; - dev->opaque = opaque ; - v->listener = vhost_vdpa_memory_listener; - v->msg_type =VHOST_IOTLB_MSG_V2 ; - -vhost_vdpa_get_iova_range (v); - -if (vhost_vdpa_one_time_request (dev)) { -return 0 ; - } - -vhost_vdpa_add_status (dev,VIRTIO_CONFIG_S_ACKNOWLEDGE | -VIRTIO_CONFIG_S_DRIVER ); - -return 0 ; -} -static void vhost_vdpa_host_notifier_uninit (struct vhost_dev *dev,int queue_index) {@@ -506,24 +471,6 @@ static int vhost_vdpa_set_mem_table(struct vhost_dev *dev,return 0 ; } -static int vhost_vdpa_set_features (struct vhost_dev *dev, -uint64_t features) -{ -int ret; - -if (vhost_vdpa_one_time_request (dev)) { -return 0 ; - } - -trace_vhost_vdpa_set_features (dev, features); - ret =vhost_vdpa_call (dev,VHOST_SET_FEATURES , &features); -if (ret) { -return ret; - } - -return vhost_vdpa_add_status (dev,VIRTIO_CONFIG_S_FEATURES_OK ); -} -static int vhost_vdpa_set_backend_cap (struct vhost_dev *dev) {uint64_t features;@@ -646,35 +593,6 @@ static int vhost_vdpa_get_config(struct vhost_dev *dev, uint8_t *config,return ret; } -static int vhost_vdpa_dev_start (struct vhost_dev *dev,bool started) -{ -struct vhost_vdpa *v = dev->opaque; -trace_vhost_vdpa_dev_start (dev, started); - -if (started) { -vhost_vdpa_host_notifiers_init (dev); -vhost_vdpa_set_vring_ready (dev); - }else { -vhost_vdpa_host_notifiers_uninit (dev, dev->nvqs); - } - -if (dev->vq_index + dev->nvqs != dev->vq_index_end) { -return 0 ; - } - -if (started) { -memory_listener_register (&v->listener, &address_space_memory); -return vhost_vdpa_add_status (dev,VIRTIO_CONFIG_S_DRIVER_OK ); - }else { -vhost_vdpa_reset_device (dev); -vhost_vdpa_add_status (dev,VIRTIO_CONFIG_S_ACKNOWLEDGE | -VIRTIO_CONFIG_S_DRIVER ); -memory_listener_unregister (&v->listener); - -return 0 ; - } -} -static int vhost_vdpa_set_log_base (struct vhost_dev *dev,uint64_t base,struct vhost_log *log) {@@ -735,6 +653,35 @@ static int vhost_vdpa_set_vring_call(struct vhost_dev *dev,return vhost_vdpa_call (dev,VHOST_SET_VRING_CALL , file); } +static int vhost_vdpa_dev_start (struct vhost_dev *dev,bool started) +{ +struct vhost_vdpa *v = dev->opaque; +trace_vhost_vdpa_dev_start (dev, started); + +if (started) { +vhost_vdpa_host_notifiers_init (dev); +vhost_vdpa_set_vring_ready (dev); + }else { +vhost_vdpa_host_notifiers_uninit (dev, dev->nvqs); + } + +if (dev->vq_index + dev->nvqs != dev->vq_index_end) { +return 0 ; + } + +if (started) { +memory_listener_register (&v->listener, &address_space_memory); +return vhost_vdpa_add_status (dev,VIRTIO_CONFIG_S_DRIVER_OK ); + }else { +vhost_vdpa_reset_device (dev); +vhost_vdpa_add_status (dev,VIRTIO_CONFIG_S_ACKNOWLEDGE | +VIRTIO_CONFIG_S_DRIVER ); +memory_listener_unregister (&v->listener); + +return 0 ; + } +} +static int vhost_vdpa_get_features (struct vhost_dev *dev,uint64_t *features) {@@ -745,6 +692,24 @@ static int vhost_vdpa_get_features(struct vhost_dev *dev,return ret; } +static int vhost_vdpa_set_features (struct vhost_dev *dev, +uint64_t features) +{ +int ret; + +if (vhost_vdpa_one_time_request (dev)) { +return 0 ; + } + +trace_vhost_vdpa_set_features (dev, features); + ret =vhost_vdpa_call (dev,VHOST_SET_FEATURES , &features); +if (ret) { +return ret; + } + +return vhost_vdpa_add_status (dev,VIRTIO_CONFIG_S_FEATURES_OK ); +} +static int vhost_vdpa_set_owner (struct vhost_dev *dev) {if (vhost_vdpa_one_time_request (dev)) {@@ -772,6 +737,41 @@ static bool vhost_vdpa_force_iommu(struct vhost_dev *dev)return true; } +static int vhost_vdpa_init (struct vhost_dev *dev,void *opaque,Error **errp) +{ +struct vhost_vdpa *v; +assert (dev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_VDPA ); +trace_vhost_vdpa_init (dev, opaque); +int ret; + +/* +* Similar to VFIO, we end up pinning all guest memory and have to +* disable discarding of RAM. +*/ + ret =ram_block_discard_disable (true); +if (ret) { +error_report ("Cannot set discarding of RAM broken" ); +return ret; + } + + v = opaque; + v->dev = dev; + dev->opaque = opaque ; + v->listener = vhost_vdpa_memory_listener; + v->msg_type =VHOST_IOTLB_MSG_V2 ; + +vhost_vdpa_get_iova_range (v); + +if (vhost_vdpa_one_time_request (dev)) { +return 0 ; + }
hw/virtio/vhost-vdpa.c static bool vhost_vdpa_force_iommu(struct vhost_dev *dev) · line 777
Quoted 10 lines
hw/virtio/vhost-vdpa.c +7
-0+ +vhost_vdpa_add_status (dev,VIRTIO_CONFIG_S_ACKNOWLEDGE | +VIRTIO_CONFIG_S_DRIVER ); + +return 0 ; +} +const VhostOps vdpa_ops = { .backend_type =VHOST_BACKEND_TYPE_VDPA , .vhost_backend_init = vhost_vdpa_init,
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH 02/31] vhost: Add VhostShadowVirtqueue
In reply to Eugenio PérezQuoted 120 lines · Eugenio Pérez
Vhost shadow virtqueue (SVQ) is an intermediate jump for virtqueue notifications and buffers, allowing qemu to track them. While qemu is forwarding the buffers and virtqueue changes, it is able to commit the memory it's being dirtied, the same way regular qemu's VirtIO devices do.
This commit only exposes basic SVQ allocation and free. Next patches of the series add functionality like notifications and buffers forwarding.
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-shadow-virtqueue.h | 21 ++++++++++ hw/virtio/vhost-shadow-virtqueue.c | 64 ++++++++++++++++++++++++++++++ hw/virtio/meson.build | 2 +- 3 files changed, 86 insertions(+), 1 deletion(-)create mode 100644
hw/virtio/vhost-shadow-virtqueue.hcreate mode 100644hw/virtio/vhost-shadow-virtqueue.chw/virtio/vhost-shadow-virtqueue.h new file +21
-0@@ -0,0 +1,21 @@ +/* +* vhost shadow virtqueue +* +* SPDX-FileCopyrightText: Red Hat, Inc. 2021 +* SPDX-FileContributor: Author: Eugenio Pérez <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef VHOST_SHADOW_VIRTQUEUE_H +#define VHOST_SHADOW_VIRTQUEUE_H + +#include "hw/virtio/vhost.h" + +typedef struct VhostShadowVirtqueue VhostShadowVirtqueue ; + +VhostShadowVirtqueue *vhost_svq_new (void ); + +void vhost_svq_free (VhostShadowVirtqueue *vq); + +#endif hw/virtio/vhost-shadow-virtqueue.c new file +64
-0@@ -0,0 +1,64 @@ +/* +* vhost shadow virtqueue +* +* SPDX-FileCopyrightText: Red Hat, Inc. 2021 +* SPDX-FileContributor: Author: Eugenio Pérez <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "qemu/osdep.h" +#include "hw/virtio/vhost-shadow-virtqueue.h" + +#include "qemu/error-report.h" +#include "qemu/event_notifier.h" + +/* Shadow virtqueue to relay notifications */ +typedef struct VhostShadowVirtqueue { +/* Shadow kick notifier, sent to vhost */ +EventNotifier hdev_kick; +/* Shadow call notifier, sent to vhost */ +EventNotifier hdev_call; +}VhostShadowVirtqueue ; + +/** +* Creates vhost shadow virtqueue, and instruct vhost device to use the shadow +* methods and file descriptors. +*/ +VhostShadowVirtqueue *vhost_svq_new (void ) +{ + g_autofree VhostShadowVirtqueue *svq = g_new0(VhostShadowVirtqueue, 1); +int r; + + r =event_notifier_init (&svq->hdev_kick,0 ); +if (r !=0 ) { +error_report ("Couldn't create kick event notifier: %s" , +strerror (errno)); + goto err_init_hdev_kick; + } + + r =event_notifier_init (&svq->hdev_call,0 ); +if (r !=0 ) { +error_report ("Couldn't create call event notifier: %s" , +strerror (errno)); + goto err_init_hdev_call; + } + +return g_steal_pointer (&svq); + +err_init_hdev_call: +event_notifier_cleanup (&svq->hdev_kick); + +err_init_hdev_kick: +return NULL ; +} + +/** +* Free the resources of the shadow virtqueue. +*/ +void vhost_svq_free (VhostShadowVirtqueue *vq) +{ +event_notifier_cleanup (&vq->hdev_kick); +event_notifier_cleanup (&vq->hdev_call); +g_free (vq); +}hw/virtio/meson.build +0
-0
hw/virtio/meson.build softmmu_ss.add(when: 'CONFIG_ALL', if_true: files('vhost-stub.c')) · line 14
Quoted 5 lines
hw/virtio/meson.build +1
-1@@ -11,7 +11,7 @@ softmmu_ss.add(when: 'CONFIG_ALL', if_true: files('vhost-stub.c')) virtio_ss = ss.source_set() virtio_ss.add(files('virtio.c')) -virtio_ss.add(when: 'CONFIG_VHOST', if_true: files('vhost.c', 'vhost-backend.c')) +virtio_ss.add(when: 'CONFIG_VHOST', if_true: files('vhost.c', 'vhost-backend.c', 'vhost-shadow-virtqueue.c'))
I wonder if we need a dedicated config option for shadow virtqueue.
Thanks
hw/virtio/meson.build softmmu_ss.add(when: 'CONFIG_ALL', if_true: files('vhost-stub.c')) · line 17
Quoted 3 lines
hw/virtio/meson.build +0
-0virtio_ss.add(when: 'CONFIG_VHOST_USER', if_true: files('vhost-user.c')) virtio_ss.add(when: 'CONFIG_VHOST_VDPA', if_true: files('vhost-vdpa.c')) virtio_ss.add(when: 'CONFIG_VIRTIO_BALLOON', if_true: files('virtio-balloon.c'))
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 46 lines · Eugenio Pérez
This series enables shadow virtqueue (SVQ) for vhost-vdpa devices. This is intended as a new method of tracking the memory the devices touch during a migration process: Instead of relay on vhost device's dirty logging capability, SVQ intercepts the VQ dataplane forwarding the descriptors between VM and device. This way qemu is the effective writer of guests memory, like in qemu's emulated virtio device operation.
When SVQ is enabled qemu offers a new virtual address space to the device to read and write into, and it maps new vrings and the guest memory in it. SVQ also intercepts kicks and calls between the device and the guest. Used buffers relay would cause dirty memory being tracked, but at this RFC SVQ is not enabled on migration automatically.
Thanks of being a buffers relay system, SVQ can be used also to communicate devices and drivers with different capabilities, like devices that only support packed vring and not split and old guests with no driver packed support.
It is based on the ideas of DPDK SW assisted LM, in the series of DPDK's https://patchwork.dpdk.org/cover/48370/ . However, these does not map the shadow vq in guest's VA, but in qemu's.
This version of SVQ is limited in the amount of features it can use with guest and device, because this series is already very big otherwise. Features like indirect or
event_idxwill be addressed in future series.SVQ needs to be enabled with cmdline parameter x-svq, like:
-netdev type=vhost-vdpa,vhostdev=/dev/vhost-vdpa-0,id=vhost-vdpa0,x-svq=true
In this version it cannot be enabled or disabled in runtime. Further series will remove this limitation and will enable it only for migration time.
Some patches are intentionally very small to ease review, but they can be squashed if preferred.
Patches 1-10 prepares the SVQ and QEMU to support both guest to device and device to guest notifications forwarding, with the extra qemu hop. That part can be tested in isolation if cmdline change is reproduced.
Patches from 11 to 18 implement the actual buffer forwarding, but with no IOMMU support. It requires a vdpa device capable of addressing all qemu vaddr.
Quoted 7 lines
Patches 19 to 23 adds the iommu support, so the device with address range limitations can access SVQ through this new virtual address space created.
The rest of the series add the last pieces needed for migration.
Comments are welcome.
I wonder the performance impact. So performance numbers are more than welcomed.
Thanks
Quoted 133 lines
TODO:
* Event, indirect, packed, and other features of virtio.
* To separate buffers forwarding in its own AIO context, so we can throw more threads to that task and we don't need to stop the main event loop.
* Support virtio-net control vq.
* Proper documentation.Changes from v5 RFC:
* Remove dynamic enablement of SVQ, making less dependent of the device.
* Enable live migration if SVQ is enabled.
* Fix SVQ when driver reset.
* Comments addressed, specially in the iova area.
* Rebase on latest master, adding multiqueue support (but no networking control vq processing).
v5 link:
https://lists.gnu.org/archive/html/qemu-devel/2021-10/msg07250.htmlChanges from v4 RFC:
* Support of allocating / freeing iova ranges in IOVA tree. Extending already present iova-tree for that.
* Proper validation of guest features. Now SVQ can negotiate a different set of features with the device when enabled.
* Support of host notifiers memory regions
* Handling of SVQ full queue in case guest's descriptors span to different memory regions (qemu's VA chunks).
* Flush pending used buffers at end of SVQ operation.
* QMP command now looks by NetClientState name. Other devices will need to implement it's way to enable vdpa.
* Rename QMP command to set, so it looks more like a way of working
* Better use of qemu error system
* Make a few assertions proper error-handling paths.
* Add more documentation
* Less coupling of virtio / vhost, that could cause friction on changes
* Addressed many other small comments and small fixes.Changes from v3 RFC:
* Move everything to vhost-vdpa backend. A big change, this allowed some cleanup but more code has been added in other places.
* More use of glib utilities, especially to manage memory.
v3 link:
https://lists.nongnu.org/archive/html/qemu-devel/2021-05/msg06032.htmlChanges from v2 RFC:
* Adding vhost-vdpa devices support
* Fixed some memory leaks pointed by different comments
v2 link:
https://lists.nongnu.org/archive/html/qemu-devel/2021-03/msg05600.htmlChanges from v1 RFC:
* Use QMP instead of migration to start SVQ mode.
* Only accepting IOMMU devices, closer behavior with target devices(vDPA)* Fix invalid masking/unmasking of vhost call fd.
* Use of proper methods for synchronization.
* No need to modify VirtIO device code, all of the changes are contained in vhost code.
* Delete superfluous code.
* An intermediate RFC was sent with only the notifications forwarding changes. It can be seen in
https://patchew.org/QEMU/[email protected]/
v1 link:
https://lists.gnu.org/archive/html/qemu-devel/2020-11/msg05372.htmlEugenio Pérez (20):
virtio: AddVIRTIO_F_QUEUE_STATE
virtio-net: HonorVIRTIO_CONFIG_S_DEVICE_STOPPED
virtio: Addvirtio_queue_is_host_notifier_enabled
vhost: Make vhost_virtqueue_{start,stop} public
vhost: Add x-vhost-enable-shadow-vq qmp
vhost: Add VhostShadowVirtqueue
vdpa: Register vdpa devices in a list
vhost: Routeguest->hostnotification through shadow virtqueue Addvhost_svq_get_svq_call_notifier
Addvhost_svq_set_guest_call_notifier
vdpa: Savecall_fdin vhost-vdpa
vhost-vdpa: Take into account SVQ invhost_vdpa_set_vring_call
vhost: Routehost->guestnotification through shadow virtqueue
virtio: Addvhost_shadow_vq_get_vring_addr
vdpa: Save host and guest features
vhost: Addvhost_svq_valid_device_featuresto shadow vq
vhost: Shadow virtqueue buffers forwarding
vhost: Add VhostIOVATree
vhost: Use a tree to store memory mappings
vdpa: Add custom IOTLB translations to SVQEugenio Pérez (31):
vdpa: Reordervirtio/vhost-vdpa.cfunctions
vhost: Add VhostShadowVirtqueue
vdpa: Addvhost_svq_get_dev_kick_notifier
vdpa: Addvhost_svq_set_svq_kick_fd
vhost: Add Shadow VirtQueue kick forwarding capabilities
vhost: Routeguest->hostnotification through shadow virtqueue
vhost: ddvhost_svq_get_svq_call_notifier
vhost: Addvhost_svq_set_guest_call_notifier
vhost-vdpa: Take into account SVQ invhost_vdpa_set_vring_call
vhost: Routehost->guestnotification through shadow virtqueue
vhost: Addvhost_svq_valid_device_featuresto shadow vq
vhost: Addvhost_svq_valid_guest_featuresto shadow vq
vhost: Addvhost_svq_ack_guest_featuresto shadow vq
virtio: Addvhost_shadow_vq_get_vring_addr
vdpa: Addvhost_svq_get_num
vhost: pass queue index tovhost_vq_get_addr
vdpa: adaptvhost_opscallbacks to svq
vhost: Shadow virtqueue buffers forwarding
utils: Add internal DMAMap to iova-tree
util: Store DMA entries in a list
util: Addiova_tree_alloc
vhost: Add VhostIOVATree
vdpa: Add custom IOTLB translations to SVQ
vhost: Addvhost_svq_get_last_used_idx
vdpa: Adaptvhost_vdpa_get_vring_baseto SVQ
vdpa: ClearVHOST_VRING_F_LOGatvhost_vdpa_set_vring_addrin SVQ
vdpa: Never setlog_baseaddr if SVQ is enabled
vdpa: ExposeVHOST_F_LOG_ALLon SVQ
vdpa: Make ncs autofree
vdpa: Movevhost_vdpa_get_iova_rangetonet/vhost-vdpa.c
vdpa: Add x-svq to NetdevVhostVDPAOptionsqapi/net.json | 5 +- hw/virtio/vhost-iova-tree.h | 27 + hw/virtio/vhost-shadow-virtqueue.h | 46 ++ include/hw/virtio/vhost-vdpa.h | 7 + include/qemu/iova-tree.h | 17 + hw/virtio/vhost-iova-tree.c | 157 ++++++ hw/virtio/vhost-shadow-virtqueue.c | 761 +++++++++++++++++++++++++++++ hw/virtio/vhost-vdpa.c | 740 ++++++++++++++++++++++++---- hw/virtio/vhost.c | 6 +- net/vhost-vdpa.c | 58 ++- util/iova-tree.c | 161 +++++- hw/virtio/meson.build | 2 +- 12 files changed, 1852 insertions(+), 135 deletions(-)
Quoted 5 lines
create mode 100644
hw/virtio/vhost-iova-tree.hcreate mode 100644hw/virtio/vhost-shadow-virtqueue.hcreate mode 100644hw/virtio/vhost-iova-tree.ccreate mode 100644hw/virtio/vhost-shadow-virtqueue.c
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH 03/31] vdpa: Add vhost_svq_get_dev_kick_notifier
In reply to Eugenio PérezQuoted 13 lines · Eugenio Pérez
Is needed so vhost-vdpa knows the device's kick event fd.
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-shadow-virtqueue.h | 4 ++++ hw/virtio/vhost-shadow-virtqueue.c | 10 +++++++++- 2 files changed, 13 insertions(+), 1 deletion(-)hw/virtio/vhost-shadow-virtqueue.h +0
-0
hw/virtio/vhost-shadow-virtqueue.h line 14
Quoted 4 lines
hw/virtio/vhost-shadow-virtqueue.h +1
-0@@ -11,9 +11,13 @@#define VHOST_SHADOW_VIRTQUEUE_H #include "hw/virtio/vhost.h" +#include "qemu/event_notifier.h"
Let's move this part to patch 2.
Thanks
Quoted 27 lines
hw/virtio/vhost-shadow-virtqueue.h +3
-0typedef struct VhostShadowVirtqueue VhostShadowVirtqueue ; +const EventNotifier *vhost_svq_get_dev_kick_notifier ( +const VhostShadowVirtqueue *svq); +VhostShadowVirtqueue *vhost_svq_new (void );void vhost_svq_free (VhostShadowVirtqueue *vq);hw/virtio/vhost-shadow-virtqueue.c +2
-1@@ -11,7 +11,6 @@#include "hw/virtio/vhost-shadow-virtqueue.h" #include "qemu/error-report.h" -#include "qemu/event_notifier.h" /* Shadow virtqueue to relay notifications */ typedef struct VhostShadowVirtqueue {@@ -21,6 +20,15 @@ typedef struct VhostShadowVirtqueue {EventNotifier hdev_call; } VhostShadowVirtqueue; +/** +* The notifier that SVQ will use to notify the device.
hw/virtio/vhost-shadow-virtqueue.c typedef struct VhostShadowVirtqueue { · line 34
Quoted 10 lines
hw/virtio/vhost-shadow-virtqueue.c +7
-0+*/ +const EventNotifier *vhost_svq_get_dev_kick_notifier ( +const VhostShadowVirtqueue *svq) +{ +return &svq->hdev_kick; +} +/** * Creates vhost shadow virtqueue, and instruct vhost device to use the shadow * methods and file descriptors.
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH 04/31] vdpa: Add vhost_svq_set_svq_kick_fd
In reply to Eugenio PérezQuoted 63 lines · Eugenio Pérez
This function allows the vhost-vdpa backend to override
kick_fd.Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-shadow-virtqueue.h | 1 + hw/virtio/vhost-shadow-virtqueue.c | 45 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+)hw/virtio/vhost-shadow-virtqueue.h +1
-0@@ -15,6 +15,7 @@typedef struct VhostShadowVirtqueue VhostShadowVirtqueue ; +void vhost_svq_set_svq_kick_fd (VhostShadowVirtqueue *svq,int svq_kick_fd);const EventNotifier *vhost_svq_get_dev_kick_notifier (const VhostShadowVirtqueue *svq);hw/virtio/vhost-shadow-virtqueue.c +19
-0@@ -11,6 +11,7 @@#include "hw/virtio/vhost-shadow-virtqueue.h" #include "qemu/error-report.h" +#include "qemu/main-loop.h" /* Shadow virtqueue to relay notifications */ typedef struct VhostShadowVirtqueue {@@ -18,8 +19,20 @@ typedef struct VhostShadowVirtqueue {EventNotifier hdev_kick;/* Shadow call notifier, sent to vhost */ EventNotifier hdev_call; + +/* +* Borrowed virtqueue's guest to host notifier. +* To borrow it in this event notifier allows to register on the event +* loop and access the associated shadow virtqueue easily. If we use the +* VirtQueue, we don't have an easy way to retrieve it. +* +* So shadow virtqueue must not clean it, or we would lose VirtQueue one. +*/ +EventNotifier svq_kick; } VhostShadowVirtqueue; +#define INVALID_SVQ_KICK_FD -1 +/** * The notifier that SVQ will use to notify the device. */ @@ -29,6 +42,35 @@ const EventNotifier *vhost_svq_get_dev_kick_notifier(return &svq->hdev_kick; } +/** +* Set a new file descriptor for the guest to kick SVQ and notify for avail +* +* @svq The svq +* @svq_kick_fd The new svq kick fd +*/
hw/virtio/vhost-shadow-virtqueue.c const EventNotifier *vhost_svq_get_dev_kick_notifier( · line 60
Quoted 10 lines
hw/virtio/vhost-shadow-virtqueue.c +10
-0+void vhost_svq_set_svq_kick_fd (VhostShadowVirtqueue *svq,int svq_kick_fd) +{ +EventNotifier tmp; +bool check_old =INVALID_SVQ_KICK_FD != +event_notifier_get_fd (&svq->svq_kick); + +if (check_old) { +event_notifier_set_handler (&svq->svq_kick,NULL ); +event_notifier_init_fd (&tmp,event_notifier_get_fd (&svq->svq_kick)); + }
It looks to me we don't do similar things in vhost-net. Any reason for caring about the old svq_kick?
hw/virtio/vhost-shadow-virtqueue.c const EventNotifier *vhost_svq_get_dev_kick_notifier( · line 70
Quoted 10 lines
hw/virtio/vhost-shadow-virtqueue.c +10
-0+ +/* +* event_notifier_set_handler already checks for guest's notifications if +* they arrive to the new file descriptor in the switch, so there is no +* need to explicitely check for them. +*/ +event_notifier_init_fd (&svq->svq_kick, svq_kick_fd); + +if (!check_old ||event_notifier_test_and_clear (&tmp)) { +event_notifier_set (&svq->hdev_kick);
Any reason we need to kick the device directly here?
Thanks
Quoted 7 lines
hw/virtio/vhost-shadow-virtqueue.c +3
-0+ } +} +/** * Creates vhost shadow virtqueue, and instruct vhost device to use the shadow * methods and file descriptors.
hw/virtio/vhost-shadow-virtqueue.c VhostShadowVirtqueue *vhost_svq_new(void) · line 102
Quoted 9 lines
hw/virtio/vhost-shadow-virtqueue.c +3
-0@@ -52,6 +94,9 @@ VhostShadowVirtqueue *vhost_svq_new(void) goto err_init_hdev_call; } +/* Placeholder descriptor, it should be deleted at set_kick_fd */ +event_notifier_init_fd (&svq->svq_kick,INVALID_SVQ_KICK_FD ); +return g_steal_pointer (&svq); err_init_hdev_call:
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH 05/31] vhost: Add Shadow VirtQueue kick forwarding capabilities
In reply to Eugenio PérezQuoted 6 lines · Eugenio Pérez
At this mode no buffer forwarding will be performed in SVQ mode: Qemu will just forward the guest's kicks to the device.
Also, host notifiers must be disabled at SVQ start, and they will not start if SVQ has been enabled when the device is stopped. This will be addressed in next patches.
We need to disable host_notifier_mr as well, otherwise guest may touch the hardware doorbell directly without going through eventfd.
Quoted 61 lines
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-shadow-virtqueue.h | 2 ++ hw/virtio/vhost-shadow-virtqueue.c | 27 ++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-)hw/virtio/vhost-shadow-virtqueue.h +2
-0@@ -19,6 +19,8 @@ void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd);const EventNotifier *vhost_svq_get_dev_kick_notifier (const VhostShadowVirtqueue *svq); +void vhost_svq_stop (VhostShadowVirtqueue *svq); +VhostShadowVirtqueue *vhost_svq_new (void );void vhost_svq_free (VhostShadowVirtqueue *vq);hw/virtio/vhost-shadow-virtqueue.c +17
-1@@ -42,11 +42,26 @@ const EventNotifier *vhost_svq_get_dev_kick_notifier(return &svq->hdev_kick; } +/* Forward guest notifications */ +static void vhost_handle_guest_kick (EventNotifier *n) +{ +VhostShadowVirtqueue *svq =container_of (n, VhostShadowVirtqueue, + svq_kick); + +if (unlikely (!event_notifier_test_and_clear (n))) { +return ; + } + +event_notifier_set (&svq->hdev_kick); +} +/** * Set a new file descriptor for the guest to kick SVQ and notify for avail * * @svq The svq -* @svq_kick_fd The new svq kick fd +* @svq_kick_fd The svq kick fd +* +* Note that SVQ will never close the old file descriptor. */ void vhost_svq_set_svq_kick_fd (VhostShadowVirtqueue *svq,int svq_kick_fd) {@@ -65,12 +80,22 @@ void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd)* need to explicitely check for them. */ event_notifier_init_fd (&svq->svq_kick, svq_kick_fd); +event_notifier_set_handler (&svq->svq_kick, vhost_handle_guest_kick);if (!check_old ||event_notifier_test_and_clear (&tmp)) {event_notifier_set (&svq->hdev_kick); }
hw/virtio/vhost-shadow-virtqueue.c void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd) · line 97
Quoted 10 lines
hw/virtio/vhost-shadow-virtqueue.c +8
-0} +/** +* Stop shadow virtqueue operation. +* @svq Shadow Virtqueue +*/ +void vhost_svq_stop (VhostShadowVirtqueue *svq) +{ +event_notifier_set_handler (&svq->svq_kick,NULL ); +}
This function is not used in the patch.
Thanks
hw/virtio/vhost-shadow-virtqueue.c void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd) · line 101
Quoted 4 lines
hw/virtio/vhost-shadow-virtqueue.c +1
-0+/** * Creates vhost shadow virtqueue, and instruct vhost device to use the shadow * methods and file descriptors.
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH 06/31] vhost: Route guest->host notification through shadow virtqueue
In reply to Eugenio PérezQuoted 6 lines · Eugenio Pérez
At this moment no buffer forwarding will be performed in SVQ mode: Qemu just forward the guest's kicks to the device. This commit also set up SVQs in the vhost device.
Host memory notifiers regions are left out for simplicity, and they will not be addressed in this series.
I wonder if it's better to squash this into patch 5 since it gives us a full guest->host forwarding.
Quoted 49 lines
Signed-off-by Eugenio Pérez <[email protected]>--- include/hw/virtio/vhost-vdpa.h | 4 ++ hw/virtio/vhost-vdpa.c | 122 ++++++++++++++++++++++++++++++++- 2 files changed, 124 insertions(+), 2 deletions(-)include/hw/virtio/vhost-vdpa.h +4
-0@@ -12,6 +12,8 @@#ifndef HW_VIRTIO_VHOST_VDPA_H #define HW_VIRTIO_VHOST_VDPA_H +#include <gmodule.h> +#include "hw/virtio/virtio.h" #include "standard-headers/linux/vhost_types.h" @@ -27,6 +29,8 @@ typedef struct vhost_vdpa {bool iotlb_batch_begin_sent;MemoryListener listener;struct vhost_vdpa_iova_range iova_range; +bool shadow_vqs_enabled; +GPtrArray *shadow_vqs;struct vhost_dev *dev;VhostVDPAHostNotifier notifier[VIRTIO_QUEUE_MAX ]; } VhostVDPA;hw/virtio/vhost-vdpa.c +2
-0@@ -17,12 +17,14 @@#include "hw/virtio/vhost.h" #include "hw/virtio/vhost-backend.h" #include "hw/virtio/virtio-net.h" +#include "hw/virtio/vhost-shadow-virtqueue.h" #include "hw/virtio/vhost-vdpa.h" #include "exec/address-spaces.h" #include "qemu/main-loop.h" #include "cpu.h" #include "trace.h" #include "qemu-common.h" +#include "qapi/error.h" /* * Return one past the end of the end of section. Be careful with uint64_t
hw/virtio/vhost-vdpa.c err: · line 418
Quoted 8 lines
hw/virtio/vhost-vdpa.c +3
-0@@ -409,8 +411,14 @@ err:static void vhost_vdpa_host_notifiers_init (struct vhost_dev *dev) { +struct vhost_vdpa *v = dev->opaque;int i; +if (v->shadow_vqs_enabled) { +/* SVQ is not compatible with host notifiers mr */
I guess there should be a TODO or FIXME here.
Quoted 91 lines
hw/virtio/vhost-vdpa.c +46
-2+return ; + } +for (i = dev->vq_index; i < dev->vq_index + dev->nvqs; i++) {if (vhost_vdpa_host_notifier_init (dev, i)) { goto err;@@ -424,6 +432,17 @@ err:return ; } +static void vhost_vdpa_svq_cleanup (struct vhost_dev *dev) +{ +struct vhost_vdpa *v = dev->opaque; +size_t idx; + +for (idx =0 ; idx < v->shadow_vqs->len; ++idx) { +vhost_svq_stop (g_ptr_array_index (v->shadow_vqs, idx)); + } +g_ptr_array_free (v->shadow_vqs, true); +} +static int vhost_vdpa_cleanup (struct vhost_dev *dev) {struct vhost_vdpa *v;@@ -432,6 +451,7 @@ static int vhost_vdpa_cleanup(struct vhost_dev *dev)trace_vhost_vdpa_cleanup (dev, v);vhost_vdpa_host_notifiers_uninit (dev, dev->nvqs);memory_listener_unregister (&v->listener); +vhost_vdpa_svq_cleanup (dev); dev->opaque =NULL ;ram_block_discard_disable (false);@@ -507,9 +527,15 @@ static int vhost_vdpa_get_device_id(struct vhost_dev *dev,static int vhost_vdpa_reset_device (struct vhost_dev *dev) { +struct vhost_vdpa *v = dev->opaque;int ret;uint8_t status =0 ; +for (unsigned i =0 ; i < v->shadow_vqs->len; ++i) { +VhostShadowVirtqueue *svq =g_ptr_array_index (v->shadow_vqs, i); +vhost_svq_stop (svq); + } + ret =vhost_vdpa_call (dev,VHOST_VDPA_SET_STATUS , &status);trace_vhost_vdpa_reset_device (dev, status);return ret;@@ -639,13 +665,28 @@ static int vhost_vdpa_get_vring_base(struct vhost_dev *dev,return ret; } -static int vhost_vdpa_set_vring_kick (struct vhost_dev *dev, -struct vhost_vring_file *file) +static int vhost_vdpa_set_vring_dev_kick (struct vhost_dev *dev, +struct vhost_vring_file *file) {trace_vhost_vdpa_set_vring_kick (dev, file->index, file->fd);return vhost_vdpa_call (dev,VHOST_SET_VRING_KICK , file); } +static int vhost_vdpa_set_vring_kick (struct vhost_dev *dev, +struct vhost_vring_file *file) +{ +struct vhost_vdpa *v = dev->opaque; +int vdpa_idx =vhost_vdpa_get_vq_index (dev, file->index); + +if (v->shadow_vqs_enabled) { +VhostShadowVirtqueue *svq =g_ptr_array_index (v->shadow_vqs, vdpa_idx); +vhost_svq_set_svq_kick_fd (svq, file->fd); +return 0 ; + }else { +return vhost_vdpa_set_vring_dev_kick (dev, file); + } +} +static int vhost_vdpa_set_vring_call (struct vhost_dev *dev,struct vhost_vring_file *file) {@@ -653,6 +694,33 @@ static int vhost_vdpa_set_vring_call(struct vhost_dev *dev,return vhost_vdpa_call (dev,VHOST_SET_VRING_CALL , file); } +/** +* Set shadow virtqueue descriptors to the device +* +* @dev The vhost device model +* @svq The shadow virtqueue +* @idx The index of the virtqueue in the vhost device +*/ +static bool vhost_vdpa_svq_setup(struct vhost_dev *dev,
hw/virtio/vhost-vdpa.c static int vhost_vdpa_set_vring_call(struct vhost_dev *dev, · line 714
Quoted 10 lines
hw/virtio/vhost-vdpa.c +10
-0+ VhostShadowVirtqueue *svq, + unsigned idx) +{ +struct vhost_vring_file file = { + .index = dev->vq_index + idx, + }; +const EventNotifier *event_notifier; +int r; + + event_notifier =vhost_svq_get_dev_kick_notifier (svq);
A question, any reason for making VhostShadowVirtqueue private? If we export it in .h we don't need helper to access its member like vhost_svq_get_dev_kick_notifier().
Note that vhost_dev is a public structure.
hw/virtio/vhost-vdpa.c static int vhost_vdpa_set_vring_call(struct vhost_dev *dev, · line 719
Quoted 5 lines
hw/virtio/vhost-vdpa.c +5
-0+ file.fd =event_notifier_get_fd (event_notifier); + r =vhost_vdpa_set_vring_dev_kick (dev, &file); +if (unlikely (r !=0 )) { +error_report ("Can't set device kick fd (%d)" , -r); + }
I wonder whether or not we can generalize the logic here and vhost_vdpa_set_vring_kick(). There's nothing vdpa specific unless the vhost_ops->set_vring_kick().
Quoted 24 lines
hw/virtio/vhost-vdpa.c +11
-0+ +return r ==0 ; +} +static int vhost_vdpa_dev_start (struct vhost_dev *dev,bool started) {struct vhost_vdpa *v = dev->opaque;@@ -660,6 +728,13 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)if (started) {vhost_vdpa_host_notifiers_init (dev); +for (unsigned i =0 ; i < v->shadow_vqs->len; ++i) { +VhostShadowVirtqueue *svq =g_ptr_array_index (v->shadow_vqs, i); +bool ok =vhost_vdpa_svq_setup (dev, svq, i); +if (unlikely (!ok)) { +return -1 ; + } + }vhost_vdpa_set_vring_ready (dev); }else {vhost_vdpa_host_notifiers_uninit (dev, dev->nvqs);@@ -737,6 +812,41 @@ static bool vhost_vdpa_force_iommu(struct vhost_dev *dev)return true; }
hw/virtio/vhost-vdpa.c static bool vhost_vdpa_force_iommu(struct vhost_dev *dev) · line 823
Quoted 10 lines
hw/virtio/vhost-vdpa.c +9
-0+/** +* Adaptor function to free shadow virtqueue through gpointer +* +* @svq The Shadow Virtqueue +*/ +static void vhost_psvq_free (gpointer svq) +{ +vhost_svq_free (svq); +}
Any reason for such indirection? Can we simply use vhost_svq_free()?
Thanks
Quoted 41 lines
hw/virtio/vhost-vdpa.c +30
-0+ +static int vhost_vdpa_init_svq (struct vhost_dev *hdev,struct vhost_vdpa *v, +Error **errp) +{ +size_t n_svqs = v->shadow_vqs_enabled ? hdev->nvqs :0 ; +g_autoptr (GPtrArray) shadow_vqs =g_ptr_array_new_full (n_svqs, + vhost_psvq_free); +if (!v->shadow_vqs_enabled) { + goto out; + } + +for (unsigned n =0 ; n < hdev->nvqs; ++n) { +VhostShadowVirtqueue *svq =vhost_svq_new (); + +if (unlikely (!svq)) { +error_setg (errp,"Cannot create svq %u" , n); +return -1 ; + } +g_ptr_array_add (v->shadow_vqs, svq); + } + +out: + v->shadow_vqs =g_steal_pointer (&shadow_vqs); +return 0 ; +} +static int vhost_vdpa_init (struct vhost_dev *dev,void *opaque,Error **errp) {struct vhost_vdpa *v;@@ -759,6 +869,10 @@ static int vhost_vdpa_init(struct vhost_dev *dev, void *opaque, Error **errp) dev->opaque = opaque ; v->listener = vhost_vdpa_memory_listener; v->msg_type =VHOST_IOTLB_MSG_V2 ; + ret =vhost_vdpa_init_svq (dev, v, errp); +if (ret) { + goto err; + }vhost_vdpa_get_iova_range (v);
hw/virtio/vhost-vdpa.c static int vhost_vdpa_init(struct vhost_dev *dev, void *opaque, Error **errp) · line 893
Quoted 10 lines
hw/virtio/vhost-vdpa.c +4
-0@@ -770,6 +884,10 @@ static int vhost_vdpa_init(struct vhost_dev *dev, void *opaque, Error **errp) VIRTIO_CONFIG_S_DRIVER);return 0 ; + +err: +ram_block_discard_disable (false); +return ret; }const VhostOps vdpa_ops = {
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Eugenio Perez Martin
maintainerRe: [PATCH 21/31] util: Add iova_tree_alloc
In reply to Jason WangQuoted 69 lines · Jason Wang
Quoted 24 lines · Peter Xu
Quoted 23 lines · Eugenio Perez Martin
Quoted 9 lines · Peter Xu
Quoted 8 lines · Eugenio Perez Martin
So I think that the first step to remove complexity from the old one is to remove
iova_beginandiova_end.As Jason points out, removing
iova_endis easier. It has the drawback of having to traverse all the list beyondiova_end, but a well formed iova tree should contain none. If the guest can manipulate it, it's only hurting itself adding nodes to it.Quoted 8 lines
Quoted 7 lines
It's possible to extract the check for
hole_right(or this in Jason's proposal) as a special case too.But removing the
iova_beginparameter is more complicated. We cannot know if it's a valid hole without knowingiova_begin, and we cannot resume traversing. Could we assumeiova_beginwill always be 0? I think not, the vdpa device can return anything through syscall.Frankly I don't know what's the syscall you're talking about,
I meant
VHOST_VDPA_GET_IOVA_RANGE, which allows qemu to know the valid range of iova addresses. We get a pair ofuint64_tfrom it, that indicates the minimum and maximum iova address the device (or iommu) supports.Quoted 32 lines
Quoted 7 lines
We must allocate iova ranges within that address range, which complicates this algorithm a little bit. Since the SVQ iova addresses are not GPA, qemu needs extra code to be able to allocate and free them, creating a new custom iova as.
Please let me know if you want more details or if you prefer me to give more context in the patch message.
That's good enough, thanks.
Quoted 23 lines
Quoted 10 lines
I mean this one:
https://lore.kernel.org/qemu-devel/[email protected]/
Though this time I have some comments on the details.
Personally I like that one (probably with some amendment upon the old version) more than the current list-based approach. But I'd like to know your thoughts too (including Jason). I'll further comment in that thread soon.
Sure, I'm fine with whatever solution we choose, but I'm just running out of ideas to simplify it. Reading your suggestions on old RFC now.
Overall I feel list-based one is both more convenient and easy to delete when qemu raises the minimal glib version, but it adds a lot more code.
It could add less code with this less elegant changes:
* If we just put the list entry in the DMAMap itself, although it exposes unneeded implementation details.
* We force the iova tree either to be an allocation-based or an insertion-based, but not both. In other words, you can only either useiova_tree_allocoriova_tree_inserton the same tree.This seems an odd API I must say :(
Quoted 3 lines
Yeah, I just noticed it yesterday that there's no easy choice on it. Let's go with either way; it shouldn't block the rest of the code. It'll be good if Jason or Michael share their preferences too.
Quoted 5 lines
(Havne't gone through the code deeply)
I wonder how about just copy-paste gtree_node_first|last()? A quick google told me it's not complicated.
Both GTree and GTreeNode definitions are not part of the ABI of glib. I think the ustream code has not changed its layout for a long time but still it's allowed to do so in the future.
Having said that, they use a list internally to traverse the nodes, with node->left and node->right instead of TAILQ entries. These patches duplicate that intrusive list in DMAMap entries, and make them private so other parts of qemu are not affected.
Thanks!
Quoted 10 lines
Thanks
Quoted 6 lines
Quoted 4 lines
I have a few tests to check the algorithms, but they are not in the qemu test format. I will post them so we all can understand better what is expected from this.
Sure. Thanks.
Eugenio Perez Martin
maintainerRe: [PATCH 01/31] vdpa: Reorder virtio/vhost-vdpa.c functions
In reply to Jason WangQuoted 6 lines · Jason Wang
Quoted 3 lines · Eugenio Pérez
vhost_vdpa_set_featuresandvhost_vdpa_initneed to usevhost_vdpa_get_featuresin svq mode.
Quoted 9 lines
Quoted 5 lines
vhost_vdpa_dev_startneeds to use almost all _set_ functions:vhost_vdpa_set_vring_dev_kick,vhost_vdpa_set_vring_dev_call,vhost_vdpa_set_dev_vring_baseandvhost_vdpa_set_dev_vring_num.No functional change intended.
Is it related (a must) to the SVQ code?
Yes, SVQ needs to access the device variants to configure it, while exposing the SVQ ones.
For example for set_features, SVQ needs to set device features in the start code, but expose SVQ ones to the guest.
Another possibility is to forward-declare them but I feel it pollutes the code more, doesn't it? Is there any reason to avoid the reordering beyond reducing the number of changes/patches?
Thanks!
Quoted 220 lines
Thanks
Quoted 216 lines
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-vdpa.c | 164 ++++++++++++++++++++--------------------- 1 file changed, 82 insertions(+), 82 deletions(-)hw/virtio/vhost-vdpa.c +82
-82@@ -342,41 +342,6 @@ static bool vhost_vdpa_one_time_request(struct vhost_dev *dev)return v->index !=0 ; } -static int vhost_vdpa_init (struct vhost_dev *dev,void *opaque,Error **errp) -{ -struct vhost_vdpa *v; -assert (dev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_VDPA ); -trace_vhost_vdpa_init (dev, opaque); -int ret; - -/* -* Similar to VFIO, we end up pinning all guest memory and have to -* disable discarding of RAM. -*/ - ret =ram_block_discard_disable (true); -if (ret) { -error_report ("Cannot set discarding of RAM broken" ); -return ret; - } - - v = opaque; - v->dev = dev; - dev->opaque = opaque ; - v->listener = vhost_vdpa_memory_listener; - v->msg_type =VHOST_IOTLB_MSG_V2 ; - -vhost_vdpa_get_iova_range (v); - -if (vhost_vdpa_one_time_request (dev)) { -return 0 ; - } - -vhost_vdpa_add_status (dev,VIRTIO_CONFIG_S_ACKNOWLEDGE | -VIRTIO_CONFIG_S_DRIVER ); - -return 0 ; -} -static void vhost_vdpa_host_notifier_uninit (struct vhost_dev *dev,int queue_index) {@@ -506,24 +471,6 @@ static int vhost_vdpa_set_mem_table(struct vhost_dev *dev,return 0 ; } -static int vhost_vdpa_set_features (struct vhost_dev *dev, -uint64_t features) -{ -int ret; - -if (vhost_vdpa_one_time_request (dev)) { -return 0 ; - } - -trace_vhost_vdpa_set_features (dev, features); - ret =vhost_vdpa_call (dev,VHOST_SET_FEATURES , &features); -if (ret) { -return ret; - } - -return vhost_vdpa_add_status (dev,VIRTIO_CONFIG_S_FEATURES_OK ); -} -static int vhost_vdpa_set_backend_cap (struct vhost_dev *dev) {uint64_t features;@@ -646,35 +593,6 @@ static int vhost_vdpa_get_config(struct vhost_dev *dev, uint8_t *config,return ret; } -static int vhost_vdpa_dev_start (struct vhost_dev *dev,bool started) -{ -struct vhost_vdpa *v = dev->opaque; -trace_vhost_vdpa_dev_start (dev, started); - -if (started) { -vhost_vdpa_host_notifiers_init (dev); -vhost_vdpa_set_vring_ready (dev); - }else { -vhost_vdpa_host_notifiers_uninit (dev, dev->nvqs); - } - -if (dev->vq_index + dev->nvqs != dev->vq_index_end) { -return 0 ; - } - -if (started) { -memory_listener_register (&v->listener, &address_space_memory); -return vhost_vdpa_add_status (dev,VIRTIO_CONFIG_S_DRIVER_OK ); - }else { -vhost_vdpa_reset_device (dev); -vhost_vdpa_add_status (dev,VIRTIO_CONFIG_S_ACKNOWLEDGE | -VIRTIO_CONFIG_S_DRIVER ); -memory_listener_unregister (&v->listener); - -return 0 ; - } -} -static int vhost_vdpa_set_log_base (struct vhost_dev *dev,uint64_t base,struct vhost_log *log) {@@ -735,6 +653,35 @@ static int vhost_vdpa_set_vring_call(struct vhost_dev *dev,return vhost_vdpa_call (dev,VHOST_SET_VRING_CALL , file); } +static int vhost_vdpa_dev_start (struct vhost_dev *dev,bool started) +{ +struct vhost_vdpa *v = dev->opaque; +trace_vhost_vdpa_dev_start (dev, started); + +if (started) { +vhost_vdpa_host_notifiers_init (dev); +vhost_vdpa_set_vring_ready (dev); + }else { +vhost_vdpa_host_notifiers_uninit (dev, dev->nvqs); + } + +if (dev->vq_index + dev->nvqs != dev->vq_index_end) { +return 0 ; + } + +if (started) { +memory_listener_register (&v->listener, &address_space_memory); +return vhost_vdpa_add_status (dev,VIRTIO_CONFIG_S_DRIVER_OK ); + }else { +vhost_vdpa_reset_device (dev); +vhost_vdpa_add_status (dev,VIRTIO_CONFIG_S_ACKNOWLEDGE | +VIRTIO_CONFIG_S_DRIVER ); +memory_listener_unregister (&v->listener); + +return 0 ; + } +} +static int vhost_vdpa_get_features (struct vhost_dev *dev,uint64_t *features) {@@ -745,6 +692,24 @@ static int vhost_vdpa_get_features(struct vhost_dev *dev,return ret; } +static int vhost_vdpa_set_features (struct vhost_dev *dev, +uint64_t features) +{ +int ret; + +if (vhost_vdpa_one_time_request (dev)) { +return 0 ; + } + +trace_vhost_vdpa_set_features (dev, features); + ret =vhost_vdpa_call (dev,VHOST_SET_FEATURES , &features); +if (ret) { +return ret; + } + +return vhost_vdpa_add_status (dev,VIRTIO_CONFIG_S_FEATURES_OK ); +} +static int vhost_vdpa_set_owner (struct vhost_dev *dev) {if (vhost_vdpa_one_time_request (dev)) {@@ -772,6 +737,41 @@ static bool vhost_vdpa_force_iommu(struct vhost_dev *dev)return true; } +static int vhost_vdpa_init (struct vhost_dev *dev,void *opaque,Error **errp) +{ +struct vhost_vdpa *v; +assert (dev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_VDPA ); +trace_vhost_vdpa_init (dev, opaque); +int ret; + +/* +* Similar to VFIO, we end up pinning all guest memory and have to +* disable discarding of RAM. +*/ + ret =ram_block_discard_disable (true); +if (ret) { +error_report ("Cannot set discarding of RAM broken" ); +return ret; + } + + v = opaque; + v->dev = dev; + dev->opaque = opaque ; + v->listener = vhost_vdpa_memory_listener; + v->msg_type =VHOST_IOTLB_MSG_V2 ; + +vhost_vdpa_get_iova_range (v); + +if (vhost_vdpa_one_time_request (dev)) { +return 0 ; + } + +vhost_vdpa_add_status (dev,VIRTIO_CONFIG_S_ACKNOWLEDGE | +VIRTIO_CONFIG_S_DRIVER ); + +return 0 ; +} +const VhostOps vdpa_ops = { .backend_type =VHOST_BACKEND_TYPE_VDPA , .vhost_backend_init = vhost_vdpa_init,
Eugenio Perez Martin
maintainerRe: [PATCH 02/31] vhost: Add VhostShadowVirtqueue
In reply to Jason WangQuoted 124 lines · Jason Wang
Quoted 121 lines · Eugenio Pérez
Vhost shadow virtqueue (SVQ) is an intermediate jump for virtqueue notifications and buffers, allowing qemu to track them. While qemu is forwarding the buffers and virtqueue changes, it is able to commit the memory it's being dirtied, the same way regular qemu's VirtIO devices do.
This commit only exposes basic SVQ allocation and free. Next patches of the series add functionality like notifications and buffers forwarding.
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-shadow-virtqueue.h | 21 ++++++++++ hw/virtio/vhost-shadow-virtqueue.c | 64 ++++++++++++++++++++++++++++++ hw/virtio/meson.build | 2 +- 3 files changed, 86 insertions(+), 1 deletion(-)create mode 100644
hw/virtio/vhost-shadow-virtqueue.hcreate mode 100644hw/virtio/vhost-shadow-virtqueue.chw/virtio/vhost-shadow-virtqueue.h new file +21
-0@@ -0,0 +1,21 @@ +/* +* vhost shadow virtqueue +* +* SPDX-FileCopyrightText: Red Hat, Inc. 2021 +* SPDX-FileContributor: Author: Eugenio Pérez <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef VHOST_SHADOW_VIRTQUEUE_H +#define VHOST_SHADOW_VIRTQUEUE_H + +#include "hw/virtio/vhost.h" + +typedef struct VhostShadowVirtqueue VhostShadowVirtqueue ; + +VhostShadowVirtqueue *vhost_svq_new (void ); + +void vhost_svq_free (VhostShadowVirtqueue *vq); + +#endif hw/virtio/vhost-shadow-virtqueue.c new file +64
-0@@ -0,0 +1,64 @@ +/* +* vhost shadow virtqueue +* +* SPDX-FileCopyrightText: Red Hat, Inc. 2021 +* SPDX-FileContributor: Author: Eugenio Pérez <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "qemu/osdep.h" +#include "hw/virtio/vhost-shadow-virtqueue.h" + +#include "qemu/error-report.h" +#include "qemu/event_notifier.h" + +/* Shadow virtqueue to relay notifications */ +typedef struct VhostShadowVirtqueue { +/* Shadow kick notifier, sent to vhost */ +EventNotifier hdev_kick; +/* Shadow call notifier, sent to vhost */ +EventNotifier hdev_call; +}VhostShadowVirtqueue ; + +/** +* Creates vhost shadow virtqueue, and instruct vhost device to use the shadow +* methods and file descriptors. +*/ +VhostShadowVirtqueue *vhost_svq_new (void ) +{ + g_autofree VhostShadowVirtqueue *svq = g_new0(VhostShadowVirtqueue, 1); +int r; + + r =event_notifier_init (&svq->hdev_kick,0 ); +if (r !=0 ) { +error_report ("Couldn't create kick event notifier: %s" , +strerror (errno)); + goto err_init_hdev_kick; + } + + r =event_notifier_init (&svq->hdev_call,0 ); +if (r !=0 ) { +error_report ("Couldn't create call event notifier: %s" , +strerror (errno)); + goto err_init_hdev_call; + } + +return g_steal_pointer (&svq); + +err_init_hdev_call: +event_notifier_cleanup (&svq->hdev_kick); + +err_init_hdev_kick: +return NULL ; +} + +/** +* Free the resources of the shadow virtqueue. +*/ +void vhost_svq_free (VhostShadowVirtqueue *vq) +{ +event_notifier_cleanup (&vq->hdev_kick); +event_notifier_cleanup (&vq->hdev_call); +g_free (vq); +}hw/virtio/meson.build +0
-0@@ -11,7 +11,7 @@ softmmu_ss.add(when: 'CONFIG_ALL', if_true: files('vhost-stub.c'))
Quoted 8 lines
hw/virtio/meson.build softmmu_ss.add(when: 'CONFIG_ALL', if_true: files('vhost-stub.c')) · line 14
Quoted 4 lines
hw/virtio/meson.build +1
-1virtio_ss = ss.source_set() virtio_ss.add(files('virtio.c')) -virtio_ss.add(when: 'CONFIG_VHOST', if_true: files('vhost.c', 'vhost-backend.c')) +virtio_ss.add(when: 'CONFIG_VHOST', if_true: files('vhost.c', 'vhost-backend.c', 'vhost-shadow-virtqueue.c'))I wonder if we need a dedicated config option for shadow virtqueue.
I'd say that the changes are isolated enough and require no external library dependencies so the gain is little. But it can be done with an explicit enable/disable for sure.
Thanks!
Quoted 7 lines
Thanks
Quoted 3 lines
hw/virtio/meson.build +0
-0virtio_ss.add(when: 'CONFIG_VHOST_USER', if_true: files('vhost-user.c')) virtio_ss.add(when: 'CONFIG_VHOST_VDPA', if_true: files('vhost-vdpa.c')) virtio_ss.add(when: 'CONFIG_VIRTIO_BALLOON', if_true: files('virtio-balloon.c'))
Re: [PATCH 07/31] vhost: dd vhost_svq_get_svq_call_notifier
In reply to Eugenio PérezQuoted 3 lines · Eugenio Pérez
This allows vhost-vdpa device to retrieve device -> svq call eventfd.
Signed-off-by Eugenio Pérez <[email protected]>
What did 'dd' mean in the title?
Thanks
Quoted 31 lines
--- hw/virtio/vhost-shadow-virtqueue.h | 2 ++ hw/virtio/vhost-shadow-virtqueue.c | 12 ++++++++++++ 2 files changed, 14 insertions(+)hw/virtio/vhost-shadow-virtqueue.h +2
-0@@ -18,6 +18,8 @@ typedef struct VhostShadowVirtqueue VhostShadowVirtqueue;void vhost_svq_set_svq_kick_fd (VhostShadowVirtqueue *svq,int svq_kick_fd);const EventNotifier *vhost_svq_get_dev_kick_notifier (const VhostShadowVirtqueue *svq); +const EventNotifier *vhost_svq_get_svq_call_notifier ( +const VhostShadowVirtqueue *svq);void vhost_svq_stop (VhostShadowVirtqueue *svq);hw/virtio/vhost-shadow-virtqueue.c +5
-0@@ -55,6 +55,18 @@ static void vhost_handle_guest_kick(EventNotifier *n)event_notifier_set (&svq->hdev_kick); } +/** +* Obtain the SVQ call notifier, where vhost device notifies SVQ that there +* exists pending used buffers. +* +* @svq Shadow Virtqueue
hw/virtio/vhost-shadow-virtqueue.c static void vhost_handle_guest_kick(EventNotifier *n) · line 72
Quoted 10 lines
hw/virtio/vhost-shadow-virtqueue.c +7
-0+*/ +const EventNotifier *vhost_svq_get_svq_call_notifier ( +const VhostShadowVirtqueue *svq) +{ +return &svq->hdev_call; +} +/** * Set a new file descriptor for the guest to kick SVQ and notify for avail *
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH 09/31] vhost-vdpa: Take into account SVQ in vhost_vdpa_set_vring_call
In reply to Eugenio PérezQuoted 22 lines · Eugenio Pérez
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-vdpa.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)hw/virtio/vhost-vdpa.c +2
-2@@ -687,13 +687,29 @@ static int vhost_vdpa_set_vring_kick(struct vhost_dev *dev, } } -static int vhost_vdpa_set_vring_call (struct vhost_dev *dev, -struct vhost_vring_file *file) +static int vhost_vdpa_set_vring_dev_call (struct vhost_dev *dev, +struct vhost_vring_file *file) {trace_vhost_vdpa_set_vring_call (dev, file->index, file->fd);return vhost_vdpa_call (dev,VHOST_SET_VRING_CALL , file); }
hw/virtio/vhost-vdpa.c static int vhost_vdpa_set_vring_kick(struct vhost_dev *dev, · line 706
Quoted 10 lines
hw/virtio/vhost-vdpa.c +10
-0+static int vhost_vdpa_set_vring_call (struct vhost_dev *dev, +struct vhost_vring_file *file) +{ +struct vhost_vdpa *v = dev->opaque; + +if (v->shadow_vqs_enabled) { +int vdpa_idx =vhost_vdpa_get_vq_index (dev, file->index); +VhostShadowVirtqueue *svq =g_ptr_array_index (v->shadow_vqs, vdpa_idx); + +vhost_svq_set_guest_call_notifier (svq, file->fd);
Two questions here (had similar questions for vring kick):
1) Any reason that we setup the eventfd for vhost-vdpa in vhost_vdpa_svq_setup() not here?
2) The call could be disabled by using -1 as the fd, I don't see any code to deal with that.
Thanks
hw/virtio/vhost-vdpa.c static int vhost_vdpa_set_vring_kick(struct vhost_dev *dev, · line 715
Quoted 9 lines
hw/virtio/vhost-vdpa.c +6
-0+return 0 ; + }else { +return vhost_vdpa_set_vring_dev_call (dev, file); + } +} +/** * Set shadow virtqueue descriptors to the device *
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH 11/31] vhost: Add vhost_svq_valid_device_features to shadow vq
In reply to Eugenio PérezQuoted 44 lines · Eugenio Pérez
This allows SVQ to negotiate features with the device. For the device, SVQ is a driver. While this function needs to bypass all non-transport features, it needs to disable the features that SVQ does not support when forwarding buffers. This includes packed vq layout, indirect descriptors or event idx.
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-shadow-virtqueue.h | 2 ++ hw/virtio/vhost-shadow-virtqueue.c | 44 ++++++++++++++++++++++++++++++ hw/virtio/vhost-vdpa.c | 21 ++++++++++++++ 3 files changed, 67 insertions(+)hw/virtio/vhost-shadow-virtqueue.h +2
-0@@ -15,6 +15,8 @@typedef struct VhostShadowVirtqueue VhostShadowVirtqueue ; +bool vhost_svq_valid_device_features (uint64_t *features); +void vhost_svq_set_svq_kick_fd (VhostShadowVirtqueue *svq,int svq_kick_fd);void vhost_svq_set_guest_call_notifier (VhostShadowVirtqueue *svq,int call_fd); const EventNotifier *vhost_svq_get_dev_kick_notifier(hw/virtio/vhost-shadow-virtqueue.c +10
-0@@ -45,6 +45,50 @@ const EventNotifier *vhost_svq_get_dev_kick_notifier(return &svq->hdev_kick; } +/** +* Validate the transport device features that SVQ can use with the device +* +* @dev_features The device features. If success, the acknowledged features. +* +* Returns true if SVQ can go with a subset of these, false otherwise. +*/ +bool vhost_svq_valid_device_features (uint64_t *dev_features) +{ +bool r = true;
hw/virtio/vhost-shadow-virtqueue.c const EventNotifier *vhost_svq_get_dev_kick_notifier( · line 67
Quoted 10 lines
hw/virtio/vhost-shadow-virtqueue.c +10
-0+ +for (uint64_t b =VIRTIO_TRANSPORT_F_START ; b <=VIRTIO_TRANSPORT_F_END ; + ++b) { +switch (b) { +case VIRTIO_F_NOTIFY_ON_EMPTY : +case VIRTIO_F_ANY_LAYOUT : +continue ; + +case VIRTIO_F_ACCESS_PLATFORM : +/* SVQ does not know how to translate addresses */
I may miss something but any reason that we need to disable ACCESS_PLATFORM? I'd expect the vring helper we used for shadow virtqueue can deal with vIOMMU perfectly.
hw/virtio/vhost-shadow-virtqueue.c const EventNotifier *vhost_svq_get_dev_kick_notifier( · line 74
Quoted 7 lines
hw/virtio/vhost-shadow-virtqueue.c +7
-0+if (*dev_features &BIT_ULL (b)) { +clear_bit (b, dev_features); + r = false; + } +break ; + +case VIRTIO_F_VERSION_1 :
I had the same question here.
Thanks
Quoted 46 lines
hw/virtio/vhost-shadow-virtqueue.c +17
-0+/* SVQ trust that guest vring is little endian */ +if (!(*dev_features &BIT_ULL (b))) { +set_bit (b, dev_features); + r = false; + } +continue ; + +default : +if (*dev_features &BIT_ULL (b)) { +clear_bit (b, dev_features); + } + } + } + +return r; +} +/* Forward guest notifications */ static void vhost_handle_guest_kick (EventNotifier *n) {hw/virtio/vhost-vdpa.c +14
-0@@ -855,10 +855,31 @@ static int vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v,size_t n_svqs = v->shadow_vqs_enabled ? hdev->nvqs :0 ;g_autoptr (GPtrArray) shadow_vqs =g_ptr_array_new_full (n_svqs, vhost_psvq_free); +uint64_t dev_features; +uint64_t svq_features; +int r; +bool ok; +if (!v->shadow_vqs_enabled) { goto out; } + r =vhost_vdpa_get_features (hdev, &dev_features); +if (r !=0 ) { +error_setg (errp,"Can't get vdpa device features, got (%d)" , r); +return r; + } + + svq_features = dev_features; + ok =vhost_svq_valid_device_features (&svq_features); +if (unlikely (!ok)) {
hw/virtio/vhost-vdpa.c static int vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v, · line 885
Quoted 10 lines
hw/virtio/vhost-vdpa.c +7
-0+error_setg (errp, +"SVQ Invalid device feature flags, offer: 0x%" PRIx64", ok: 0x%" PRIx64, + hdev->features, svq_features); +return -1 ; + } + + shadow_vqs =g_ptr_array_new_full (hdev->nvqs, vhost_psvq_free);for (unsigned n =0 ; n < hdev->nvqs; ++n) {VhostShadowVirtqueue *svq =vhost_svq_new ();
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH 15/31] vdpa: Add vhost_svq_get_num
In reply to Eugenio PérezQuoted 2 lines · Eugenio Pérez
This reports the guest's visible SVQ effective length, not the device's one.
I think we need to explain if there could be a case that the SVQ size is not equal to the device queue size.
Thanks
Quoted 25 lines
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-shadow-virtqueue.h | 1 + hw/virtio/vhost-shadow-virtqueue.c | 5 +++++ 2 files changed, 6 insertions(+)hw/virtio/vhost-shadow-virtqueue.h +1
-0@@ -29,6 +29,7 @@ const EventNotifier *vhost_svq_get_svq_call_notifier( const VhostShadowVirtqueue *svq);void vhost_svq_get_vring_addr (const VhostShadowVirtqueue *svq,struct vhost_vring_addr *addr); +uint16_t vhost_svq_get_num (const VhostShadowVirtqueue *svq);size_t vhost_svq_driver_area_size (const VhostShadowVirtqueue *svq);size_t vhost_svq_device_area_size (const VhostShadowVirtqueue *svq);hw/virtio/vhost-shadow-virtqueue.c +0
-0@@ -212,6 +212,11 @@ void vhost_svq_get_vring_addr(const VhostShadowVirtqueue *svq, addr->used_user_addr = (uint64_t )svq->vring.used;
hw/virtio/vhost-shadow-virtqueue.c void vhost_svq_get_vring_addr(const VhostShadowVirtqueue *svq, · line 222
Quoted 10 lines
hw/virtio/vhost-shadow-virtqueue.c +5
-0} +uint16_t vhost_svq_get_num (const VhostShadowVirtqueue *svq) +{ +return svq->vring.num; +} +size_t vhost_svq_driver_area_size (const VhostShadowVirtqueue *svq) {size_t desc_size =sizeof (vring_desc_t) * svq->vring.num;
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH 16/31] vhost: pass queue index to vhost_vq_get_addr
In reply to Eugenio PérezQuoted 25 lines · Eugenio Pérez
Doing that way allows vhost backend to know what address to return.
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)hw/virtio/vhost.c +3
-2@@ -798,9 +798,10 @@ static int vhost_virtqueue_set_addr(struct vhost_dev *dev,struct vhost_virtqueue *vq, unsigned idx, bool enable_log) { - struct vhost_vring_addr addr; + struct vhost_vring_addr addr = { + .index = idx, + };int r; -memset (&addr,0 ,sizeof (struct vhost_vring_addr ));if (dev->vhost_ops->vhost_vq_get_addr) { r = dev->vhost_ops->vhost_vq_get_addr (dev, &addr, vq);
hw/virtio/vhost.c static int vhost_virtqueue_set_addr(struct vhost_dev *dev, · line 816
Quoted 3 lines
hw/virtio/vhost.c +0
-0@@ -813,7 +814,6 @@ static int vhost_virtqueue_set_addr(struct vhost_dev *dev, addr.avail_user_addr = (uint64_t )(unsigned long )vq->avail; addr.used_user_addr = (uint64_t )(unsigned long )vq->used; }
I'm a bit lost in the logic above, any reason we need call vhost_vq_get_addr() :) ?
Thanks
hw/virtio/vhost.c static int vhost_virtqueue_set_addr(struct vhost_dev *dev, · line 819
Quoted 4 lines
hw/virtio/vhost.c +0
-1- addr.index = idx; addr.log_guest_addr = vq->used_phys; addr.flags = enable_log ? (1 <<VHOST_VRING_F_LOG ) :0 ; r = dev->vhost_ops->vhost_set_vring_addr (dev, &addr);
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Eugenio Perez Martin
maintainerRe: [PATCH 07/31] vhost: dd vhost_svq_get_svq_call_notifier
In reply to Jason WangQuoted 10 lines · Jason Wang
Quoted 3 lines · Eugenio Pérez
This allows vhost-vdpa device to retrieve device -> svq call eventfd.
Signed-off-by Eugenio Pérez <[email protected]>What did 'dd' mean in the title?
It was intended to be "add" but I missed the first letter. I will fix for the next version.
Thanks!
Quoted 45 lines
Thanks
Quoted 41 lines
--- hw/virtio/vhost-shadow-virtqueue.h | 2 ++ hw/virtio/vhost-shadow-virtqueue.c | 12 ++++++++++++ 2 files changed, 14 insertions(+)hw/virtio/vhost-shadow-virtqueue.h +2
-0@@ -18,6 +18,8 @@ typedef struct VhostShadowVirtqueue VhostShadowVirtqueue;void vhost_svq_set_svq_kick_fd (VhostShadowVirtqueue *svq,int svq_kick_fd);const EventNotifier *vhost_svq_get_dev_kick_notifier (const VhostShadowVirtqueue *svq); +const EventNotifier *vhost_svq_get_svq_call_notifier ( +const VhostShadowVirtqueue *svq);void vhost_svq_stop (VhostShadowVirtqueue *svq);hw/virtio/vhost-shadow-virtqueue.c +12
-0@@ -55,6 +55,18 @@ static void vhost_handle_guest_kick(EventNotifier *n)event_notifier_set (&svq->hdev_kick); } +/** +* Obtain the SVQ call notifier, where vhost device notifies SVQ that there +* exists pending used buffers. +* +* @svq Shadow Virtqueue +*/ +const EventNotifier *vhost_svq_get_svq_call_notifier ( +const VhostShadowVirtqueue *svq) +{ +return &svq->hdev_call; +} +/** * Set a new file descriptor for the guest to kick SVQ and notify for avail *
Re: [PATCH 17/31] vdpa: adapt vhost_ops callbacks to svq
In reply to Eugenio PérezQuoted 49 lines · Eugenio Pérez
First half of the buffers forwarding part, preparing vhost-vdpa callbacks to SVQ to offer it. QEMU cannot enable it at this moment, so this is effectively dead code at the moment, but it helps to reduce patch size.
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-shadow-virtqueue.h | 2 +- hw/virtio/vhost-shadow-virtqueue.c | 21 ++++- hw/virtio/vhost-vdpa.c | 133 ++++++++++++++++++++++++++--- 3 files changed, 143 insertions(+), 13 deletions(-)hw/virtio/vhost-shadow-virtqueue.h +1
-1@@ -35,7 +35,7 @@ size_t vhost_svq_device_area_size(const VhostShadowVirtqueue *svq);void vhost_svq_stop (VhostShadowVirtqueue *svq); -VhostShadowVirtqueue *vhost_svq_new (void ); +VhostShadowVirtqueue *vhost_svq_new (uint16_t qsize);void vhost_svq_free (VhostShadowVirtqueue *vq);hw/virtio/vhost-shadow-virtqueue.c +9
-1@@ -277,9 +277,17 @@ void vhost_svq_stop(VhostShadowVirtqueue *svq)/** * Creates vhost shadow virtqueue, and instruct vhost device to use the shadow * methods and file descriptors. +* +* @qsize Shadow VirtQueue size +* +* Returns the new virtqueue or NULL. +* +* In case of error, reason is reported through error_report. */ -VhostShadowVirtqueue *vhost_svq_new (void ) +VhostShadowVirtqueue *vhost_svq_new (uint16_t qsize) { +size_t desc_size =sizeof (vring_desc_t) * qsize; +size_t device_size, driver_size; g_autofree VhostShadowVirtqueue *svq = g_new0(VhostShadowVirtqueue, 1);int r;
hw/virtio/vhost-shadow-virtqueue.c VhostShadowVirtqueue *vhost_svq_new(void) · line 311
Quoted 4 lines
hw/virtio/vhost-shadow-virtqueue.c +1
-0@@ -300,6 +308,15 @@ VhostShadowVirtqueue *vhost_svq_new(void)/* Placeholder descriptor, it should be deleted at set_kick_fd */ event_notifier_init_fd (&svq->svq_kick,INVALID_SVQ_KICK_FD ); + svq->vring.num = qsize;
I wonder if this is the best. E.g some hardware can support up to 32K queue size. So this will probably end up with:
1) SVQ use 32K queue size
2) hardware queue uses 256
? Or we SVQ can stick to 256 but this will this cause trouble if we want to add event index support?
Quoted 94 lines
hw/virtio/vhost-shadow-virtqueue.c +10
-0+ driver_size =vhost_svq_driver_area_size (svq); + device_size =vhost_svq_device_area_size (svq); + svq->vring.desc =qemu_memalign (qemu_real_host_page_size, driver_size); + svq->vring.avail = (void *)((char *)svq->vring.desc + desc_size); +memset (svq->vring.desc,0 , driver_size); + svq->vring.used =qemu_memalign (qemu_real_host_page_size, device_size); +memset (svq->vring.used,0 , device_size); +event_notifier_set_handler (&svq->hdev_call, vhost_svq_handle_call);return g_steal_pointer (&svq);@@ -318,5 +335,7 @@ void vhost_svq_free(VhostShadowVirtqueue *vq)event_notifier_cleanup (&vq->hdev_kick);event_notifier_set_handler (&vq->hdev_call,NULL );event_notifier_cleanup (&vq->hdev_call); +qemu_vfree (vq->vring.desc); +qemu_vfree (vq->vring.used);g_free (vq); }hw/virtio/vhost-vdpa.c +38
-6@@ -641,20 +641,52 @@ static int vhost_vdpa_set_vring_addr(struct vhost_dev *dev,return vhost_vdpa_call (dev,VHOST_SET_VRING_ADDR , addr); } -static int vhost_vdpa_set_vring_num (struct vhost_dev *dev, -struct vhost_vring_state *ring) +static int vhost_vdpa_set_dev_vring_num (struct vhost_dev *dev, +struct vhost_vring_state *ring) {trace_vhost_vdpa_set_vring_num (dev, ring->index, ring->num);return vhost_vdpa_call (dev,VHOST_SET_VRING_NUM , ring); } -static int vhost_vdpa_set_vring_base (struct vhost_dev *dev, -struct vhost_vring_state *ring) +static int vhost_vdpa_set_vring_num (struct vhost_dev *dev, +struct vhost_vring_state *ring) +{ +struct vhost_vdpa *v = dev->opaque; + +if (v->shadow_vqs_enabled) { +/* +* Vring num was set at device start. SVQ num is handled by VirtQueue +* code +*/ +return 0 ; + } + +return vhost_vdpa_set_dev_vring_num (dev, ring); +} + +static int vhost_vdpa_set_dev_vring_base (struct vhost_dev *dev, +struct vhost_vring_state *ring) {trace_vhost_vdpa_set_vring_base (dev, ring->index, ring->num);return vhost_vdpa_call (dev,VHOST_SET_VRING_BASE , ring); } +static int vhost_vdpa_set_vring_base (struct vhost_dev *dev, +struct vhost_vring_state *ring) +{ +struct vhost_vdpa *v = dev->opaque; + +if (v->shadow_vqs_enabled) { +/* +* Vring base was set at device start. SVQ base is handled by VirtQueue +* code +*/ +return 0 ; + } + +return vhost_vdpa_set_dev_vring_base (dev, ring); +} +static int vhost_vdpa_get_vring_base (struct vhost_dev *dev,struct vhost_vring_state *ring) {@@ -784,8 +816,8 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started) } } -static int vhost_vdpa_get_features (struct vhost_dev *dev, -uint64_t *features) +static int vhost_vdpa_get_dev_features (struct vhost_dev *dev, +uint64_t *features) {int ret;@@ -794,15 +826,64 @@ static int vhost_vdpa_get_features(struct vhost_dev *dev,return ret; }
hw/virtio/vhost-vdpa.c static int vhost_vdpa_get_features(struct vhost_dev *dev, · line 837
Quoted 10 lines
hw/virtio/vhost-vdpa.c +9
-0+static int vhost_vdpa_get_features (struct vhost_dev *dev,uint64_t *features) +{ +struct vhost_vdpa *v = dev->opaque; +int ret =vhost_vdpa_get_dev_features (dev, features); + +if (ret ==0 && v->shadow_vqs_enabled) { +/* Filter only features that SVQ can offer to guest */ +vhost_svq_valid_guest_features (features); + }
Sorry if I've asked before, I think it's sufficient to filter out the device features that we don't support during and fail the vhost initialization. Any reason we need do it again here?
Quoted 118 lines
hw/virtio/vhost-vdpa.c +69
-4+ +return ret; +} +static int vhost_vdpa_set_features (struct vhost_dev *dev,uint64_t features) { +struct vhost_vdpa *v = dev->opaque;int ret;if (vhost_vdpa_one_time_request (dev)) {return 0 ; } +if (v->shadow_vqs_enabled) { +uint64_t dev_features, svq_features, acked_features; +bool ok; + + ret =vhost_vdpa_get_dev_features (dev, &dev_features); +if (ret !=0 ) { +error_report ("Can't get vdpa device features, got (%d)" , ret); +return ret; + } + + svq_features = dev_features; + ok =vhost_svq_valid_device_features (&svq_features); +if (unlikely (!ok)) { +error_report ("SVQ Invalid device feature flags, offer: 0x%" + PRIx64", ok: 0x%" PRIx64, dev->features, svq_features); +return -1 ; + } + + ok =vhost_svq_valid_guest_features (&features); +if (unlikely (!ok)) { +error_report ( +"Invalid guest acked feature flag, acked: 0x%" + PRIx64", ok: 0x%" PRIx64, dev->acked_features, features); +return -1 ; + } + + ok =vhost_svq_ack_guest_features (svq_features, features, + &acked_features); +if (unlikely (!ok)) { +return -1 ; + } + + features = acked_features; + } +trace_vhost_vdpa_set_features (dev, features); ret =vhost_vdpa_call (dev,VHOST_SET_FEATURES , &features);if (ret) {@@ -822,13 +903,31 @@ static int vhost_vdpa_set_owner(struct vhost_dev *dev)return vhost_vdpa_call (dev,VHOST_SET_OWNER ,NULL ); } -static int vhost_vdpa_vq_get_addr (struct vhost_dev *dev, -struct vhost_vring_addr *addr,struct vhost_virtqueue *vq) +static void vhost_vdpa_vq_get_guest_addr (struct vhost_vring_addr *addr, +struct vhost_virtqueue *vq) { -assert (dev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_VDPA ); addr->desc_user_addr = (uint64_t )(unsigned long )vq->desc_phys; addr->avail_user_addr = (uint64_t )(unsigned long )vq->avail_phys; addr->used_user_addr = (uint64_t )(unsigned long )vq->used_phys; +} + +static int vhost_vdpa_vq_get_addr (struct vhost_dev *dev, +struct vhost_vring_addr *addr, +struct vhost_virtqueue *vq) +{ +struct vhost_vdpa *v = dev->opaque; + +assert (dev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_VDPA ); + +if (v->shadow_vqs_enabled) { +int idx =vhost_vdpa_get_vq_index (dev, addr->index); +VhostShadowVirtqueue *svq =g_ptr_array_index (v->shadow_vqs, idx); + +vhost_svq_get_vring_addr (svq, addr); + }else { +vhost_vdpa_vq_get_guest_addr (addr, vq); + } +trace_vhost_vdpa_vq_get_addr (dev, vq, addr->desc_user_addr, addr->avail_user_addr, addr->used_user_addr);return 0 ;@@ -849,6 +948,12 @@ static void vhost_psvq_free(gpointer svq)vhost_svq_free (svq); } +static int vhost_vdpa_get_max_queue_size (struct vhost_dev *dev, +uint16_t *qsize) +{ +return vhost_vdpa_call (dev,VHOST_VDPA_GET_VRING_NUM , qsize); +} +static int vhost_vdpa_init_svq (struct vhost_dev *hdev,struct vhost_vdpa *v,Error **errp) {@@ -857,6 +962,7 @@ static int vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v, vhost_psvq_free);uint64_t dev_features;uint64_t svq_features; +uint16_t qsize;int r;bool ok;@@ -864,7 +970,7 @@ static int vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v, goto out; } - r =vhost_vdpa_get_features (hdev, &dev_features); + r =vhost_vdpa_get_dev_features (hdev, &dev_features);if (r !=0 ) {error_setg (errp,"Can't get vdpa device features, got (%d)" , r);return r;
hw/virtio/vhost-vdpa.c static int vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v, · line 991
Quoted 7 lines
hw/virtio/vhost-vdpa.c +4
-0@@ -879,9 +985,14 @@ static int vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v,return -1 ; } + r =vhost_vdpa_get_max_queue_size (hdev, &qsize); +if (unlikely (r)) { + qsize =256 ; + }
Should we fail instead of having a "default" value here?
Thanks
hw/virtio/vhost-vdpa.c static int vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v, · line 998
Quoted 8 lines
hw/virtio/vhost-vdpa.c +2
-1+ shadow_vqs =g_ptr_array_new_full (hdev->nvqs, vhost_psvq_free);for (unsigned n =0 ; n < hdev->nvqs; ++n) { -VhostShadowVirtqueue *svq =vhost_svq_new (); +VhostShadowVirtqueue *svq =vhost_svq_new (qsize);if (unlikely (!svq)) {error_setg (errp,"Cannot create svq %u" , n);
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH 18/31] vhost: Shadow virtqueue buffers forwarding
In reply to Eugenio PérezQuoted 156 lines · Eugenio Pérez
Initial version of shadow virtqueue that actually forward buffers. There is no iommu support at the moment, and that will be addressed in future patches of this series. Since all vhost-vdpa devices use forced IOMMU, this means that SVQ is not usable at this point of the series on any device.
For simplicity it only supports modern devices, that expects vring in little endian, with split ring and no event idx or indirect descriptors. Support for them will not be added in this series.
It reuses the VirtQueue code for the device part. The driver part is based on Linux's
virtio_ringdriver, but with stripped functionality and optimizations so it's easier to review.However, forwarding buffers have some particular pieces: One of the most unexpected ones is that a guest's buffer can expand through more than one descriptor in SVQ. While this is handled gracefully by qemu's emulated virtio devices, it may cause unexpected SVQ queue full. This patch also solves it by checking for this condition at both guest's kicks and device's calls. The code may be more elegant in the future if SVQ code runs in its own iocontext.
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-shadow-virtqueue.h | 2 + hw/virtio/vhost-shadow-virtqueue.c | 365 ++++++++++++++++++++++++++++- hw/virtio/vhost-vdpa.c | 111 ++++++++- 3 files changed, 462 insertions(+), 16 deletions(-)hw/virtio/vhost-shadow-virtqueue.h +2
-0@@ -33,6 +33,8 @@ uint16_t vhost_svq_get_num(const VhostShadowVirtqueue *svq);size_t vhost_svq_driver_area_size (const VhostShadowVirtqueue *svq);size_t vhost_svq_device_area_size (const VhostShadowVirtqueue *svq); +void vhost_svq_start (VhostShadowVirtqueue *svq,VirtIODevice *vdev, +VirtQueue *vq);void vhost_svq_stop (VhostShadowVirtqueue *svq);VhostShadowVirtqueue *vhost_svq_new (uint16_t qsize);hw/virtio/vhost-shadow-virtqueue.c +85
-5@@ -9,6 +9,8 @@#include "qemu/osdep.h" #include "hw/virtio/vhost-shadow-virtqueue.h" +#include "hw/virtio/vhost.h" +#include "hw/virtio/virtio-access.h" #include "standard-headers/linux/vhost_types.h" #include "qemu/error-report.h" @@ -36,6 +38,33 @@ typedef struct VhostShadowVirtqueue {/* Guest's call notifier, where SVQ calls guest. */ EventNotifier svq_call; + +/* Virtio queue shadowing */ +VirtQueue *vq; + +/* Virtio device */ +VirtIODevice *vdev; + +/* Map for returning guest's descriptors */ +VirtQueueElement **ring_id_maps; + +/* Next VirtQueue element that guest made available */ +VirtQueueElement *next_guest_avail_elem; + +/* Next head to expose to device */ +uint16_t avail_idx_shadow; + +/* Next free descriptor */ +uint16_t free_head; + +/* Last seen used idx */ +uint16_t shadow_used_idx; + +/* Next head to consume from device */ +uint16_t last_used_idx; + +/* Cache for the exposed notification flag */ +bool notification; } VhostShadowVirtqueue;#define INVALID_SVQ_KICK_FD -1@@ -148,30 +177,294 @@ bool vhost_svq_ack_guest_features(uint64_t dev_features,return true; } -/* Forward guest notifications */ -static void vhost_handle_guest_kick (EventNotifier *n) +/** +* Number of descriptors that SVQ can make available from the guest. +* +* @svq The svq +*/ +static uint16_t vhost_svq_available_slots (const VhostShadowVirtqueue *svq) { -VhostShadowVirtqueue *svq =container_of (n, VhostShadowVirtqueue, - svq_kick); +return svq->vring.num - (svq->avail_idx_shadow - svq->shadow_used_idx); +} + +static void vhost_svq_set_notification (VhostShadowVirtqueue *svq,bool enable) +{ +uint16_t notification_flag; -if (unlikely (!event_notifier_test_and_clear (n))) { +if (svq->notification == enable) { +return ; + } + + notification_flag =cpu_to_le16 (VRING_AVAIL_F_NO_INTERRUPT ); + + svq->notification = enable; +if (enable) { + svq->vring.avail->flags &= ~notification_flag; + }else { + svq->vring.avail->flags |= notification_flag; + } +} + +static void vhost_vring_write_descs (VhostShadowVirtqueue *svq, +const struct iovec *iovec, +size_t num,bool more_descs,bool write) +{ +uint16_t i = svq->free_head, last = svq->free_head; +unsigned n; +uint16_t flags = write ?cpu_to_le16 (VRING_DESC_F_WRITE ) :0 ; +vring_desc_t *descs = svq->vring.desc; + +if (num ==0 ) { +return ; + } + +for (n =0 ; n < num; n++) { +if (more_descs || (n +1 < num)) { + descs[i].flags = flags |cpu_to_le16 (VRING_DESC_F_NEXT ); + }else { + descs[i].flags = flags; + } + descs[i].addr =cpu_to_le64 ((hwaddr )iovec[n].iov_base); + descs[i].len =cpu_to_le32 (iovec[n].iov_len); + + last = i; + i =cpu_to_le16 (descs[i].next); + } + + svq->free_head =le16_to_cpu (descs[last].next); +} + +static unsigned vhost_svq_add_split(VhostShadowVirtqueue *svq,
hw/virtio/vhost-shadow-virtqueue.c bool vhost_svq_ack_guest_features(uint64_t dev_features, · line 247
Quoted 10 lines
hw/virtio/vhost-shadow-virtqueue.c +10
-0+ VirtQueueElement *elem) +{ +int head; +unsigned avail_idx; +vring_avail_t *avail = svq->vring.avail; + + head = svq->free_head; + +/* We need some descriptors here */ +assert (elem->out_num || elem->in_num);
Looks like this could be triggered by guest, we need fail instead assert here.
Quoted 51 lines
hw/virtio/vhost-shadow-virtqueue.c +44
-1+ +vhost_vring_write_descs (svq, elem->out_sg, elem->out_num, + elem->in_num >0 , false); +vhost_vring_write_descs (svq, elem->in_sg, elem->in_num, false, true); + +/* +* Put entry in available array (but don't update avail->idx until they +* do sync). +*/ + avail_idx = svq->avail_idx_shadow & (svq->vring.num -1 ); + avail->ring[avail_idx] =cpu_to_le16 (head); + svq->avail_idx_shadow++; + +/* Update avail index after the descriptor is wrote */ +smp_wmb (); + avail->idx =cpu_to_le16 (svq->avail_idx_shadow); + +return head; +} + +static void vhost_svq_add (VhostShadowVirtqueue *svq,VirtQueueElement *elem) +{ +unsigned qemu_head =vhost_svq_add_split (svq, elem); + + svq->ring_id_maps[qemu_head] = elem; +} + +static void vhost_svq_kick (VhostShadowVirtqueue *svq) +{ +/* We need to expose available array entries before checking used flags */ +smp_mb (); +if (svq->vring.used->flags &VRING_USED_F_NO_NOTIFY ) {return ; }event_notifier_set (&svq->hdev_kick); } -/* Forward vhost notifications */ +/** +* Forward available buffers. +* +* @svq Shadow VirtQueue +* +* Note that this function does not guarantee that all guest's available +* buffers are available to the device in SVQ avail ring. The guest may have +* exposed a GPA / GIOVA congiuous buffer, but it may not be contiguous in qemu +* vaddr. +* +* If that happens, guest's kick notifications will be disabled until device +* makes some buffers used.
hw/virtio/vhost-shadow-virtqueue.c bool vhost_svq_ack_guest_features(uint64_t dev_features, · line 307
Quoted 10 lines
hw/virtio/vhost-shadow-virtqueue.c +10
-0+*/ +static void vhost_handle_guest_kick (VhostShadowVirtqueue *svq) +{ +/* Clear event notifier */ +event_notifier_test_and_clear (&svq->svq_kick); + +/* Make available as many buffers as possible */ + do { + if (virtio_queue_get_notification(svq->vq)) { + virtio_queue_set_notification(svq->vq, false);
This looks like an optimization the should belong to virtio_queue_set_notification() itself.
Quoted 113 lines
hw/virtio/vhost-shadow-virtqueue.c +113
-0+ } + +while (true) { +VirtQueueElement *elem; + +if (svq->next_guest_avail_elem) { + elem =g_steal_pointer (&svq->next_guest_avail_elem); + }else { + elem =virtqueue_pop (svq->vq,sizeof (*elem)); + } + +if (!elem) { +break ; + } + +if (elem->out_num + elem->in_num > +vhost_svq_available_slots (svq)) { +/* +* This condition is possible since a contiguous buffer in GPA +* does not imply a contiguous buffer in qemu's VA +* scatter-gather segments. If that happen, the buffer exposed +* to the device needs to be a chain of descriptors at this +* moment. +* +* SVQ cannot hold more available buffers if we are here: +* queue the current guest descriptor and ignore further kicks +* until some elements are used. +*/ + svq->next_guest_avail_elem = elem; +return ; + } + +vhost_svq_add (svq, elem); +vhost_svq_kick (svq); + } + +virtio_queue_set_notification (svq->vq, true); + }while (!virtio_queue_empty (svq->vq)); +} + +/** +* Handle guest's kick. +* +* @n guest kick event notifier, the one that guest set to notify svq. +*/ +static void vhost_handle_guest_kick_notifier (EventNotifier *n) +{ +VhostShadowVirtqueue *svq =container_of (n, VhostShadowVirtqueue, + svq_kick); +vhost_handle_guest_kick (svq); +} + +static bool vhost_svq_more_used (VhostShadowVirtqueue *svq) +{ +if (svq->last_used_idx != svq->shadow_used_idx) { +return true; + } + + svq->shadow_used_idx =cpu_to_le16 (svq->vring.used->idx); + +return svq->last_used_idx != svq->shadow_used_idx; +} + +static VirtQueueElement *vhost_svq_get_buf (VhostShadowVirtqueue *svq) +{ +vring_desc_t *descs = svq->vring.desc; +const vring_used_t *used = svq->vring.used; +vring_used_elem_t used_elem; +uint16_t last_used; + +if (!vhost_svq_more_used (svq)) { +return NULL ; + } + +/* Only get used array entries after they have been exposed by dev */ +smp_rmb (); + last_used = svq->last_used_idx & (svq->vring.num -1 ); + used_elem.id =le32_to_cpu (used->ring[last_used].id); + used_elem.len =le32_to_cpu (used->ring[last_used].len); + + svq->last_used_idx++; +if (unlikely (used_elem.id >= svq->vring.num)) { +error_report ("Device %s says index %u is used" , svq->vdev->name, + used_elem.id); +return NULL ; + } + +if (unlikely (!svq->ring_id_maps[used_elem.id])) { +error_report ( +"Device %s says index %u is used, but it was not available" , + svq->vdev->name, used_elem.id); +return NULL ; + } + + descs[used_elem.id].next = svq->free_head; + svq->free_head = used_elem.id; + + svq->ring_id_maps[used_elem.id]->len = used_elem.len; +return g_steal_pointer (&svq->ring_id_maps[used_elem.id]); +} + +static void vhost_svq_flush (VhostShadowVirtqueue *svq, +bool check_for_avail_queue) +{ +VirtQueue *vq = svq->vq; + +/* Make as many buffers as possible used. */ + do { + unsigned i = 0; + + vhost_svq_set_notification(svq, false); + while (true) { + g_autofree VirtQueueElement *elem = vhost_svq_get_buf(svq);
hw/virtio/vhost-shadow-virtqueue.c bool vhost_svq_ack_guest_features(uint64_t dev_features, · line 430
Quoted 10 lines
hw/virtio/vhost-shadow-virtqueue.c +10
-0+if (!elem) { +break ; + } + +if (unlikely (i >= svq->vring.num)) { +virtio_error (svq->vdev, +"More than %u used buffers obtained in a %u size SVQ" , + i, svq->vring.num); +virtqueue_fill (vq, elem, elem->len, i); +virtqueue_flush (vq, i);
Let's simply use virtqueue_push() here?
hw/virtio/vhost-shadow-virtqueue.c bool vhost_svq_ack_guest_features(uint64_t dev_features, · line 431
Quoted 1 line
hw/virtio/vhost-shadow-virtqueue.c +1
-0+ i =0 ;
Do we need to bail out here?
hw/virtio/vhost-shadow-virtqueue.c bool vhost_svq_ack_guest_features(uint64_t dev_features, · line 444
Quoted 13 lines
hw/virtio/vhost-shadow-virtqueue.c +13
-0+ } +virtqueue_fill (vq, elem, elem->len, i++); + } + +virtqueue_flush (vq, i); +event_notifier_set (&svq->svq_call); + +if (check_for_avail_queue && svq->next_guest_avail_elem) { +/* +* Avail ring was full when vhost_svq_flush was called, so it's a +* good moment to make more descriptors available if possible +*/ +vhost_handle_guest_kick (svq);
Is there better to have a similar check as vhost_handle_guest_kick() did?
if (elem->out_num + elem->in_num >
vhost_svq_available_slots(svq)) {
hw/virtio/vhost-shadow-virtqueue.c bool vhost_svq_ack_guest_features(uint64_t dev_features, · line 447
Quoted 3 lines
hw/virtio/vhost-shadow-virtqueue.c +3
-0+ } + +vhost_svq_set_notification (svq, true);
A mb() is needed here? Otherwise we may lost a call here (where vhost_svq_more_used() is run before vhost_svq_set_notification()).
Quoted 11 lines
hw/virtio/vhost-shadow-virtqueue.c +11
-0+ }while (vhost_svq_more_used (svq)); +} + +/** +* Forward used buffers. +* +* @n hdev call event notifier, the one that device set to notify svq. +* +* Note that we are not making any buffers available in the loop, there is no +* way that it runs more than virtqueue size times. +*/
hw/virtio/vhost-shadow-virtqueue.c bool vhost_svq_ack_guest_features(uint64_t dev_features, · line 465
Quoted 10 lines
hw/virtio/vhost-shadow-virtqueue.c +2
-3static void vhost_svq_handle_call (EventNotifier *n) {VhostShadowVirtqueue *svq =container_of (n, VhostShadowVirtqueue, hdev_call); -if (unlikely (!event_notifier_test_and_clear (n))) { -return ; - } +/* Clear event notifier */ +event_notifier_test_and_clear (n);
Any reason that we remove the above check?
Quoted 172 lines
hw/virtio/vhost-shadow-virtqueue.c +51
-3-event_notifier_set (&svq->svq_call); +vhost_svq_flush (svq, true); }/** @@ -258,13 +551,38 @@ void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd)* need to explicitely check for them. */ event_notifier_init_fd (&svq->svq_kick, svq_kick_fd); -event_notifier_set_handler (&svq->svq_kick, vhost_handle_guest_kick); +event_notifier_set_handler (&svq->svq_kick, + vhost_handle_guest_kick_notifier);if (!check_old ||event_notifier_test_and_clear (&tmp)) {event_notifier_set (&svq->hdev_kick); } } +/** +* Start shadow virtqueue operation. +* +* @svq Shadow Virtqueue +* @vdev VirtIO device +* @vq Virtqueue to shadow +*/ +void vhost_svq_start (VhostShadowVirtqueue *svq,VirtIODevice *vdev, +VirtQueue *vq) +{ + svq->next_guest_avail_elem =NULL ; + svq->avail_idx_shadow =0 ; + svq->shadow_used_idx =0 ; + svq->last_used_idx =0 ; + svq->vdev = vdev; + svq->vq = vq; + +memset (svq->vring.avail,0 ,sizeof (*svq->vring.avail)); +memset (svq->vring.used,0 ,sizeof (*svq->vring.avail)); +for (unsigned i =0 ; i < svq->vring.num -1 ; i++) { + svq->vring.desc[i].next =cpu_to_le16 (i +1 ); + } +} +/** * Stop shadow virtqueue operation. * @svq Shadow Virtqueue @@ -272,6 +590,28 @@ void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd)void vhost_svq_stop (VhostShadowVirtqueue *svq) {event_notifier_set_handler (&svq->svq_kick,NULL ); + g_autofree VirtQueueElement *next_avail_elem = NULL; + +if (!svq->vq) { +return ; + } + +/* Send all pending used descriptors to guest */ +vhost_svq_flush (svq, false); + +for (unsigned i =0 ; i < svq->vring.num; ++i) { + g_autofree VirtQueueElement *elem = NULL; + elem =g_steal_pointer (&svq->ring_id_maps[i]); +if (elem) { +virtqueue_detach_element (svq->vq, elem, elem->len); + } + } + + next_avail_elem =g_steal_pointer (&svq->next_guest_avail_elem); +if (next_avail_elem) { +virtqueue_detach_element (svq->vq, next_avail_elem, + next_avail_elem->len); + } }/** @@ -316,7 +656,7 @@ VhostShadowVirtqueue *vhost_svq_new(uint16_t qsize)memset (svq->vring.desc,0 , driver_size); svq->vring.used =qemu_memalign (qemu_real_host_page_size, device_size);memset (svq->vring.used,0 , device_size); - + svq->ring_id_maps = g_new0(VirtQueueElement *, qsize);event_notifier_set_handler (&svq->hdev_call, vhost_svq_handle_call);return g_steal_pointer (&svq);@@ -335,6 +675,7 @@ void vhost_svq_free(VhostShadowVirtqueue *vq)event_notifier_cleanup (&vq->hdev_kick);event_notifier_set_handler (&vq->hdev_call,NULL );event_notifier_cleanup (&vq->hdev_call); +g_free (vq->ring_id_maps);qemu_vfree (vq->vring.desc);qemu_vfree (vq->vring.used);g_free (vq);hw/virtio/vhost-vdpa.c +54
-4@@ -752,9 +752,9 @@ static int vhost_vdpa_set_vring_call(struct vhost_dev *dev,* Note that this function does not rewind kick file descriptor if cannot set * call one. */ -static bool vhost_vdpa_svq_setup (struct vhost_dev *dev, -VhostShadowVirtqueue *svq, -unsigned idx) +static int vhost_vdpa_svq_set_fds (struct vhost_dev *dev, +VhostShadowVirtqueue *svq, +unsigned idx) {struct vhost_vring_file file = { .index = dev->vq_index + idx,@@ -767,7 +767,7 @@ static bool vhost_vdpa_svq_setup(struct vhost_dev *dev, r =vhost_vdpa_set_vring_dev_kick (dev, &file);if (unlikely (r !=0 )) {error_report ("Can't set device kick fd (%d)" , -r); -return false; +return r; } event_notifier =vhost_svq_get_svq_call_notifier (svq);@@ -777,6 +777,99 @@ static bool vhost_vdpa_svq_setup(struct vhost_dev *dev,error_report ("Can't set device call fd (%d)" , -r); } +return r; +} + +/** +* Unmap SVQ area in the device +*/ +static bool vhost_vdpa_svq_unmap_ring (struct vhost_vdpa *v,hwaddr iova, +hwaddr size) +{ +int r; + + size =ROUND_UP (size, qemu_real_host_page_size); + r =vhost_vdpa_dma_unmap (v, iova, size); +return r ==0 ; +} + +static bool vhost_vdpa_svq_unmap_rings (struct vhost_dev *dev, +const VhostShadowVirtqueue *svq) +{ +struct vhost_vdpa *v = dev->opaque; +struct vhost_vring_addr svq_addr; +size_t device_size =vhost_svq_device_area_size (svq); +size_t driver_size =vhost_svq_driver_area_size (svq); +bool ok; + +vhost_svq_get_vring_addr (svq, &svq_addr); + + ok =vhost_vdpa_svq_unmap_ring (v, svq_addr.desc_user_addr, driver_size); +if (unlikely (!ok)) { +return false; + } + +return vhost_vdpa_svq_unmap_ring (v, svq_addr.used_user_addr, device_size); +} + +/** +* Map shadow virtqueue rings in device +* +* @dev The vhost device +* @svq The shadow virtqueue +*/ +static bool vhost_vdpa_svq_map_rings (struct vhost_dev *dev, +const VhostShadowVirtqueue *svq) +{ +struct vhost_vdpa *v = dev->opaque; +struct vhost_vring_addr svq_addr; +size_t device_size =vhost_svq_device_area_size (svq); +size_t driver_size =vhost_svq_driver_area_size (svq); +int r; +
hw/virtio/vhost-vdpa.c static bool vhost_vdpa_svq_setup(struct vhost_dev *dev, · line 839
Quoted 10 lines
hw/virtio/vhost-vdpa.c +10
-0+vhost_svq_get_vring_addr (svq, &svq_addr); + + r =vhost_vdpa_dma_map (v, svq_addr.desc_user_addr, driver_size, + (void *)svq_addr.desc_user_addr, true); +if (unlikely (r !=0 )) { +return false; + } + + r =vhost_vdpa_dma_map (v, svq_addr.used_user_addr, device_size, + (void *)svq_addr.used_user_addr, false);
Do we need unmap the driver area if we fail here?
Thanks
Quoted 51 lines
hw/virtio/vhost-vdpa.c +36
-0+return r ==0 ; +} + +static bool vhost_vdpa_svq_setup (struct vhost_dev *dev, +VhostShadowVirtqueue *svq, +unsigned idx) +{ +uint16_t vq_index = dev->vq_index + idx; +struct vhost_vring_state s = { + .index = vq_index, + }; +int r; +bool ok; + + r =vhost_vdpa_set_dev_vring_base (dev, &s); +if (unlikely (r)) { +error_report ("Can't set vring base (%d)" , r); +return false; + } + + s.num =vhost_svq_get_num (svq); + r =vhost_vdpa_set_dev_vring_num (dev, &s); +if (unlikely (r)) { +error_report ("Can't set vring num (%d)" , r); +return false; + } + + ok =vhost_vdpa_svq_map_rings (dev, svq); +if (unlikely (!ok)) { +return false; + } + + r =vhost_vdpa_svq_set_fds (dev, svq, idx);return r ==0 ; }@@ -788,14 +881,24 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)if (started) {vhost_vdpa_host_notifiers_init (dev);for (unsigned i =0 ; i < v->shadow_vqs->len; ++i) { +VirtQueue *vq =virtio_get_queue (dev->vdev, dev->vq_index + i);VhostShadowVirtqueue *svq =g_ptr_array_index (v->shadow_vqs, i);bool ok =vhost_vdpa_svq_setup (dev, svq, i);if (unlikely (!ok)) {return -1 ; } +vhost_svq_start (svq, dev->vdev, vq); }vhost_vdpa_set_vring_ready (dev); }else { +for (unsigned i =0 ; i < v->shadow_vqs->len; ++i) {
hw/virtio/vhost-vdpa.c static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started) · line 904
Quoted 10 lines
hw/virtio/vhost-vdpa.c +7
-0+VhostShadowVirtqueue *svq =g_ptr_array_index (v->shadow_vqs, + i); +bool ok =vhost_vdpa_svq_unmap_rings (dev, svq); +if (unlikely (!ok)) { +return -1 ; + } + }vhost_vdpa_host_notifiers_uninit (dev, dev->nvqs); }
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH 21/31] util: Add iova_tree_alloc
In reply to Eugenio Perez MartinQuoted 64 lines · Eugenio Perez Martin
Quoted 2 lines · Peter Xu
Quoted 1 line · Eugenio Pérez
+int iova_tree_alloc(IOVATree *tree, DMAMap *map, hwaddr
iova_begin,I forgot to s/iova_tree_alloc/iova_tree_alloc_map/ here.
Quoted 42 lines
Quoted 26 lines
util/iova-tree.c +26
-0+ hwaddr iova_last) +{ +const DMAMapInternal *last, *i; + +assert (iova_begin < iova_last); + +/* +* Find a valid hole for the mapping +* +* TODO: Replace all this with g_tree_node_first/next/last when available +* (from glib since 2.68). Using a sepparated QTAILQ complicates code. +* +* Try to allocate first at the end of the list. +*/ + last =QTAILQ_LAST (&tree->list); +if (iova_tree_alloc_map_in_hole (last,NULL , iova_begin, iova_last, + map->size)) { + goto alloc; + } + +/* Look for inner hole */ + last =NULL ; +for (i =QTAILQ_FIRST (&tree->list); i; + last = i, i =QTAILQ_NEXT (i, entry)) { +if (iova_tree_alloc_map_in_hole (last, i, iova_begin, iova_last, + map->size)) {util/iova-tree.c int iova_tree_remove(IOVATree *tree, const DMAMap *map) · line 247
Quoted 10 lines
util/iova-tree.c +10
-0+ goto alloc; + } + } + +return IOVA_ERR_NOMEM ; + +alloc: + map->iova = last ? last->map.iova + last->map.size +1 : iova_begin; +return iova_tree_insert (tree, map); +}Hi, Eugenio,
Have you tried with what Jason suggested previously?
https://lore.kernel.org/qemu-devel/CACGkMEtZAPd9xQTP_R4w296N_Qz7VuV1FLnb544fEVoYO0of+g@mail.gmail.com/Quoted 9 lines
That solution still sounds very sensible to me even without the newly introduced list in previous two patches.
IMHO we could move "DMAMap *previous, *this" into the IOVATreeAllocArgs* stucture that was passed into the traverse func though, so it'll naturally work with threading.
Or is there any blocker for it?
Hi Peter,
I can try that solution again, but the main problem was the special cases of the beginning and ending.
For the function to locate a hole, DMAMap first = {.iova = 0, .size = 0} means that it cannot account 0 for the hole.
Quoted 7 lines
In other words, with that algorithm, if the only valid hole is [0, N) and we try to allocate a block of size N, it would fail.
Same happens with
iova_end, although in practice it seems that IOMMU hardware iova upper limit is neverUINT64_MAX.Maybe we could treat .size = 0 as a special case?
Yes, the pseudo-code I past is just to show the idea of using g_tree_foreach() instead of introducing new auxiliary data structures. That will simplify both the codes and the reviewers.
Down the road, we may start from an iova range specified during the creation of the iova tree. E.g for vtd, it's the GAW, for vhost-vdpa, it's the one that we get from VHOST_VDPA_GET_IOVA_RANGE.
Thanks
Quoted 13 lines
I see cleaner either
to build the list (but insert needs to take the list into account) or to explicitly tell that prev == NULL means to useiova_first.Another solution that comes to my mind: to add both exceptions outside of transverse function, and skip the first iteration with something like:
if (prev == NULL) {
prev = this;
return false /* continue */
}
Quoted 9 lines
So the transverse callback has way less code paths. Would it work for you if I send a separate RFC from SVQ only to validate this?
Thanks!
Quoted 4 lines
Thanks,
--
Peter Xu
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH 22/31] vhost: Add VhostIOVATree
In reply to Eugenio PérezQuoted 4 lines · Eugenio Pérez
This tree is able to look for a translated address from an IOVA address.
At first glance it is similar to util/iova-tree. However, SVQ working on devices with limited IOVA space need more capabilities,
So did the IOVA tree (e.g l2 vtd can only work in the range of GAW and without RMRRs).
Quoted 2 lines
like allocating
IOVA chunks or performing reverse translations (qemu addresses to iova).
This looks like a general request as well. So I wonder if we can simply extend iova tree instead.
Thanks
Quoted 220 lines
The allocation capability, as "assign a free IOVA address to this chunk of memory in qemu's address space" allows shadow virtqueue to create a new address space that is not restricted by guest's addressable one, so we can allocate shadow vqs vrings outside of it.
It duplicates the tree so it can search efficiently both directions, and it will signal overlap if iova or the translated address is present in any tree.
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-iova-tree.h | 27 +++++++ hw/virtio/vhost-iova-tree.c | 157 ++++++++++++++++++++++++++++++++++++ hw/virtio/meson.build | 2 +- 3 files changed, 185 insertions(+), 1 deletion(-)create mode 100644
hw/virtio/vhost-iova-tree.h
create mode 100644hw/virtio/vhost-iova-tree.chw/virtio/vhost-iova-tree.h new file +27
-0@@ -0,0 +1,27 @@ +/* +* vhost software live migration ring +* +* SPDX-FileCopyrightText: Red Hat, Inc. 2021 +* SPDX-FileContributor: Author: Eugenio Pérez <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef HW_VIRTIO_VHOST_IOVA_TREE_H +#define HW_VIRTIO_VHOST_IOVA_TREE_H + +#include "qemu/iova-tree.h" +#include "exec/memory.h" + +typedef struct VhostIOVATree VhostIOVATree ; + +VhostIOVATree *vhost_iova_tree_new (uint64_t iova_first,uint64_t iova_last); +void vhost_iova_tree_delete (VhostIOVATree *iova_tree); +G_DEFINE_AUTOPTR_CLEANUP_FUNC (VhostIOVATree, vhost_iova_tree_delete); + +const DMAMap *vhost_iova_tree_find_iova (const VhostIOVATree *iova_tree, +const DMAMap *map); +int vhost_iova_tree_map_alloc (VhostIOVATree *iova_tree,DMAMap *map); +void vhost_iova_tree_remove (VhostIOVATree *iova_tree,const DMAMap *map); + +#endif hw/virtio/vhost-iova-tree.c new file +157
-0@@ -0,0 +1,157 @@ +/* +* vhost software live migration ring +* +* SPDX-FileCopyrightText: Red Hat, Inc. 2021 +* SPDX-FileContributor: Author: Eugenio Pérez <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "qemu/osdep.h" +#include "qemu/iova-tree.h" +#include "vhost-iova-tree.h" + +#define iova_min_addr qemu_real_host_page_size + +/** +* VhostIOVATree, able to: +* - Translate iova address +* - Reverse translate iova address (from translated to iova) +* - Allocate IOVA regions for translated range (potentially slow operation) +* +* Note that it cannot remove nodes. +*/ +struct VhostIOVATree { +/* First addresable iova address in the device */ +uint64_t iova_first; + +/* Last addressable iova address in the device */ +uint64_t iova_last; + +/* IOVA address to qemu memory maps. */ +IOVATree *iova_taddr_map; + +/* QEMU virtual memory address to iova maps */ +GTree *taddr_iova_map; +}; + +static gint vhost_iova_tree_cmp_taddr (gconstpointer a,gconstpointer b, +gpointer data) +{ +const DMAMap *m1 = a, *m2 = b; + +if (m1->translated_addr > m2->translated_addr + m2->size) { +return 1 ; + } + +if (m1->translated_addr + m1->size < m2->translated_addr) { +return -1 ; + } + +/* Overlapped */ +return 0 ; +} + +/** +* Create a new IOVA tree +* +* Returns the new IOVA tree +*/ +VhostIOVATree *vhost_iova_tree_new (hwaddr iova_first,hwaddr iova_last) +{ +VhostIOVATree *tree =g_new (VhostIOVATree,1 ); + +/* Some devices does not like 0 addresses */ + tree->iova_first =MAX (iova_first, iova_min_addr); + tree->iova_last = iova_last; + + tree->iova_taddr_map =iova_tree_new (); + tree->taddr_iova_map =g_tree_new_full (vhost_iova_tree_cmp_taddr,NULL , +NULL , g_free); +return tree; +} + +/** +* Delete an iova tree +*/ +void vhost_iova_tree_delete (VhostIOVATree *iova_tree) +{ +iova_tree_destroy (iova_tree->iova_taddr_map); +g_tree_unref (iova_tree->taddr_iova_map); +g_free (iova_tree); +} + +/** +* Find the IOVA address stored from a memory address +* +* @tree The iova tree +* @map The map with the memory address +* +* Return the stored mapping, or NULL if not found. +*/ +const DMAMap *vhost_iova_tree_find_iova (const VhostIOVATree *tree, +const DMAMap *map) +{ +return g_tree_lookup (tree->taddr_iova_map, map); +} + +/** +* Allocate a new mapping +* +* @tree The iova tree +* @map The iova map +* +* Returns: +* - IOVA_OK if the map fits in the container +* - IOVA_ERR_INVALID if the map does not make sense (like size overflow) +* - IOVA_ERR_OVERLAP if the tree already contains that map +* - IOVA_ERR_NOMEM if tree cannot allocate more space. +* +* It returns assignated iova in map->iova if return value is VHOST_DMA_MAP_OK. +*/ +int vhost_iova_tree_map_alloc (VhostIOVATree *tree,DMAMap *map) +{ +/* Some vhost devices does not like addr 0. Skip first page */ +hwaddr iova_first = tree->iova_first ?: qemu_real_host_page_size; +DMAMap *new; +int r; + +if (map->translated_addr + map->size < map->translated_addr || + map->perm ==IOMMU_NONE ) { +return IOVA_ERR_INVALID ; + } + +/* Check for collisions in translated addresses */ +if (vhost_iova_tree_find_iova (tree, map)) { +return IOVA_ERR_OVERLAP ; + } + +/* Allocate a node in IOVA address */ + r =iova_tree_alloc (tree->iova_taddr_map, map, iova_first, + tree->iova_last); +if (r !=IOVA_OK ) { +return r; + } + +/* Allocate node in qemu -> iova translations */ + new =g_malloc (sizeof (*new)); +memcpy (new, map,sizeof (*new)); +g_tree_insert (tree->taddr_iova_map, new, new); +return IOVA_OK ; +} + +/** +* Remove existing mappings from iova tree +* +* @param iova_tree The vhost iova tree +* @param map The map to remove +*/ +void vhost_iova_tree_remove (VhostIOVATree *iova_tree,const DMAMap *map) +{ +const DMAMap *overlap; + +iova_tree_remove (iova_tree->iova_taddr_map, map); +while ((overlap =vhost_iova_tree_find_iova (iova_tree, map))) { +g_tree_remove (iova_tree->taddr_iova_map, overlap); + } +}hw/virtio/meson.build +0
-0
hw/virtio/meson.build softmmu_ss.add(when: 'CONFIG_ALL', if_true: files('vhost-stub.c')) · line 17
Quoted 8 lines
hw/virtio/meson.build +1
-1@@ -11,7 +11,7 @@ softmmu_ss.add(when: 'CONFIG_ALL', if_true: files('vhost-stub.c')) virtio_ss = ss.source_set() virtio_ss.add(files('virtio.c')) -virtio_ss.add(when: 'CONFIG_VHOST', if_true: files('vhost.c', 'vhost-backend.c', 'vhost-shadow-virtqueue.c')) +virtio_ss.add(when: 'CONFIG_VHOST', if_true: files('vhost.c', 'vhost-backend.c', 'vhost-shadow-virtqueue.c', 'vhost-iova-tree.c')) virtio_ss.add(when: 'CONFIG_VHOST_USER', if_true: files('vhost-user.c')) virtio_ss.add(when: 'CONFIG_VHOST_VDPA', if_true: files('vhost-vdpa.c')) virtio_ss.add(when: 'CONFIG_VIRTIO_BALLOON', if_true: files('virtio-balloon.c'))
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH 23/31] vdpa: Add custom IOTLB translations to SVQ
In reply to Eugenio PérezQuoted 121 lines · Eugenio Pérez
Use translations added in VhostIOVATree in SVQ.
Only introduce usage here, not allocation and deallocation. As with previous patches, we use the dead code paths of
shadow_vqs_enabledto avoid commiting too many changes at once. These are impossible to take at the moment.Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-shadow-virtqueue.h | 3 +- include/hw/virtio/vhost-vdpa.h | 3 + hw/virtio/vhost-shadow-virtqueue.c | 111 ++++++++++++++++---- hw/virtio/vhost-vdpa.c | 161 +++++++++++++++++++++++++---- 4 files changed, 238 insertions(+), 40 deletions(-)hw/virtio/vhost-shadow-virtqueue.h +2
-1@@ -12,6 +12,7 @@#include "hw/virtio/vhost.h" #include "qemu/event_notifier.h" +#include "hw/virtio/vhost-iova-tree.h" typedef struct VhostShadowVirtqueue VhostShadowVirtqueue ;@@ -37,7 +38,7 @@ void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIODevice *vdev, VirtQueue *vq);void vhost_svq_stop (VhostShadowVirtqueue *svq); -VhostShadowVirtqueue *vhost_svq_new (uint16_t qsize); +VhostShadowVirtqueue *vhost_svq_new (uint16_t qsize,VhostIOVATree *iova_map);void vhost_svq_free (VhostShadowVirtqueue *vq);include/hw/virtio/vhost-vdpa.h +3
-0@@ -14,6 +14,7 @@#include <gmodule.h> +#include "hw/virtio/vhost-iova-tree.h" #include "hw/virtio/virtio.h" #include "standard-headers/linux/vhost_types.h" @@ -30,6 +31,8 @@ typedef struct vhost_vdpa {MemoryListener listener;struct vhost_vdpa_iova_range iova_range;bool shadow_vqs_enabled; +/* IOVA mapping used by Shadow Virtqueue */ +VhostIOVATree *iova_tree;GPtrArray *shadow_vqs;struct vhost_dev *dev;VhostVDPAHostNotifier notifier[VIRTIO_QUEUE_MAX ];hw/virtio/vhost-shadow-virtqueue.c +28
-7@@ -11,6 +11,7 @@#include "hw/virtio/vhost-shadow-virtqueue.h" #include "hw/virtio/vhost.h" #include "hw/virtio/virtio-access.h" +#include "hw/virtio/vhost-iova-tree.h" #include "standard-headers/linux/vhost_types.h" #include "qemu/error-report.h" @@ -45,6 +46,9 @@ typedef struct VhostShadowVirtqueue {/* Virtio device */ VirtIODevice *vdev; +/* IOVA mapping */ +VhostIOVATree *iova_tree; +/* Map for returning guest's descriptors */ VirtQueueElement **ring_id_maps;@@ -97,13 +101,7 @@ bool vhost_svq_valid_device_features(uint64_t *dev_features)continue ;case VIRTIO_F_ACCESS_PLATFORM : -/* SVQ does not know how to translate addresses */ -if (*dev_features &BIT_ULL (b)) { -clear_bit (b, dev_features); - r = false; - } -break ; - +/* SVQ trust in host's IOMMU to translate addresses */ case VIRTIO_F_VERSION_1 :/* SVQ trust that guest vring is little endian */ if (!(*dev_features &BIT_ULL (b))) {@@ -205,7 +203,55 @@ static void vhost_svq_set_notification(VhostShadowVirtqueue *svq, bool enable) } } +/** +* Translate addresses between qemu's virtual address and SVQ IOVA +* +* @svq Shadow VirtQueue +* @vaddr Translated IOVA addresses +* @iovec Source qemu's VA addresses +* @num Length of iovec and minimum length of vaddr +*/ +static bool vhost_svq_translate_addr (const VhostShadowVirtqueue *svq, +void **addrs,const struct iovec *iovec, +size_t num) +{ +size_t i; + +if (num ==0 ) { +return true; + } + +for (i =0 ; i < num; ++i) { +DMAMap needle = { + .translated_addr = (hwaddr )iovec[i].iov_base, + .size = iovec[i].iov_len, + };
hw/virtio/vhost-shadow-virtqueue.c static void vhost_svq_set_notification(VhostShadowVirtqueue *svq, bool enable) · line 238
Quoted 10 lines
hw/virtio/vhost-shadow-virtqueue.c +10
-0+size_t off; + +const DMAMap *map =vhost_iova_tree_find_iova (svq->iova_tree, &needle); +/* +* Map cannot be NULL since iova map contains all guest space and +* qemu already has a physical address mapped +*/ +if (unlikely (!map)) { +error_report ("Invalid address 0x%" HWADDR_PRIx" given by guest" , + needle.translated_addr);
This can be triggered by guest, we need use once or log_guest_error() etc.
hw/virtio/vhost-shadow-virtqueue.c static void vhost_svq_set_notification(VhostShadowVirtqueue *svq, bool enable) · line 245
Quoted 7 lines
hw/virtio/vhost-shadow-virtqueue.c +7
-0+return false; + } + +/* +* Map->iova chunk size is ignored. What to do if descriptor +* (addr, size) does not fit is delegated to the device. +*/
I think we need at least check the size and fail if the size doesn't match here. Or is it possible that we have a buffer that may cross two memory regions?
Quoted 22 lines
hw/virtio/vhost-shadow-virtqueue.c +9
-1+ off = needle.translated_addr - map->translated_addr; + addrs[i] = (void *)(map->iova + off); + } + +return true; +} +static void vhost_vring_write_descs (VhostShadowVirtqueue *svq, +void *const *vaddr_sg,const struct iovec *iovec,size_t num,bool more_descs,bool write) {@@ -224,7 +270,7 @@ static void vhost_vring_write_descs(VhostShadowVirtqueue *svq, }else { descs[i].flags = flags; } - descs[i].addr =cpu_to_le64 ((hwaddr )iovec[n].iov_base); + descs[i].addr =cpu_to_le64 ((hwaddr )vaddr_sg[n]); descs[i].len =cpu_to_le32 (iovec[n].iov_len); last = i;
hw/virtio/vhost-shadow-virtqueue.c static void vhost_vring_write_descs(VhostShadowVirtqueue *svq, · line 285
Quoted 8 lines
hw/virtio/vhost-shadow-virtqueue.c +3
-2@@ -234,42 +280,60 @@ static void vhost_vring_write_descs(VhostShadowVirtqueue *svq, svq->free_head =le16_to_cpu (descs[last].next); } -static unsigned vhost_svq_add_split (VhostShadowVirtqueue *svq, -VirtQueueElement *elem) +static bool vhost_svq_add_split (VhostShadowVirtqueue *svq, +VirtQueueElement *elem, +unsigned *head)
I'd suggest to make it returns bool since the patch that introduces this function.
Quoted 113 lines
hw/virtio/vhost-shadow-virtqueue.c +34
-10{ -int head;unsigned avail_idx;vring_avail_t *avail = svq->vring.avail; +bool ok; + g_autofree void **sgs = g_new(void *, MAX(elem->out_num, elem->in_num)); - head = svq->free_head; + *head = svq->free_head;/* We need some descriptors here */ assert (elem->out_num || elem->in_num); -vhost_vring_write_descs (svq, elem->out_sg, elem->out_num, + ok =vhost_svq_translate_addr (svq, sgs, elem->out_sg, elem->out_num); +if (unlikely (!ok)) { +return false; + } +vhost_vring_write_descs (svq, sgs, elem->out_sg, elem->out_num, elem->in_num >0 , false); -vhost_vring_write_descs (svq, elem->in_sg, elem->in_num, false, true); + + + ok =vhost_svq_translate_addr (svq, sgs, elem->in_sg, elem->in_num); +if (unlikely (!ok)) { +return false; + } + +vhost_vring_write_descs (svq, sgs, elem->in_sg, elem->in_num, false, true);/* * Put entry in available array (but don't update avail->idx until they * do sync). */ avail_idx = svq->avail_idx_shadow & (svq->vring.num -1 ); - avail->ring[avail_idx] =cpu_to_le16 (head); + avail->ring[avail_idx] =cpu_to_le16 (*head); svq->avail_idx_shadow++;/* Update avail index after the descriptor is wrote */ smp_wmb (); avail->idx =cpu_to_le16 (svq->avail_idx_shadow); -return head; +return true; } -static void vhost_svq_add (VhostShadowVirtqueue *svq,VirtQueueElement *elem) +static bool vhost_svq_add (VhostShadowVirtqueue *svq,VirtQueueElement *elem) { -unsigned qemu_head =vhost_svq_add_split (svq, elem); +unsigned qemu_head; +bool ok =vhost_svq_add_split (svq, elem, &qemu_head); +if (unlikely (!ok)) { +return false; + } svq->ring_id_maps[qemu_head] = elem; +return true; }static void vhost_svq_kick (VhostShadowVirtqueue *svq)@@ -309,6 +373,7 @@ static void vhost_handle_guest_kick(VhostShadowVirtqueue *svq)while (true) {VirtQueueElement *elem; +bool ok;if (svq->next_guest_avail_elem) { elem =g_steal_pointer (&svq->next_guest_avail_elem);@@ -337,7 +402,11 @@ static void vhost_handle_guest_kick(VhostShadowVirtqueue *svq)return ; } -vhost_svq_add (svq, elem); + ok =vhost_svq_add (svq, elem); +if (unlikely (!ok)) { +/* VQ is broken, just return and ignore any other kicks */ +return ; + }vhost_svq_kick (svq); }@@ -619,12 +688,13 @@ void vhost_svq_stop(VhostShadowVirtqueue *svq)* methods and file descriptors. * * @qsize Shadow VirtQueue size +* @iova_tree Tree to perform descriptors translations * * Returns the new virtqueue or NULL. * * In case of error, reason is reported through error_report. */ -VhostShadowVirtqueue *vhost_svq_new (uint16_t qsize) +VhostShadowVirtqueue *vhost_svq_new (uint16_t qsize,VhostIOVATree *iova_tree) {size_t desc_size =sizeof (vring_desc_t) * qsize;size_t device_size, driver_size;@@ -656,6 +726,7 @@ VhostShadowVirtqueue *vhost_svq_new(uint16_t qsize)memset (svq->vring.desc,0 , driver_size); svq->vring.used =qemu_memalign (qemu_real_host_page_size, device_size);memset (svq->vring.used,0 , device_size); + svq->iova_tree = iova_tree; svq->ring_id_maps = g_new0(VirtQueueElement *, qsize);event_notifier_set_handler (&svq->hdev_call, vhost_svq_handle_call);return g_steal_pointer (&svq);hw/virtio/vhost-vdpa.c +0
-0@@ -209,6 +209,18 @@ static void vhost_vdpa_listener_region_add(MemoryListener *listener, vaddr, section->readonly);
hw/virtio/vhost-vdpa.c static void vhost_vdpa_listener_region_add(MemoryListener *listener, · line 220
Quoted 10 lines
hw/virtio/vhost-vdpa.c +9
-0llsize =int128_sub (llend,int128_make64 (iova)); +if (v->shadow_vqs_enabled) { +DMAMap mem_region = { + .translated_addr = (hwaddr )vaddr, + .size =int128_get64 (llsize) -1 , + .perm =IOMMU_ACCESS_FLAG (true, section->readonly), + }; + +int r =vhost_iova_tree_map_alloc (v->iova_tree, &mem_region); +assert (r ==IOVA_OK );
It's better to fail or warn here.
Quoted 216 lines
hw/virtio/vhost-vdpa.c +119
-19+ + iova = mem_region.iova; + }vhost_vdpa_iotlb_batch_begin_once (v); ret = vhost_vdpa_dma_map(v, iova, int128_get64(llsize),@@ -261,6 +273,20 @@ static void vhost_vdpa_listener_region_del(MemoryListener *listener, llsize =int128_sub (llend,int128_make64 (iova)); +if (v->shadow_vqs_enabled) { +const DMAMap *result; +const void *vaddr =memory_region_get_ram_ptr (section->mr) + + section->offset_within_region + + (iova - section->offset_within_address_space); +DMAMap mem_region = { + .translated_addr = (hwaddr )vaddr, + .size =int128_get64 (llsize) -1 , + }; + + result =vhost_iova_tree_find_iova (v->iova_tree, &mem_region); + iova = result->iova; +vhost_iova_tree_remove (v->iova_tree, &mem_region); + }vhost_vdpa_iotlb_batch_begin_once (v); ret =vhost_vdpa_dma_unmap (v, iova,int128_get64 (llsize));if (ret) {@@ -783,33 +809,70 @@ static int vhost_vdpa_svq_set_fds(struct vhost_dev *dev,/** * Unmap SVQ area in the device */ -static bool vhost_vdpa_svq_unmap_ring (struct vhost_vdpa *v,hwaddr iova, -hwaddr size) +static bool vhost_vdpa_svq_unmap_ring (struct vhost_vdpa *v, +const DMAMap *needle) { +const DMAMap *result =vhost_iova_tree_find_iova (v->iova_tree, needle); +hwaddr size;int r; - size =ROUND_UP (size, qemu_real_host_page_size); - r =vhost_vdpa_dma_unmap (v, iova, size); +if (unlikely (!result)) { +error_report ("Unable to find SVQ address to unmap" ); +return false; + } + + size =ROUND_UP (result->size, qemu_real_host_page_size); + r =vhost_vdpa_dma_unmap (v, result->iova, size);return r ==0 ; }static bool vhost_vdpa_svq_unmap_rings (struct vhost_dev *dev,const VhostShadowVirtqueue *svq) { +DMAMap needle;struct vhost_vdpa *v = dev->opaque;struct vhost_vring_addr svq_addr; -size_t device_size =vhost_svq_device_area_size (svq); -size_t driver_size =vhost_svq_driver_area_size (svq);bool ok;vhost_svq_get_vring_addr (svq, &svq_addr); - ok =vhost_vdpa_svq_unmap_ring (v, svq_addr.desc_user_addr, driver_size); + needle = (DMAMap ) { + .translated_addr = svq_addr.desc_user_addr, + }; + ok =vhost_vdpa_svq_unmap_ring (v, &needle);if (unlikely (!ok)) {return false; } -return vhost_vdpa_svq_unmap_ring (v, svq_addr.used_user_addr, device_size); + needle = (DMAMap ) { + .translated_addr = svq_addr.used_user_addr, + }; +return vhost_vdpa_svq_unmap_ring (v, &needle); +} + +/** +* Map SVQ area in the device +* +* @v Vhost-vdpa device +* @needle The area to search iova +* @readonly Permissions of the area +*/ +static bool vhost_vdpa_svq_map_ring (struct vhost_vdpa *v,const DMAMap *needle, +bool readonly) +{ +hwaddr off; +const DMAMap *result =vhost_iova_tree_find_iova (v->iova_tree, needle); +int r; + +if (unlikely (!result)) { +error_report ("Can't locate SVQ ring" ); +return false; + } + + off = needle->translated_addr - result->translated_addr; + r =vhost_vdpa_dma_map (v, result->iova + off, needle->size, + (void *)needle->translated_addr, readonly); +return r ==0 ; }/** @@ -821,23 +884,29 @@ static bool vhost_vdpa_svq_unmap_rings(struct vhost_dev *dev,static bool vhost_vdpa_svq_map_rings (struct vhost_dev *dev,const VhostShadowVirtqueue *svq) { +DMAMap needle;struct vhost_vdpa *v = dev->opaque;struct vhost_vring_addr svq_addr;size_t device_size =vhost_svq_device_area_size (svq);size_t driver_size =vhost_svq_driver_area_size (svq); -int r; +bool ok;vhost_svq_get_vring_addr (svq, &svq_addr); - r =vhost_vdpa_dma_map (v, svq_addr.desc_user_addr, driver_size, - (void *)svq_addr.desc_user_addr, true); -if (unlikely (r !=0 )) { + needle = (DMAMap ) { + .translated_addr = svq_addr.desc_user_addr, + .size = driver_size, + }; + ok =vhost_vdpa_svq_map_ring (v, &needle, true); +if (unlikely (!ok)) {return false; } - r =vhost_vdpa_dma_map (v, svq_addr.used_user_addr, device_size, - (void *)svq_addr.used_user_addr, false); -return r ==0 ; + needle = (DMAMap ) { + .translated_addr = svq_addr.used_user_addr, + .size = device_size, + }; +return vhost_vdpa_svq_map_ring (v, &needle, false); } static bool vhost_vdpa_svq_setup(struct vhost_dev *dev,@@ -1006,6 +1075,23 @@ static int vhost_vdpa_set_owner(struct vhost_dev *dev)return vhost_vdpa_call (dev,VHOST_SET_OWNER ,NULL ); } +static bool vhost_vdpa_svq_get_vq_region (struct vhost_vdpa *v, +unsigned long long addr, +uint64_t *iova_addr) +{ +const DMAMap needle = { + .translated_addr = addr, + }; +const DMAMap *translation =vhost_iova_tree_find_iova (v->iova_tree, + &needle); +if (!translation) { +return false; + } + + *iova_addr = translation->iova + (addr - translation->translated_addr); +return true; +} +static void vhost_vdpa_vq_get_guest_addr (struct vhost_vring_addr *addr,struct vhost_virtqueue *vq) {@@ -1023,10 +1109,23 @@ static int vhost_vdpa_vq_get_addr(struct vhost_dev *dev,assert (dev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_VDPA );if (v->shadow_vqs_enabled) { +struct vhost_vring_addr svq_addr;int idx =vhost_vdpa_get_vq_index (dev, addr->index);VhostShadowVirtqueue *svq =g_ptr_array_index (v->shadow_vqs, idx); -vhost_svq_get_vring_addr (svq, addr); +vhost_svq_get_vring_addr (svq, &svq_addr); +if (!vhost_vdpa_svq_get_vq_region (v, svq_addr.desc_user_addr, + &addr->desc_user_addr)) { +return -1 ; + } +if (!vhost_vdpa_svq_get_vq_region (v, svq_addr.avail_user_addr, + &addr->avail_user_addr)) { +return -1 ; + } +if (!vhost_vdpa_svq_get_vq_region (v, svq_addr.used_user_addr, + &addr->used_user_addr)) { +return -1 ; + } }else {vhost_vdpa_vq_get_guest_addr (addr, vq); }@@ -1095,13 +1194,37 @@ static int vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v, shadow_vqs =g_ptr_array_new_full (hdev->nvqs, vhost_psvq_free);for (unsigned n =0 ; n < hdev->nvqs; ++n) { -VhostShadowVirtqueue *svq =vhost_svq_new (qsize); - +DMAMap device_region, driver_region; +struct vhost_vring_addr addr; +VhostShadowVirtqueue *svq =vhost_svq_new (qsize, v->iova_tree);if (unlikely (!svq)) {error_setg (errp,"Cannot create svq %u" , n);return -1 ; } -g_ptr_array_add (v->shadow_vqs, svq); + +vhost_svq_get_vring_addr (svq, &addr); + driver_region = (DMAMap ) { + .translated_addr = (hwaddr )addr.desc_user_addr, + +/* +* DMAMAp.size include the last byte included in the range, while +* sizeof marks one past it. Substract one byte to make them match. +*/ + .size =vhost_svq_driver_area_size (svq) -1 ,
hw/virtio/vhost-vdpa.c static int vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v, · line 1223
Quoted 10 lines
hw/virtio/vhost-vdpa.c +10
-0+ .perm = VHOST_ACCESS_RO, + }; + device_region = (DMAMap ) { + .translated_addr = (hwaddr )addr.used_user_addr, + .size =vhost_svq_device_area_size (svq) -1 , + .perm =VHOST_ACCESS_RW , + }; + + r =vhost_iova_tree_map_alloc (v->iova_tree, &driver_region); +assert (r ==IOVA_OK );
Let's fail instead of assert here.
Thanks
hw/virtio/vhost-vdpa.c static int vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v, · line 1230
Quoted 7 lines
hw/virtio/vhost-vdpa.c +4
-0+ r =vhost_iova_tree_map_alloc (v->iova_tree, &device_region); +assert (r ==IOVA_OK ); + +g_ptr_array_add (shadow_vqs, svq); } out:
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH 18/31] vhost: Shadow virtqueue buffers forwarding
In reply to Eugenio Pérezhw/virtio/vhost-shadow-virtqueue.c void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd) · line 600
Quoted 12 lines · Eugenio Pérez
hw/virtio/vhost-shadow-virtqueue.c +8
-0@@ -272,6 +590,28 @@ void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd)void vhost_svq_stop (VhostShadowVirtqueue *svq) {event_notifier_set_handler (&svq->svq_kick,NULL ); + g_autofree VirtQueueElement *next_avail_elem = NULL; + +if (!svq->vq) { +return ; + } + +/* Send all pending used descriptors to guest */ +vhost_svq_flush (svq, false);
Do we need to wait for all the pending descriptors to be completed here?
Thanks
Quoted 5 lines
hw/virtio/vhost-shadow-virtqueue.c +5
-0+ +for (unsigned i =0 ; i < svq->vring.num; ++i) { + g_autofree VirtQueueElement *elem = NULL; + elem =g_steal_pointer (&svq->ring_id_maps[i]); +if (elem) {
hw/virtio/vhost-shadow-virtqueue.c void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd) · line 615
Quoted 10 lines
hw/virtio/vhost-shadow-virtqueue.c +9
-0+virtqueue_detach_element (svq->vq, elem, elem->len); + } + } + + next_avail_elem =g_steal_pointer (&svq->next_guest_avail_elem); +if (next_avail_elem) { +virtqueue_detach_element (svq->vq, next_avail_elem, + next_avail_elem->len); + } }
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH 28/31] vdpa: Expose VHOST_F_LOG_ALL on SVQ
In reply to Eugenio PérezQuoted 36 lines · Eugenio Pérez
SVQ is able to log the dirty bits by itself, so let's use it to not block migration.
Also, ignore set and clear of
VHOST_F_LOG_ALLonset_featuresif SVQ is enabled. Even if the device supports it, the reports would be nonsense because SVQ memory is in the qemu region.The log region is still allocated. Future changes might skip that, but this series is already long enough.
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-vdpa.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)hw/virtio/vhost-vdpa.c +4
-0@@ -1022,6 +1022,9 @@ static int vhost_vdpa_get_features(struct vhost_dev *dev, uint64_t *features)if (ret ==0 && v->shadow_vqs_enabled) {/* Filter only features that SVQ can offer to guest */ vhost_svq_valid_guest_features (features); + +/* Add SVQ logging capabilities */ + *features |=BIT_ULL (VHOST_F_LOG_ALL ); }return ret;@@ -1039,8 +1042,25 @@ static int vhost_vdpa_set_features(struct vhost_dev *dev,if (v->shadow_vqs_enabled) {uint64_t dev_features, svq_features, acked_features; +uint8_t status =0 ;bool ok;
hw/virtio/vhost-vdpa.c static int vhost_vdpa_set_features(struct vhost_dev *dev, · line 1057
Quoted 10 lines
hw/virtio/vhost-vdpa.c +10
-0+ ret =vhost_vdpa_call (dev,VHOST_VDPA_GET_STATUS , &status); +if (unlikely (ret)) { +return ret; + } + +if (status &VIRTIO_CONFIG_S_DRIVER_OK ) { +/* +* vhost is trying to enable or disable _F_LOG, and the device +* would report wrong dirty pages. SVQ handles it. +*/
I fail to understand this comment, I'd think there's no way to disable dirty page tracking for SVQ.
Thanks
hw/virtio/vhost-vdpa.c static int vhost_vdpa_set_features(struct vhost_dev *dev, · line 1066
Quoted 9 lines
hw/virtio/vhost-vdpa.c +6
-0+return 0 ; + } + +/* We must not ack _F_LOG if SVQ is enabled */ + features &= ~BIT_ULL (VHOST_F_LOG_ALL ); + ret =vhost_vdpa_get_dev_features (dev, &dev_features);if (ret !=0 ) {error_report ("Can't get vdpa device features, got (%d)" , ret);
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH 29/31] vdpa: Make ncs autofree
In reply to Eugenio PérezQuoted 3 lines · Eugenio Pérez
Simplifying memory management.
Signed-off-by Eugenio Pérez <[email protected]>
To reduce the size of this series. This can be sent as an separate patch if I was not wrong.
Thanks
Quoted 27 lines
--- net/vhost-vdpa.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)net/vhost-vdpa.c +2
-2@@ -264,7 +264,8 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name, {const NetdevVhostVDPAOptions *opts;int vdpa_device_fd; -NetClientState **ncs, *nc; + g_autofree NetClientState **ncs = NULL; +NetClientState *nc;int queue_pairs, i, has_cvq =0 ;assert (netdev->type ==NET_CLIENT_DRIVER_VHOST_VDPA );@@ -302,7 +303,6 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name, goto err; } -g_free (ncs);return 0 ; err:
net/vhost-vdpa.c err: · line 315
Quoted 7 lines
net/vhost-vdpa.c +0
-1@@ -310,7 +310,6 @@ err:qemu_del_net_client (ncs[0 ]); }qemu_close (vdpa_device_fd); -g_free (ncs);return -1 ; }
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH 30/31] vdpa: Move vhost_vdpa_get_iova_range to net/vhost-vdpa.c
In reply to Eugenio PérezQuoted 8 lines · Eugenio Pérez
Since it's a device property, it can be done in
net/. This helps SVQ to allocate the rings in vdpa device initialization, rather than delay that.Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-vdpa.c | 15 --------------- net/vhost-vdpa.c | 32 ++++++++++++++++++++++++--------
I don't understand here, since we will support device other than net?
Quoted 12 lines
2 files changed, 24 insertions(+), 23 deletions(-)hw/virtio/vhost-vdpa.c +0
-2@@ -350,19 +350,6 @@ static int vhost_vdpa_add_status(struct vhost_dev *dev, uint8_t status)return 0 ; } -static void vhost_vdpa_get_iova_range (struct vhost_vdpa *v) -{
hw/virtio/vhost-vdpa.c static int vhost_vdpa_add_status(struct vhost_dev *dev, uint8_t status) · line 364
Quoted 10 lines
hw/virtio/vhost-vdpa.c +0
-10-int ret =vhost_vdpa_call (v->dev,VHOST_VDPA_GET_IOVA_RANGE , - &v->iova_range); -if (ret !=0 ) { - v->iova_range.first =0 ; - v->iova_range.last =UINT64_MAX ; - } - -trace_vhost_vdpa_get_iova_range (v->dev, v->iova_range.first, - v->iova_range.last); -}
Let's just export this instead?
Thanks
Quoted 86 lines
hw/virtio/vhost-vdpa.c +0
-3-static bool vhost_vdpa_one_time_request (struct vhost_dev *dev) {struct vhost_vdpa *v = dev->opaque;@@ -1295,8 +1282,6 @@ static int vhost_vdpa_init(struct vhost_dev *dev, void *opaque, Error **errp) goto err; } -vhost_vdpa_get_iova_range (v); -if (vhost_vdpa_one_time_request (dev)) {return 0 ; }net/vhost-vdpa.c +23
-7@@ -22,6 +22,7 @@#include <sys/ioctl.h> #include <err.h> #include "standard-headers/linux/virtio_net.h" +#include "standard-headers/linux/vhost_types.h" #include "monitor/monitor.h" #include "hw/virtio/vhost.h" @@ -187,13 +188,25 @@ static NetClientInfo net_vhost_vdpa_info = { .check_peer_type = vhost_vdpa_check_peer_type, }; +static void vhost_vdpa_get_iova_range (int fd, +struct vhost_vdpa_iova_range *iova_range) +{ +int ret =ioctl (fd,VHOST_VDPA_GET_IOVA_RANGE , iova_range); + +if (ret <0 ) { + iova_range->first =0 ; + iova_range->last =UINT64_MAX ; + } +} +static NetClientState *net_vhost_vdpa_init (NetClientState *peer, -const char *device, -const char *name, -int vdpa_device_fd, -int queue_pair_index, -int nvqs, -bool is_datapath) +const char *device, +const char *name, +int vdpa_device_fd, +int queue_pair_index, +int nvqs, +bool is_datapath, +struct vhost_vdpa_iova_range iova_range) {NetClientState *nc =NULL ;VhostVDPAState *s;@@ -211,6 +224,7 @@ static NetClientState *net_vhost_vdpa_init(NetClientState *peer, s->vhost_vdpa.device_fd = vdpa_device_fd; s->vhost_vdpa.index = queue_pair_index; + s->vhost_vdpa.iova_range = iova_range; ret =vhost_vdpa_add (nc, (void *)&s->vhost_vdpa, queue_pair_index, nvqs);if (ret) {qemu_del_net_client (nc);@@ -267,6 +281,7 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name, g_autofree NetClientState **ncs = NULL;NetClientState *nc;int queue_pairs, i, has_cvq =0 ; +struct vhost_vdpa_iova_range iova_range;assert (netdev->type ==NET_CLIENT_DRIVER_VHOST_VDPA ); opts = &netdev->u.vhost_vdpa;@@ -286,19 +301,20 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,qemu_close (vdpa_device_fd);return queue_pairs; } +vhost_vdpa_get_iova_range (vdpa_device_fd, &iova_range); ncs =g_malloc0 (sizeof (*ncs) * queue_pairs);for (i =0 ; i < queue_pairs; i++) { ncs[i] =net_vhost_vdpa_init (peer,TYPE_VHOST_VDPA , name, - vdpa_device_fd, i,2 , true); + vdpa_device_fd, i,2 , true, iova_range);if (!ncs[i])
net/vhost-vdpa.c int net_init_vhost_vdpa(const Netdev *netdev, const char *name, · line 320
Quoted 10 lines
net/vhost-vdpa.c +1
-1goto err; }if (has_cvq) { nc =net_vhost_vdpa_init (peer,TYPE_VHOST_VDPA , name, - vdpa_device_fd, i,1 , false); + vdpa_device_fd, i,1 , false, iova_range);if (!nc) goto err; }
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Eugenio Perez Martin
maintainerQuoted 53 lines · Jason Wang
Quoted 50 lines · Eugenio Pérez
This series enables shadow virtqueue (SVQ) for vhost-vdpa devices. This is intended as a new method of tracking the memory the devices touch during a migration process: Instead of relay on vhost device's dirty logging capability, SVQ intercepts the VQ dataplane forwarding the descriptors between VM and device. This way qemu is the effective writer of guests memory, like in qemu's emulated virtio device operation.
When SVQ is enabled qemu offers a new virtual address space to the device to read and write into, and it maps new vrings and the guest memory in it. SVQ also intercepts kicks and calls between the device and the guest. Used buffers relay would cause dirty memory being tracked, but at this RFC SVQ is not enabled on migration automatically.
Thanks of being a buffers relay system, SVQ can be used also to communicate devices and drivers with different capabilities, like devices that only support packed vring and not split and old guests with no driver packed support.
It is based on the ideas of DPDK SW assisted LM, in the series of DPDK's https://patchwork.dpdk.org/cover/48370/ . However, these does not map the shadow vq in guest's VA, but in qemu's.
This version of SVQ is limited in the amount of features it can use with guest and device, because this series is already very big otherwise. Features like indirect or
event_idxwill be addressed in future series.SVQ needs to be enabled with cmdline parameter x-svq, like:
-netdev type=vhost-vdpa,vhostdev=/dev/vhost-vdpa-0,id=vhost-vdpa0,x-svq=true
In this version it cannot be enabled or disabled in runtime. Further series will remove this limitation and will enable it only for migration time.
Some patches are intentionally very small to ease review, but they can be squashed if preferred.
Patches 1-10 prepares the SVQ and QEMU to support both guest to device and device to guest notifications forwarding, with the extra qemu hop. That part can be tested in isolation if cmdline change is reproduced.
Patches from 11 to 18 implement the actual buffer forwarding, but with no IOMMU support. It requires a vdpa device capable of addressing all qemu vaddr.
Patches 19 to 23 adds the iommu support, so the device with address range limitations can access SVQ through this new virtual address space created.
Quoted 8 lines
Quoted 3 lines
The rest of the series add the last pieces needed for migration.
Comments are welcome.
I wonder the performance impact. So performance numbers are more than welcomed.
Sure, I'll do it for the next revision. Since this one brings a decent amount of changes, I chose to collect the feedback first.
Thanks!
Quoted 142 lines
Thanks
Quoted 138 lines
TODO:
* Event, indirect, packed, and other features of virtio.
* To separate buffers forwarding in its own AIO context, so we can throw more threads to that task and we don't need to stop the main event loop.
* Support virtio-net control vq.
* Proper documentation.Changes from v5 RFC:
* Remove dynamic enablement of SVQ, making less dependent of the device.
* Enable live migration if SVQ is enabled.
* Fix SVQ when driver reset.
* Comments addressed, specially in the iova area.
* Rebase on latest master, adding multiqueue support (but no networking control vq processing).
v5 link:
https://lists.gnu.org/archive/html/qemu-devel/2021-10/msg07250.htmlChanges from v4 RFC:
* Support of allocating / freeing iova ranges in IOVA tree. Extending already present iova-tree for that.
* Proper validation of guest features. Now SVQ can negotiate a different set of features with the device when enabled.
* Support of host notifiers memory regions
* Handling of SVQ full queue in case guest's descriptors span to different memory regions (qemu's VA chunks).
* Flush pending used buffers at end of SVQ operation.
* QMP command now looks by NetClientState name. Other devices will need to implement it's way to enable vdpa.
* Rename QMP command to set, so it looks more like a way of working
* Better use of qemu error system
* Make a few assertions proper error-handling paths.
* Add more documentation
* Less coupling of virtio / vhost, that could cause friction on changes
* Addressed many other small comments and small fixes.Changes from v3 RFC:
* Move everything to vhost-vdpa backend. A big change, this allowed some cleanup but more code has been added in other places.
* More use of glib utilities, especially to manage memory.
v3 link:
https://lists.nongnu.org/archive/html/qemu-devel/2021-05/msg06032.htmlChanges from v2 RFC:
* Adding vhost-vdpa devices support
* Fixed some memory leaks pointed by different comments
v2 link:
https://lists.nongnu.org/archive/html/qemu-devel/2021-03/msg05600.htmlChanges from v1 RFC:
* Use QMP instead of migration to start SVQ mode.
* Only accepting IOMMU devices, closer behavior with target devices(vDPA)* Fix invalid masking/unmasking of vhost call fd.
* Use of proper methods for synchronization.
* No need to modify VirtIO device code, all of the changes are contained in vhost code.
* Delete superfluous code.
* An intermediate RFC was sent with only the notifications forwarding changes. It can be seen in
https://patchew.org/QEMU/[email protected]/
v1 link:
https://lists.gnu.org/archive/html/qemu-devel/2020-11/msg05372.htmlEugenio Pérez (20):
virtio: AddVIRTIO_F_QUEUE_STATE
virtio-net: HonorVIRTIO_CONFIG_S_DEVICE_STOPPED
virtio: Addvirtio_queue_is_host_notifier_enabled
vhost: Make vhost_virtqueue_{start,stop} public
vhost: Add x-vhost-enable-shadow-vq qmp
vhost: Add VhostShadowVirtqueue
vdpa: Register vdpa devices in a list
vhost: Routeguest->hostnotification through shadow virtqueue Addvhost_svq_get_svq_call_notifier
Addvhost_svq_set_guest_call_notifier
vdpa: Savecall_fdin vhost-vdpa
vhost-vdpa: Take into account SVQ invhost_vdpa_set_vring_call
vhost: Routehost->guestnotification through shadow virtqueue
virtio: Addvhost_shadow_vq_get_vring_addr
vdpa: Save host and guest features
vhost: Addvhost_svq_valid_device_featuresto shadow vq
vhost: Shadow virtqueue buffers forwarding
vhost: Add VhostIOVATree
vhost: Use a tree to store memory mappings
vdpa: Add custom IOTLB translations to SVQEugenio Pérez (31):
vdpa: Reordervirtio/vhost-vdpa.cfunctions
vhost: Add VhostShadowVirtqueue
vdpa: Addvhost_svq_get_dev_kick_notifier
vdpa: Addvhost_svq_set_svq_kick_fd
vhost: Add Shadow VirtQueue kick forwarding capabilities
vhost: Routeguest->hostnotification through shadow virtqueue
vhost: ddvhost_svq_get_svq_call_notifier
vhost: Addvhost_svq_set_guest_call_notifier
vhost-vdpa: Take into account SVQ invhost_vdpa_set_vring_call
vhost: Routehost->guestnotification through shadow virtqueue
vhost: Addvhost_svq_valid_device_featuresto shadow vq
vhost: Addvhost_svq_valid_guest_featuresto shadow vq
vhost: Addvhost_svq_ack_guest_featuresto shadow vq
virtio: Addvhost_shadow_vq_get_vring_addr
vdpa: Addvhost_svq_get_num
vhost: pass queue index tovhost_vq_get_addr
vdpa: adaptvhost_opscallbacks to svq
vhost: Shadow virtqueue buffers forwarding
utils: Add internal DMAMap to iova-tree
util: Store DMA entries in a list
util: Addiova_tree_alloc
vhost: Add VhostIOVATree
vdpa: Add custom IOTLB translations to SVQ
vhost: Addvhost_svq_get_last_used_idx
vdpa: Adaptvhost_vdpa_get_vring_baseto SVQ
vdpa: ClearVHOST_VRING_F_LOGatvhost_vdpa_set_vring_addrin SVQ
vdpa: Never setlog_baseaddr if SVQ is enabled
vdpa: ExposeVHOST_F_LOG_ALLon SVQ
vdpa: Make ncs autofree
vdpa: Movevhost_vdpa_get_iova_rangetonet/vhost-vdpa.c
vdpa: Add x-svq to NetdevVhostVDPAOptionsqapi/net.json | 5 +- hw/virtio/vhost-iova-tree.h | 27 + hw/virtio/vhost-shadow-virtqueue.h | 46 ++ include/hw/virtio/vhost-vdpa.h | 7 + include/qemu/iova-tree.h | 17 + hw/virtio/vhost-iova-tree.c | 157 ++++++ hw/virtio/vhost-shadow-virtqueue.c | 761 +++++++++++++++++++++++++++++ hw/virtio/vhost-vdpa.c | 740 ++++++++++++++++++++++++---- hw/virtio/vhost.c | 6 +- net/vhost-vdpa.c | 58 ++- util/iova-tree.c | 161 +++++- hw/virtio/meson.build | 2 +- 12 files changed, 1852 insertions(+), 135 deletions(-)create mode 100644
hw/virtio/vhost-iova-tree.hcreate mode 100644hw/virtio/vhost-shadow-virtqueue.hcreate mode 100644hw/virtio/vhost-iova-tree.ccreate mode 100644hw/virtio/vhost-shadow-virtqueue.c
Eugenio Perez Martin
maintainerRe: [PATCH 03/31] vdpa: Add vhost_svq_get_dev_kick_notifier
In reply to Jason WangQuoted 18 lines · Jason Wang
Quoted 15 lines · Eugenio Pérez
Is needed so vhost-vdpa knows the device's kick event fd.
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-shadow-virtqueue.h | 4 ++++ hw/virtio/vhost-shadow-virtqueue.c | 10 +++++++++- 2 files changed, 13 insertions(+), 1 deletion(-)hw/virtio/vhost-shadow-virtqueue.h +0
-0@@ -11,9 +11,13 @@#define VHOST_SHADOW_VIRTQUEUE_H
Quoted 6 lines
hw/virtio/vhost-shadow-virtqueue.h line 14
Quoted 2 lines
hw/virtio/vhost-shadow-virtqueue.h +1
-0#include "hw/virtio/vhost.h" +#include "qemu/event_notifier.h" Let's move this part to patch 2.
Sure, I'll change for the next revision.
Quoted 41 lines
Thanks
Quoted 37 lines
hw/virtio/vhost-shadow-virtqueue.h +3
-0typedef struct VhostShadowVirtqueue VhostShadowVirtqueue ; +const EventNotifier *vhost_svq_get_dev_kick_notifier ( +const VhostShadowVirtqueue *svq); +VhostShadowVirtqueue *vhost_svq_new (void );void vhost_svq_free (VhostShadowVirtqueue *vq);hw/virtio/vhost-shadow-virtqueue.c +9
-1@@ -11,7 +11,6 @@#include "hw/virtio/vhost-shadow-virtqueue.h" #include "qemu/error-report.h" -#include "qemu/event_notifier.h" /* Shadow virtqueue to relay notifications */ typedef struct VhostShadowVirtqueue {@@ -21,6 +20,15 @@ typedef struct VhostShadowVirtqueue {EventNotifier hdev_call; } VhostShadowVirtqueue; +/** +* The notifier that SVQ will use to notify the device. +*/ +const EventNotifier *vhost_svq_get_dev_kick_notifier ( +const VhostShadowVirtqueue *svq) +{ +return &svq->hdev_kick; +} +/** * Creates vhost shadow virtqueue, and instruct vhost device to use the shadow * methods and file descriptors.
Eugenio Perez Martin
maintainerRe: [PATCH 04/31] vdpa: Add vhost_svq_set_svq_kick_fd
In reply to Jason WangQuoted 72 lines · Jason Wang
Quoted 69 lines · Eugenio Pérez
This function allows the vhost-vdpa backend to override
kick_fd.Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-shadow-virtqueue.h | 1 + hw/virtio/vhost-shadow-virtqueue.c | 45 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+)hw/virtio/vhost-shadow-virtqueue.h +1
-0@@ -15,6 +15,7 @@typedef struct VhostShadowVirtqueue VhostShadowVirtqueue ; +void vhost_svq_set_svq_kick_fd (VhostShadowVirtqueue *svq,int svq_kick_fd);const EventNotifier *vhost_svq_get_dev_kick_notifier (const VhostShadowVirtqueue *svq);hw/virtio/vhost-shadow-virtqueue.c +25
-0@@ -11,6 +11,7 @@#include "hw/virtio/vhost-shadow-virtqueue.h" #include "qemu/error-report.h" +#include "qemu/main-loop.h" /* Shadow virtqueue to relay notifications */ typedef struct VhostShadowVirtqueue {@@ -18,8 +19,20 @@ typedef struct VhostShadowVirtqueue {EventNotifier hdev_kick;/* Shadow call notifier, sent to vhost */ EventNotifier hdev_call; + +/* +* Borrowed virtqueue's guest to host notifier. +* To borrow it in this event notifier allows to register on the event +* loop and access the associated shadow virtqueue easily. If we use the +* VirtQueue, we don't have an easy way to retrieve it. +* +* So shadow virtqueue must not clean it, or we would lose VirtQueue one. +*/ +EventNotifier svq_kick; } VhostShadowVirtqueue; +#define INVALID_SVQ_KICK_FD -1 +/** * The notifier that SVQ will use to notify the device. */ @@ -29,6 +42,35 @@ const EventNotifier *vhost_svq_get_dev_kick_notifier(return &svq->hdev_kick; } +/** +* Set a new file descriptor for the guest to kick SVQ and notify for avail +* +* @svq The svq +* @svq_kick_fd The new svq kick fd +*/ +void vhost_svq_set_svq_kick_fd (VhostShadowVirtqueue *svq,int svq_kick_fd) +{ +EventNotifier tmp; +bool check_old =INVALID_SVQ_KICK_FD != +event_notifier_get_fd (&svq->svq_kick); +
Quoted 9 lines
hw/virtio/vhost-shadow-virtqueue.c const EventNotifier *vhost_svq_get_dev_kick_notifier( · line 60
Quoted 4 lines
hw/virtio/vhost-shadow-virtqueue.c +4
-0+if (check_old) { +event_notifier_set_handler (&svq->svq_kick,NULL ); +event_notifier_init_fd (&tmp,event_notifier_get_fd (&svq->svq_kick)); + }It looks to me we don't do similar things in vhost-net. Any reason for caring about the old
svq_kick?
Do you mean to check for old kick_fd in case we miss notifications, and explicitly omit the INVALID_SVQ_KICK_FD?
If you mean qemu's vhost-net, I guess it's because the device's kick fd is never changed in all the vhost device lifecycle, it's only set at the beginning. Previous RFC also depended on that, but you suggested better vhost and SVQ in v4 feedback if I understood correctly [1]. Or am I missing something?
Qemu's vhost-net does not need to use this because it is not polling it. For kernel's vhost, I guess the closest is the use of pollstop and pollstart at vhost_vring_ioctl.
In my opinion, I think that SVQ code size can benefit from now allowing to override kick_fd from the start of the operation. Not from initialization, but start. But I can see the benefits of having the change into account from this moment so it's more resilient to the future.
Quoted 15 lines
hw/virtio/vhost-shadow-virtqueue.c const EventNotifier *vhost_svq_get_dev_kick_notifier( · line 70
Quoted 11 lines
hw/virtio/vhost-shadow-virtqueue.c +10
-0+ +/* +* event_notifier_set_handler already checks for guest's notifications if +* they arrive to the new file descriptor in the switch, so there is no +* need to explicitely check for them. +*/ +event_notifier_init_fd (&svq->svq_kick, svq_kick_fd); + +if (!check_old ||event_notifier_test_and_clear (&tmp)) { +event_notifier_set (&svq->hdev_kick);Any reason we need to kick the device directly here?
At this point of the series only notifications are forwarded, not buffers. If kick_fd is set, we need to check the old one, the same way as vhost checks the masked notifier in case of change.
Thanks!
[1] https://lists.gnu.org/archive/html/qemu-devel/2021-10/msg03152.html
, from "I'd suggest to not depend on this since it:"
Quoted 20 lines
Thanks
Quoted 16 lines
hw/virtio/vhost-shadow-virtqueue.c +6
-0+ } +} +/** * Creates vhost shadow virtqueue, and instruct vhost device to use the shadow * methods and file descriptors. @@ -52,6 +94,9 @@ VhostShadowVirtqueue *vhost_svq_new(void) goto err_init_hdev_call; } +/* Placeholder descriptor, it should be deleted at set_kick_fd */ +event_notifier_init_fd (&svq->svq_kick,INVALID_SVQ_KICK_FD ); +return g_steal_pointer (&svq); err_init_hdev_call:
Eugenio Perez Martin
maintainerRe: [PATCH 05/31] vhost: Add Shadow VirtQueue kick forwarding capabilities
In reply to Jason WangQuoted 6 lines · Jason Wang
Quoted 3 lines · Eugenio Pérez
At this mode no buffer forwarding will be performed in SVQ mode: Qemu will just forward the guest's kicks to the device.
Quoted 8 lines
Quoted 3 lines
Also, host notifiers must be disabled at SVQ start, and they will not start if SVQ has been enabled when the device is stopped. This will be addressed in next patches.
We need to disable
host_notifier_mras well, otherwise guest may touch the hardware doorbell directly without going through eventfd.
Yes. SVQ cannot be enabled at this point anyway, but I think it's a good idea to reorder so we disable hn_mr first.
Quoted 64 lines
Quoted 64 lines
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-shadow-virtqueue.h | 2 ++ hw/virtio/vhost-shadow-virtqueue.c | 27 ++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-)hw/virtio/vhost-shadow-virtqueue.h +2
-0@@ -19,6 +19,8 @@ void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd);const EventNotifier *vhost_svq_get_dev_kick_notifier (const VhostShadowVirtqueue *svq); +void vhost_svq_stop (VhostShadowVirtqueue *svq); +VhostShadowVirtqueue *vhost_svq_new (void );void vhost_svq_free (VhostShadowVirtqueue *vq);hw/virtio/vhost-shadow-virtqueue.c +17
-1@@ -42,11 +42,26 @@ const EventNotifier *vhost_svq_get_dev_kick_notifier(return &svq->hdev_kick; } +/* Forward guest notifications */ +static void vhost_handle_guest_kick (EventNotifier *n) +{ +VhostShadowVirtqueue *svq =container_of (n, VhostShadowVirtqueue, + svq_kick); + +if (unlikely (!event_notifier_test_and_clear (n))) { +return ; + } + +event_notifier_set (&svq->hdev_kick); +} +/** * Set a new file descriptor for the guest to kick SVQ and notify for avail * * @svq The svq -* @svq_kick_fd The new svq kick fd +* @svq_kick_fd The svq kick fd +* +* Note that SVQ will never close the old file descriptor. */ void vhost_svq_set_svq_kick_fd (VhostShadowVirtqueue *svq,int svq_kick_fd) {@@ -65,12 +80,22 @@ void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd)* need to explicitely check for them. */ event_notifier_init_fd (&svq->svq_kick, svq_kick_fd); +event_notifier_set_handler (&svq->svq_kick, vhost_handle_guest_kick);if (!check_old ||event_notifier_test_and_clear (&tmp)) {event_notifier_set (&svq->hdev_kick); } }
Quoted 12 lines
hw/virtio/vhost-shadow-virtqueue.c void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd) · line 97
Quoted 8 lines
hw/virtio/vhost-shadow-virtqueue.c +8
-0+/** +* Stop shadow virtqueue operation. +* @svq Shadow Virtqueue +*/ +void vhost_svq_stop (VhostShadowVirtqueue *svq) +{ +event_notifier_set_handler (&svq->svq_kick,NULL ); +}This function is not used in the patch.
Right, I will add the use of it here.
Thanks!
Quoted 8 lines
Thanks
Quoted 4 lines
hw/virtio/vhost-shadow-virtqueue.c +1
-0+/** * Creates vhost shadow virtqueue, and instruct vhost device to use the shadow * methods and file descriptors.
Eugenio Perez Martin
maintainerRe: [PATCH 06/31] vhost: Route guest->host notification through shadow virtqueue
In reply to Jason WangQuoted 7 lines · Jason Wang
Quoted 4 lines · Eugenio Pérez
At this moment no buffer forwarding will be performed in SVQ mode: Qemu just forward the guest's kicks to the device. This commit also set up SVQs in the vhost device.
Quoted 7 lines
Quoted 2 lines
Host memory notifiers regions are left out for simplicity, and they will not be addressed in this series.
I wonder if it's better to squash this into patch 5 since it gives us a full
guest->hostforwarding.
I'm fine with that if you think it makes the review easier.
Quoted 56 lines
Quoted 56 lines
Signed-off-by Eugenio Pérez <[email protected]>--- include/hw/virtio/vhost-vdpa.h | 4 ++ hw/virtio/vhost-vdpa.c | 122 ++++++++++++++++++++++++++++++++- 2 files changed, 124 insertions(+), 2 deletions(-)include/hw/virtio/vhost-vdpa.h +4
-0@@ -12,6 +12,8 @@#ifndef HW_VIRTIO_VHOST_VDPA_H #define HW_VIRTIO_VHOST_VDPA_H +#include <gmodule.h> +#include "hw/virtio/virtio.h" #include "standard-headers/linux/vhost_types.h" @@ -27,6 +29,8 @@ typedef struct vhost_vdpa {bool iotlb_batch_begin_sent;MemoryListener listener;struct vhost_vdpa_iova_range iova_range; +bool shadow_vqs_enabled; +GPtrArray *shadow_vqs;struct vhost_dev *dev;VhostVDPAHostNotifier notifier[VIRTIO_QUEUE_MAX ]; } VhostVDPA;hw/virtio/vhost-vdpa.c +3
-0@@ -17,12 +17,14 @@#include "hw/virtio/vhost.h" #include "hw/virtio/vhost-backend.h" #include "hw/virtio/virtio-net.h" +#include "hw/virtio/vhost-shadow-virtqueue.h" #include "hw/virtio/vhost-vdpa.h" #include "exec/address-spaces.h" #include "qemu/main-loop.h" #include "cpu.h" #include "trace.h" #include "qemu-common.h" +#include "qapi/error.h" /* * Return one past the end of the end of section. Be careful with uint64_t @@ -409,8 +411,14 @@ err:static void vhost_vdpa_host_notifiers_init (struct vhost_dev *dev) { +struct vhost_vdpa *v = dev->opaque;int i;
Quoted 6 lines
hw/virtio/vhost-vdpa.c err: · line 418
Quoted 2 lines
hw/virtio/vhost-vdpa.c +2
-0+if (v->shadow_vqs_enabled) { +/* SVQ is not compatible with host notifiers mr */ I guess there should be a TODO or FIXME here.
Sure I can add it.
Quoted 99 lines
Quoted 99 lines
hw/virtio/vhost-vdpa.c +53
-2+return ; + } +for (i = dev->vq_index; i < dev->vq_index + dev->nvqs; i++) {if (vhost_vdpa_host_notifier_init (dev, i)) { goto err;@@ -424,6 +432,17 @@ err:return ; } +static void vhost_vdpa_svq_cleanup (struct vhost_dev *dev) +{ +struct vhost_vdpa *v = dev->opaque; +size_t idx; + +for (idx =0 ; idx < v->shadow_vqs->len; ++idx) { +vhost_svq_stop (g_ptr_array_index (v->shadow_vqs, idx)); + } +g_ptr_array_free (v->shadow_vqs, true); +} +static int vhost_vdpa_cleanup (struct vhost_dev *dev) {struct vhost_vdpa *v;@@ -432,6 +451,7 @@ static int vhost_vdpa_cleanup(struct vhost_dev *dev)trace_vhost_vdpa_cleanup (dev, v);vhost_vdpa_host_notifiers_uninit (dev, dev->nvqs);memory_listener_unregister (&v->listener); +vhost_vdpa_svq_cleanup (dev); dev->opaque =NULL ;ram_block_discard_disable (false);@@ -507,9 +527,15 @@ static int vhost_vdpa_get_device_id(struct vhost_dev *dev,static int vhost_vdpa_reset_device (struct vhost_dev *dev) { +struct vhost_vdpa *v = dev->opaque;int ret;uint8_t status =0 ; +for (unsigned i =0 ; i < v->shadow_vqs->len; ++i) { +VhostShadowVirtqueue *svq =g_ptr_array_index (v->shadow_vqs, i); +vhost_svq_stop (svq); + } + ret =vhost_vdpa_call (dev,VHOST_VDPA_SET_STATUS , &status);trace_vhost_vdpa_reset_device (dev, status);return ret;@@ -639,13 +665,28 @@ static int vhost_vdpa_get_vring_base(struct vhost_dev *dev,return ret; } -static int vhost_vdpa_set_vring_kick (struct vhost_dev *dev, -struct vhost_vring_file *file) +static int vhost_vdpa_set_vring_dev_kick (struct vhost_dev *dev, +struct vhost_vring_file *file) {trace_vhost_vdpa_set_vring_kick (dev, file->index, file->fd);return vhost_vdpa_call (dev,VHOST_SET_VRING_KICK , file); } +static int vhost_vdpa_set_vring_kick (struct vhost_dev *dev, +struct vhost_vring_file *file) +{ +struct vhost_vdpa *v = dev->opaque; +int vdpa_idx =vhost_vdpa_get_vq_index (dev, file->index); + +if (v->shadow_vqs_enabled) { +VhostShadowVirtqueue *svq =g_ptr_array_index (v->shadow_vqs, vdpa_idx); +vhost_svq_set_svq_kick_fd (svq, file->fd); +return 0 ; + }else { +return vhost_vdpa_set_vring_dev_kick (dev, file); + } +} +static int vhost_vdpa_set_vring_call (struct vhost_dev *dev,struct vhost_vring_file *file) {@@ -653,6 +694,33 @@ static int vhost_vdpa_set_vring_call(struct vhost_dev *dev,return vhost_vdpa_call (dev,VHOST_SET_VRING_CALL , file); } +/** +* Set shadow virtqueue descriptors to the device +* +* @dev The vhost device model +* @svq The shadow virtqueue +* @idx The index of the virtqueue in the vhost device +*/ +static bool vhost_vdpa_svq_setup (struct vhost_dev *dev, +VhostShadowVirtqueue *svq, +unsigned idx) +{ +struct vhost_vring_file file = { + .index = dev->vq_index + idx, + }; +const EventNotifier *event_notifier;
Quoted 9 lines
hw/virtio/vhost-vdpa.c static int vhost_vdpa_set_vring_call(struct vhost_dev *dev, · line 714
Quoted 3 lines
hw/virtio/vhost-vdpa.c +3
-0+int r; + + event_notifier =vhost_svq_get_dev_kick_notifier (svq);A question, any reason for making VhostShadowVirtqueue private? If we export it in .h we don't need helper to access its member like
vhost_svq_get_dev_kick_notifier().
To export it it's always a possibility of course, but that direct access will not be thread safe if we decide to move SVQ to its own iothread for example.
I feel it will be easier to work with it this way but it might be that I'm just used to making as much as possible private. Not like it's needed to use the helpers in the hot paths, only in the setup and teardown.
Quoted 2 lines
Note that
vhost_devis a public structure.
Sure we could embed in vhost_virtqueue if we choose to do it that way, for example.
Quoted 8 lines
hw/virtio/vhost-vdpa.c static int vhost_vdpa_set_vring_call(struct vhost_dev *dev, · line 719
Quoted 6 lines
hw/virtio/vhost-vdpa.c +5
-0+ file.fd =event_notifier_get_fd (event_notifier); + r =vhost_vdpa_set_vring_dev_kick (dev, &file); +if (unlikely (r !=0 )) { +error_report ("Can't set device kick fd (%d)" , -r); + }
Quoted 4 lines
I wonder whether or not we can generalize the logic here and
vhost_vdpa_set_vring_kick(). There's nothing vdpa specific unless thevhost_ops->set_vring_kick().
If we call vhost_ops->set_vring_kick we are setting guest->SVQ kick notifier, not SVQ -> vDPA device, because the if(v->shadow_vqs_enabled). All of the modified ops callbacks are hiding the actual device from the vhost subsystem so we need to explicitly use the newly created _dev_ ones.
Quoted 26 lines
Quoted 26 lines
hw/virtio/vhost-vdpa.c +11
-0+ +return r ==0 ; +} +static int vhost_vdpa_dev_start (struct vhost_dev *dev,bool started) {struct vhost_vdpa *v = dev->opaque;@@ -660,6 +728,13 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)if (started) {vhost_vdpa_host_notifiers_init (dev); +for (unsigned i =0 ; i < v->shadow_vqs->len; ++i) { +VhostShadowVirtqueue *svq =g_ptr_array_index (v->shadow_vqs, i); +bool ok =vhost_vdpa_svq_setup (dev, svq, i); +if (unlikely (!ok)) { +return -1 ; + } + }vhost_vdpa_set_vring_ready (dev); }else {vhost_vdpa_host_notifiers_uninit (dev, dev->nvqs);@@ -737,6 +812,41 @@ static bool vhost_vdpa_force_iommu(struct vhost_dev *dev)return true; }
Quoted 13 lines
hw/virtio/vhost-vdpa.c static bool vhost_vdpa_force_iommu(struct vhost_dev *dev) · line 823
Quoted 9 lines
hw/virtio/vhost-vdpa.c +9
-0+/** +* Adaptor function to free shadow virtqueue through gpointer +* +* @svq The Shadow Virtqueue +*/ +static void vhost_psvq_free (gpointer svq) +{ +vhost_svq_free (svq); +}Any reason for such indirection? Can we simply use
vhost_svq_free()?
GCC complains about different types. I think we could do a function type cast and it's valid for every architecture qemu supports, but the indirection seems cleaner to me, and I would be surprised if the compiler does not optimize it away in the cases that the casting are valid.
../hw/virtio/vhost-vdpa.c:1186:60: error: incompatible function pointer types passing 'void (VhostShadowVirtqueue *)' (aka 'void (struct VhostShadowVirtqueue *)') to parameter of type 'GDestroyNotify' (aka 'void (*)(void *)')
Thanks!
Quoted 55 lines
Thanks
Quoted 51 lines
hw/virtio/vhost-vdpa.c +34
-0+ +static int vhost_vdpa_init_svq (struct vhost_dev *hdev,struct vhost_vdpa *v, +Error **errp) +{ +size_t n_svqs = v->shadow_vqs_enabled ? hdev->nvqs :0 ; +g_autoptr (GPtrArray) shadow_vqs =g_ptr_array_new_full (n_svqs, + vhost_psvq_free); +if (!v->shadow_vqs_enabled) { + goto out; + } + +for (unsigned n =0 ; n < hdev->nvqs; ++n) { +VhostShadowVirtqueue *svq =vhost_svq_new (); + +if (unlikely (!svq)) { +error_setg (errp,"Cannot create svq %u" , n); +return -1 ; + } +g_ptr_array_add (v->shadow_vqs, svq); + } + +out: + v->shadow_vqs =g_steal_pointer (&shadow_vqs); +return 0 ; +} +static int vhost_vdpa_init (struct vhost_dev *dev,void *opaque,Error **errp) {struct vhost_vdpa *v;@@ -759,6 +869,10 @@ static int vhost_vdpa_init(struct vhost_dev *dev, void *opaque, Error **errp) dev->opaque = opaque ; v->listener = vhost_vdpa_memory_listener; v->msg_type =VHOST_IOTLB_MSG_V2 ; + ret =vhost_vdpa_init_svq (dev, v, errp); +if (ret) { + goto err; + }vhost_vdpa_get_iova_range (v);@@ -770,6 +884,10 @@ static int vhost_vdpa_init(struct vhost_dev *dev, void *opaque, Error **errp) VIRTIO_CONFIG_S_DRIVER);return 0 ; + +err: +ram_block_discard_disable (false); +return ret; }const VhostOps vdpa_ops = {
Eugenio Perez Martin
maintainerRe: [PATCH 09/31] vhost-vdpa: Take into account SVQ in vhost_vdpa_set_vring_call
In reply to Jason WangQuoted 39 lines · Jason Wang
Quoted 22 lines · Eugenio Pérez
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-vdpa.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)hw/virtio/vhost-vdpa.c +2
-2@@ -687,13 +687,29 @@ static int vhost_vdpa_set_vring_kick(struct vhost_dev *dev, } } -static int vhost_vdpa_set_vring_call (struct vhost_dev *dev, -struct vhost_vring_file *file) +static int vhost_vdpa_set_vring_dev_call (struct vhost_dev *dev, +struct vhost_vring_file *file) {trace_vhost_vdpa_set_vring_call (dev, file->index, file->fd);return vhost_vdpa_call (dev,VHOST_SET_VRING_CALL , file); }hw/virtio/vhost-vdpa.c static int vhost_vdpa_set_vring_kick(struct vhost_dev *dev, · line 706
Quoted 10 lines
hw/virtio/vhost-vdpa.c +10
-0+static int vhost_vdpa_set_vring_call (struct vhost_dev *dev, +struct vhost_vring_file *file) +{ +struct vhost_vdpa *v = dev->opaque; + +if (v->shadow_vqs_enabled) { +int vdpa_idx =vhost_vdpa_get_vq_index (dev, file->index); +VhostShadowVirtqueue *svq =g_ptr_array_index (v->shadow_vqs, vdpa_idx); + +vhost_svq_set_guest_call_notifier (svq, file->fd);Two questions here (had similar questions for vring kick):
Quoted 3 lines
1) Any reason that we setup the eventfd for vhost-vdpa in
vhost_vdpa_svq_setup()not here?
I'm not sure what you mean.
The guest->SVQ call and kick fds are set here and at vhost_vdpa_set_vring_kick. The event notifier handler of the guest -> SVQ kick_fd is set at vhost_vdpa_set_vring_kick / vhost_svq_set_svq_kick_fd. The guest -> SVQ call fd has no event notifier handler since we don't poll it.
On the other hand, the connection SVQ <-> device uses the same fds from the beginning to the end, and they will not change with, for example, call fd masking. That's why it's setup from vhost_vdpa_svq_setup. Delaying to vhost_vdpa_set_vring_call would make us add way more logic there.
Quoted 3 lines
2) The call could be disabled by using -1 as the fd, I don't see any code to deal with that.
Right, I didn't take that into account. vhost-kernel takes also -1 as kick_fd to unbind, so SVQ can be reworked to take that into account for sure.
Thanks!
Quoted 13 lines
Thanks
Quoted 9 lines
hw/virtio/vhost-vdpa.c +6
-0+return 0 ; + }else { +return vhost_vdpa_set_vring_dev_call (dev, file); + } +} +/** * Set shadow virtqueue descriptors to the device *
Eugenio Perez Martin
maintainerRe: [PATCH 11/31] vhost: Add vhost_svq_valid_device_features to shadow vq
In reply to Jason WangQuoted 54 lines · Jason Wang
Quoted 51 lines · Eugenio Pérez
This allows SVQ to negotiate features with the device. For the device, SVQ is a driver. While this function needs to bypass all non-transport features, it needs to disable the features that SVQ does not support when forwarding buffers. This includes packed vq layout, indirect descriptors or event idx.
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-shadow-virtqueue.h | 2 ++ hw/virtio/vhost-shadow-virtqueue.c | 44 ++++++++++++++++++++++++++++++ hw/virtio/vhost-vdpa.c | 21 ++++++++++++++ 3 files changed, 67 insertions(+)hw/virtio/vhost-shadow-virtqueue.h +2
-0@@ -15,6 +15,8 @@typedef struct VhostShadowVirtqueue VhostShadowVirtqueue ; +bool vhost_svq_valid_device_features (uint64_t *features); +void vhost_svq_set_svq_kick_fd (VhostShadowVirtqueue *svq,int svq_kick_fd);void vhost_svq_set_guest_call_notifier (VhostShadowVirtqueue *svq,int call_fd); const EventNotifier *vhost_svq_get_dev_kick_notifier(hw/virtio/vhost-shadow-virtqueue.c +17
-0@@ -45,6 +45,50 @@ const EventNotifier *vhost_svq_get_dev_kick_notifier(return &svq->hdev_kick; } +/** +* Validate the transport device features that SVQ can use with the device +* +* @dev_features The device features. If success, the acknowledged features. +* +* Returns true if SVQ can go with a subset of these, false otherwise. +*/ +bool vhost_svq_valid_device_features (uint64_t *dev_features) +{ +bool r = true; + +for (uint64_t b =VIRTIO_TRANSPORT_F_START ; b <=VIRTIO_TRANSPORT_F_END ; + ++b) { +switch (b) { +case VIRTIO_F_NOTIFY_ON_EMPTY : +case VIRTIO_F_ANY_LAYOUT : +continue ;
Quoted 9 lines
hw/virtio/vhost-shadow-virtqueue.c const EventNotifier *vhost_svq_get_dev_kick_notifier( · line 67
Quoted 3 lines
hw/virtio/vhost-shadow-virtqueue.c +3
-0+ +case VIRTIO_F_ACCESS_PLATFORM : +/* SVQ does not know how to translate addresses */ I may miss something but any reason that we need to disable
ACCESS_PLATFORM? I'd expect the vring helper we used for shadow virtqueue can deal with vIOMMU perfectly.
This function is validating SVQ <-> Device communications features, that may or may not be the same as guest <-> SVQ. These feature flags are valid for guest <-> SVQ communication, same as with indirect descriptors one.
Having said that, there is a point in the series where VIRTIO_F_ACCESS_PLATFORM is actually mandatory, so I think we could use the latter addition of x-svq cmdline parameter and delay the feature validations where it makes more sense.
Quoted 10 lines
hw/virtio/vhost-shadow-virtqueue.c const EventNotifier *vhost_svq_get_dev_kick_notifier( · line 74
Quoted 8 lines
hw/virtio/vhost-shadow-virtqueue.c +7
-0+if (*dev_features &BIT_ULL (b)) { +clear_bit (b, dev_features); + r = false; + } +break ; + +case VIRTIO_F_VERSION_1 :
Quoted 2 lines
I had the same question here.
For VERSION_1 it's easier to assume that guest is little endian at some points, but we could try harder to support both endianness if needed.
Thanks!
Quoted 60 lines
Thanks
Quoted 56 lines
hw/virtio/vhost-shadow-virtqueue.c +17
-0+/* SVQ trust that guest vring is little endian */ +if (!(*dev_features &BIT_ULL (b))) { +set_bit (b, dev_features); + r = false; + } +continue ; + +default : +if (*dev_features &BIT_ULL (b)) { +clear_bit (b, dev_features); + } + } + } + +return r; +} +/* Forward guest notifications */ static void vhost_handle_guest_kick (EventNotifier *n) {hw/virtio/vhost-vdpa.c +21
-0@@ -855,10 +855,31 @@ static int vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v,size_t n_svqs = v->shadow_vqs_enabled ? hdev->nvqs :0 ;g_autoptr (GPtrArray) shadow_vqs =g_ptr_array_new_full (n_svqs, vhost_psvq_free); +uint64_t dev_features; +uint64_t svq_features; +int r; +bool ok; +if (!v->shadow_vqs_enabled) { goto out; } + r =vhost_vdpa_get_features (hdev, &dev_features); +if (r !=0 ) { +error_setg (errp,"Can't get vdpa device features, got (%d)" , r); +return r; + } + + svq_features = dev_features; + ok =vhost_svq_valid_device_features (&svq_features); +if (unlikely (!ok)) { +error_setg (errp, +"SVQ Invalid device feature flags, offer: 0x%" PRIx64", ok: 0x%" PRIx64, + hdev->features, svq_features); +return -1 ; + } + + shadow_vqs =g_ptr_array_new_full (hdev->nvqs, vhost_psvq_free);for (unsigned n =0 ; n < hdev->nvqs; ++n) {VhostShadowVirtqueue *svq =vhost_svq_new ();
Eugenio Perez Martin
maintainerRe: [PATCH 15/31] vdpa: Add vhost_svq_get_num
In reply to Jason WangQuoted 10 lines · Jason Wang
Quoted 2 lines · Eugenio Pérez
This reports the guest's visible SVQ effective length, not the device's one.
I think we need to explain if there could be a case that the SVQ size is not equal to the device queue size.
The description is actually misleading now that I re-read it. It reports the size that the guest negotiated with SVQ for the guest's vring, not the one that SVQ negotiates with the device for SVQ's vring. I'll reword for the next version so thanks for pointing it out.
Regarding your comment, the only case it can happen is if SVQ cannot get device's num, something that we could make an error as you point out later in the series.
Thanks!
Quoted 39 lines
Thanks
Quoted 35 lines
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-shadow-virtqueue.h | 1 + hw/virtio/vhost-shadow-virtqueue.c | 5 +++++ 2 files changed, 6 insertions(+)hw/virtio/vhost-shadow-virtqueue.h +1
-0@@ -29,6 +29,7 @@ const EventNotifier *vhost_svq_get_svq_call_notifier( const VhostShadowVirtqueue *svq);void vhost_svq_get_vring_addr (const VhostShadowVirtqueue *svq,struct vhost_vring_addr *addr); +uint16_t vhost_svq_get_num (const VhostShadowVirtqueue *svq);size_t vhost_svq_driver_area_size (const VhostShadowVirtqueue *svq);size_t vhost_svq_device_area_size (const VhostShadowVirtqueue *svq);hw/virtio/vhost-shadow-virtqueue.c +5
-0@@ -212,6 +212,11 @@ void vhost_svq_get_vring_addr(const VhostShadowVirtqueue *svq, addr->used_user_addr = (uint64_t )svq->vring.used; } +uint16_t vhost_svq_get_num (const VhostShadowVirtqueue *svq) +{ +return svq->vring.num; +} +size_t vhost_svq_driver_area_size (const VhostShadowVirtqueue *svq) {size_t desc_size =sizeof (vring_desc_t) * svq->vring.num;
Eugenio Perez Martin
maintainerRe: [PATCH 16/31] vhost: pass queue index to vhost_vq_get_addr
In reply to Jason WangQuoted 25 lines · Jason Wang
Quoted 22 lines · Eugenio Pérez
Doing that way allows vhost backend to know what address to return.
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)hw/virtio/vhost.c +3
-2@@ -798,9 +798,10 @@ static int vhost_virtqueue_set_addr(struct vhost_dev *dev,struct vhost_virtqueue *vq, unsigned idx, bool enable_log) { - struct vhost_vring_addr addr; + struct vhost_vring_addr addr = { + .index = idx, + };int r; -memset (&addr,0 ,sizeof (struct vhost_vring_addr ));
Quoted 11 lines
hw/virtio/vhost.c static int vhost_virtqueue_set_addr(struct vhost_dev *dev, · line 816
Quoted 6 lines
hw/virtio/vhost.c +0
-0if (dev->vhost_ops->vhost_vq_get_addr) { r = dev->vhost_ops->vhost_vq_get_addr (dev, &addr, vq);@@ -813,7 +814,6 @@ static int vhost_virtqueue_set_addr(struct vhost_dev *dev, addr.avail_user_addr = (uint64_t )(unsigned long )vq->avail; addr.used_user_addr = (uint64_t )(unsigned long )vq->used; }I'm a bit lost in the logic above, any reason we need call
vhost_vq_get_addr():) ?
It's the way vhost_virtqueue_set_addr works if the backend has a vhost_vq_get_addr operation (currently, only vhost-vdpa). vhost first ask the address to the back end and then set it.
Previously, index was not needed because all the information was in vhost_virtqueue. However to extract queue index from vhost_virtqueue is tricky, so I think it's easier to simply have that information at request, something similar to get_base or get_num when asking vdpa device. We can extract the index from vq - dev->vqs or something similar if it's prefered.
Thanks!
Quoted 8 lines
Thanks
Quoted 4 lines
hw/virtio/vhost.c +0
-1- addr.index = idx; addr.log_guest_addr = vq->used_phys; addr.flags = enable_log ? (1 <<VHOST_VRING_F_LOG ) :0 ; r = dev->vhost_ops->vhost_set_vring_addr (dev, &addr);
Eugenio Perez Martin
maintainerRe: [PATCH 17/31] vdpa: adapt vhost_ops callbacks to svq
In reply to Jason WangQuoted 55 lines · Jason Wang
Quoted 52 lines · Eugenio Pérez
First half of the buffers forwarding part, preparing vhost-vdpa callbacks to SVQ to offer it. QEMU cannot enable it at this moment, so this is effectively dead code at the moment, but it helps to reduce patch size.
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-shadow-virtqueue.h | 2 +- hw/virtio/vhost-shadow-virtqueue.c | 21 ++++- hw/virtio/vhost-vdpa.c | 133 ++++++++++++++++++++++++++--- 3 files changed, 143 insertions(+), 13 deletions(-)hw/virtio/vhost-shadow-virtqueue.h +1
-1@@ -35,7 +35,7 @@ size_t vhost_svq_device_area_size(const VhostShadowVirtqueue *svq);void vhost_svq_stop (VhostShadowVirtqueue *svq); -VhostShadowVirtqueue *vhost_svq_new (void ); +VhostShadowVirtqueue *vhost_svq_new (uint16_t qsize);void vhost_svq_free (VhostShadowVirtqueue *vq);hw/virtio/vhost-shadow-virtqueue.c +9
-1@@ -277,9 +277,17 @@ void vhost_svq_stop(VhostShadowVirtqueue *svq)/** * Creates vhost shadow virtqueue, and instruct vhost device to use the shadow * methods and file descriptors. +* +* @qsize Shadow VirtQueue size +* +* Returns the new virtqueue or NULL. +* +* In case of error, reason is reported through error_report. */ -VhostShadowVirtqueue *vhost_svq_new (void ) +VhostShadowVirtqueue *vhost_svq_new (uint16_t qsize) { +size_t desc_size =sizeof (vring_desc_t) * qsize; +size_t device_size, driver_size; g_autofree VhostShadowVirtqueue *svq = g_new0(VhostShadowVirtqueue, 1);int r;@@ -300,6 +308,15 @@ VhostShadowVirtqueue *vhost_svq_new(void)/* Placeholder descriptor, it should be deleted at set_kick_fd */ event_notifier_init_fd (&svq->svq_kick,INVALID_SVQ_KICK_FD );
Quoted 9 lines
hw/virtio/vhost-shadow-virtqueue.c VhostShadowVirtqueue *vhost_svq_new(void) · line 311
Quoted 1 line
hw/virtio/vhost-shadow-virtqueue.c +1
-0+ svq->vring.num = qsize;I wonder if this is the best. E.g some hardware can support up to 32K queue size. So this will probably end up with:
1) SVQ use 32K queue size
2) hardware queue uses 256
In that case SVQ vring queue size will be 32K and guest's vring can negotiate any number with SVQ equal or less than 32K, including 256. Is that what you mean?
If with hardware queues you mean guest's vring, not sure why it is "probably 256". I'd say that in that case with the virtio-net kernel driver the ring size will be the same as the device export, for example, isn't it?
The implementation should support any combination of sizes, but the ring size exposed to the guest is never bigger than hardware one.
Quoted 3 lines
? Or we SVQ can stick to 256 but this will this cause trouble if we want to add event index support?
I think we should not have any problem with event idx. If you mean that the guest could mark more buffers available than SVQ vring's size, that should not happen because there must be less entries in the guest than SVQ.
But if I understood you correctly, a similar situation could happen if a guest's contiguous buffer is scattered across many qemu's VA chunks. Even if that would happen, the situation should be ok too: SVQ knows the guest's avail idx and, if SVQ is full, it will continue forwarding avail buffers when the device uses more buffers.
Does that make sense to you?
Quoted 96 lines
Quoted 96 lines
hw/virtio/vhost-shadow-virtqueue.c +10
-0+ driver_size =vhost_svq_driver_area_size (svq); + device_size =vhost_svq_device_area_size (svq); + svq->vring.desc =qemu_memalign (qemu_real_host_page_size, driver_size); + svq->vring.avail = (void *)((char *)svq->vring.desc + desc_size); +memset (svq->vring.desc,0 , driver_size); + svq->vring.used =qemu_memalign (qemu_real_host_page_size, device_size); +memset (svq->vring.used,0 , device_size); +event_notifier_set_handler (&svq->hdev_call, vhost_svq_handle_call);return g_steal_pointer (&svq);@@ -318,5 +335,7 @@ void vhost_svq_free(VhostShadowVirtqueue *vq)event_notifier_cleanup (&vq->hdev_kick);event_notifier_set_handler (&vq->hdev_call,NULL );event_notifier_cleanup (&vq->hdev_call); +qemu_vfree (vq->vring.desc); +qemu_vfree (vq->vring.used);g_free (vq); }hw/virtio/vhost-vdpa.c +38
-6@@ -641,20 +641,52 @@ static int vhost_vdpa_set_vring_addr(struct vhost_dev *dev,return vhost_vdpa_call (dev,VHOST_SET_VRING_ADDR , addr); } -static int vhost_vdpa_set_vring_num (struct vhost_dev *dev, -struct vhost_vring_state *ring) +static int vhost_vdpa_set_dev_vring_num (struct vhost_dev *dev, +struct vhost_vring_state *ring) {trace_vhost_vdpa_set_vring_num (dev, ring->index, ring->num);return vhost_vdpa_call (dev,VHOST_SET_VRING_NUM , ring); } -static int vhost_vdpa_set_vring_base (struct vhost_dev *dev, -struct vhost_vring_state *ring) +static int vhost_vdpa_set_vring_num (struct vhost_dev *dev, +struct vhost_vring_state *ring) +{ +struct vhost_vdpa *v = dev->opaque; + +if (v->shadow_vqs_enabled) { +/* +* Vring num was set at device start. SVQ num is handled by VirtQueue +* code +*/ +return 0 ; + } + +return vhost_vdpa_set_dev_vring_num (dev, ring); +} + +static int vhost_vdpa_set_dev_vring_base (struct vhost_dev *dev, +struct vhost_vring_state *ring) {trace_vhost_vdpa_set_vring_base (dev, ring->index, ring->num);return vhost_vdpa_call (dev,VHOST_SET_VRING_BASE , ring); } +static int vhost_vdpa_set_vring_base (struct vhost_dev *dev, +struct vhost_vring_state *ring) +{ +struct vhost_vdpa *v = dev->opaque; + +if (v->shadow_vqs_enabled) { +/* +* Vring base was set at device start. SVQ base is handled by VirtQueue +* code +*/ +return 0 ; + } + +return vhost_vdpa_set_dev_vring_base (dev, ring); +} +static int vhost_vdpa_get_vring_base (struct vhost_dev *dev,struct vhost_vring_state *ring) {@@ -784,8 +816,8 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started) } } -static int vhost_vdpa_get_features (struct vhost_dev *dev, -uint64_t *features) +static int vhost_vdpa_get_dev_features (struct vhost_dev *dev, +uint64_t *features) {int ret;@@ -794,15 +826,64 @@ static int vhost_vdpa_get_features(struct vhost_dev *dev,return ret; }
Quoted 15 lines
hw/virtio/vhost-vdpa.c static int vhost_vdpa_get_features(struct vhost_dev *dev, · line 837
Quoted 9 lines
hw/virtio/vhost-vdpa.c +9
-0+static int vhost_vdpa_get_features (struct vhost_dev *dev,uint64_t *features) +{ +struct vhost_vdpa *v = dev->opaque; +int ret =vhost_vdpa_get_dev_features (dev, features); + +if (ret ==0 && v->shadow_vqs_enabled) { +/* Filter only features that SVQ can offer to guest */ +vhost_svq_valid_guest_features (features); + }Sorry if I've asked before, I think it's sufficient to filter out the device features that we don't support during and fail the vhost initialization. Any reason we need do it again here?
On the contrary, if something needs to be asked that means that is not clear enough :).
At initialization we validate that the device offers all the needed features (ACCESS_PLATFORM, VERSION_1). We don't have the features acknowledged by the guest at that point.
This is checking the written features by the guest. For example, we accept _F_INDIRECT here, so the guest can write indirect descriptors to SVQ, since qemu VirtQueue code is handling it for us. I've stayed on the safe side and I've not included packed or event_idx, but I might try to run with them. These would not have been accepted for the device.
Quoted 122 lines
Quoted 122 lines
hw/virtio/vhost-vdpa.c +69
-4+ +return ret; +} +static int vhost_vdpa_set_features (struct vhost_dev *dev,uint64_t features) { +struct vhost_vdpa *v = dev->opaque;int ret;if (vhost_vdpa_one_time_request (dev)) {return 0 ; } +if (v->shadow_vqs_enabled) { +uint64_t dev_features, svq_features, acked_features; +bool ok; + + ret =vhost_vdpa_get_dev_features (dev, &dev_features); +if (ret !=0 ) { +error_report ("Can't get vdpa device features, got (%d)" , ret); +return ret; + } + + svq_features = dev_features; + ok =vhost_svq_valid_device_features (&svq_features); +if (unlikely (!ok)) { +error_report ("SVQ Invalid device feature flags, offer: 0x%" + PRIx64", ok: 0x%" PRIx64, dev->features, svq_features); +return -1 ; + } + + ok =vhost_svq_valid_guest_features (&features); +if (unlikely (!ok)) { +error_report ( +"Invalid guest acked feature flag, acked: 0x%" + PRIx64", ok: 0x%" PRIx64, dev->acked_features, features); +return -1 ; + } + + ok =vhost_svq_ack_guest_features (svq_features, features, + &acked_features); +if (unlikely (!ok)) { +return -1 ; + } + + features = acked_features; + } +trace_vhost_vdpa_set_features (dev, features); ret =vhost_vdpa_call (dev,VHOST_SET_FEATURES , &features);if (ret) {@@ -822,13 +903,31 @@ static int vhost_vdpa_set_owner(struct vhost_dev *dev)return vhost_vdpa_call (dev,VHOST_SET_OWNER ,NULL ); } -static int vhost_vdpa_vq_get_addr (struct vhost_dev *dev, -struct vhost_vring_addr *addr,struct vhost_virtqueue *vq) +static void vhost_vdpa_vq_get_guest_addr (struct vhost_vring_addr *addr, +struct vhost_virtqueue *vq) { -assert (dev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_VDPA ); addr->desc_user_addr = (uint64_t )(unsigned long )vq->desc_phys; addr->avail_user_addr = (uint64_t )(unsigned long )vq->avail_phys; addr->used_user_addr = (uint64_t )(unsigned long )vq->used_phys; +} + +static int vhost_vdpa_vq_get_addr (struct vhost_dev *dev, +struct vhost_vring_addr *addr, +struct vhost_virtqueue *vq) +{ +struct vhost_vdpa *v = dev->opaque; + +assert (dev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_VDPA ); + +if (v->shadow_vqs_enabled) { +int idx =vhost_vdpa_get_vq_index (dev, addr->index); +VhostShadowVirtqueue *svq =g_ptr_array_index (v->shadow_vqs, idx); + +vhost_svq_get_vring_addr (svq, addr); + }else { +vhost_vdpa_vq_get_guest_addr (addr, vq); + } +trace_vhost_vdpa_vq_get_addr (dev, vq, addr->desc_user_addr, addr->avail_user_addr, addr->used_user_addr);return 0 ;@@ -849,6 +948,12 @@ static void vhost_psvq_free(gpointer svq)vhost_svq_free (svq); } +static int vhost_vdpa_get_max_queue_size (struct vhost_dev *dev, +uint16_t *qsize) +{ +return vhost_vdpa_call (dev,VHOST_VDPA_GET_VRING_NUM , qsize); +} +static int vhost_vdpa_init_svq (struct vhost_dev *hdev,struct vhost_vdpa *v,Error **errp) {@@ -857,6 +962,7 @@ static int vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v, vhost_psvq_free);uint64_t dev_features;uint64_t svq_features; +uint16_t qsize;int r;bool ok;@@ -864,7 +970,7 @@ static int vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v, goto out; } - r =vhost_vdpa_get_features (hdev, &dev_features); + r =vhost_vdpa_get_dev_features (hdev, &dev_features);if (r !=0 ) {error_setg (errp,"Can't get vdpa device features, got (%d)" , r);return r;@@ -879,9 +985,14 @@ static int vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v,return -1 ; }
Quoted 8 lines
hw/virtio/vhost-vdpa.c static int vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v, · line 991
Quoted 4 lines
hw/virtio/vhost-vdpa.c +4
-0+ r =vhost_vdpa_get_max_queue_size (hdev, &qsize); +if (unlikely (r)) { + qsize =256 ; + }Should we fail instead of having a "default" value here?
Maybe it is better to fail here, yes. I guess there is no safe default value.
Thanks!
Quoted 12 lines
Thanks
Quoted 8 lines
hw/virtio/vhost-vdpa.c +2
-1+ shadow_vqs =g_ptr_array_new_full (hdev->nvqs, vhost_psvq_free);for (unsigned n =0 ; n < hdev->nvqs; ++n) { -VhostShadowVirtqueue *svq =vhost_svq_new (); +VhostShadowVirtqueue *svq =vhost_svq_new (qsize);if (unlikely (!svq)) {error_setg (errp,"Cannot create svq %u" , n);
Eugenio Perez Martin
maintainerRe: [PATCH 23/31] vdpa: Add custom IOTLB translations to SVQ
In reply to Jason WangQuoted 129 lines · Jason Wang
Quoted 126 lines · Eugenio Pérez
Use translations added in VhostIOVATree in SVQ.
Only introduce usage here, not allocation and deallocation. As with previous patches, we use the dead code paths of
shadow_vqs_enabledto avoid commiting too many changes at once. These are impossible to take at the moment.Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-shadow-virtqueue.h | 3 +- include/hw/virtio/vhost-vdpa.h | 3 + hw/virtio/vhost-shadow-virtqueue.c | 111 ++++++++++++++++---- hw/virtio/vhost-vdpa.c | 161 +++++++++++++++++++++++++---- 4 files changed, 238 insertions(+), 40 deletions(-)hw/virtio/vhost-shadow-virtqueue.h +2
-1@@ -12,6 +12,7 @@#include "hw/virtio/vhost.h" #include "qemu/event_notifier.h" +#include "hw/virtio/vhost-iova-tree.h" typedef struct VhostShadowVirtqueue VhostShadowVirtqueue ;@@ -37,7 +38,7 @@ void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIODevice *vdev, VirtQueue *vq);void vhost_svq_stop (VhostShadowVirtqueue *svq); -VhostShadowVirtqueue *vhost_svq_new (uint16_t qsize); +VhostShadowVirtqueue *vhost_svq_new (uint16_t qsize,VhostIOVATree *iova_map);void vhost_svq_free (VhostShadowVirtqueue *vq);include/hw/virtio/vhost-vdpa.h +3
-0@@ -14,6 +14,7 @@#include <gmodule.h> +#include "hw/virtio/vhost-iova-tree.h" #include "hw/virtio/virtio.h" #include "standard-headers/linux/vhost_types.h" @@ -30,6 +31,8 @@ typedef struct vhost_vdpa {MemoryListener listener;struct vhost_vdpa_iova_range iova_range;bool shadow_vqs_enabled; +/* IOVA mapping used by Shadow Virtqueue */ +VhostIOVATree *iova_tree;GPtrArray *shadow_vqs;struct vhost_dev *dev;VhostVDPAHostNotifier notifier[VIRTIO_QUEUE_MAX ];hw/virtio/vhost-shadow-virtqueue.c +33
-7@@ -11,6 +11,7 @@#include "hw/virtio/vhost-shadow-virtqueue.h" #include "hw/virtio/vhost.h" #include "hw/virtio/virtio-access.h" +#include "hw/virtio/vhost-iova-tree.h" #include "standard-headers/linux/vhost_types.h" #include "qemu/error-report.h" @@ -45,6 +46,9 @@ typedef struct VhostShadowVirtqueue {/* Virtio device */ VirtIODevice *vdev; +/* IOVA mapping */ +VhostIOVATree *iova_tree; +/* Map for returning guest's descriptors */ VirtQueueElement **ring_id_maps;@@ -97,13 +101,7 @@ bool vhost_svq_valid_device_features(uint64_t *dev_features)continue ;case VIRTIO_F_ACCESS_PLATFORM : -/* SVQ does not know how to translate addresses */ -if (*dev_features &BIT_ULL (b)) { -clear_bit (b, dev_features); - r = false; - } -break ; - +/* SVQ trust in host's IOMMU to translate addresses */ case VIRTIO_F_VERSION_1 :/* SVQ trust that guest vring is little endian */ if (!(*dev_features &BIT_ULL (b))) {@@ -205,7 +203,55 @@ static void vhost_svq_set_notification(VhostShadowVirtqueue *svq, bool enable) } } +/** +* Translate addresses between qemu's virtual address and SVQ IOVA +* +* @svq Shadow VirtQueue +* @vaddr Translated IOVA addresses +* @iovec Source qemu's VA addresses +* @num Length of iovec and minimum length of vaddr +*/ +static bool vhost_svq_translate_addr (const VhostShadowVirtqueue *svq, +void **addrs,const struct iovec *iovec, +size_t num) +{ +size_t i; + +if (num ==0 ) { +return true; + } + +for (i =0 ; i < num; ++i) { +DMAMap needle = { + .translated_addr = (hwaddr )iovec[i].iov_base, + .size = iovec[i].iov_len, + }; +size_t off; + +const DMAMap *map =vhost_iova_tree_find_iova (svq->iova_tree, &needle); +/* +* Map cannot be NULL since iova map contains all guest space and
Quoted 9 lines
hw/virtio/vhost-shadow-virtqueue.c static void vhost_svq_set_notification(VhostShadowVirtqueue *svq, bool enable) · line 238
Quoted 5 lines
hw/virtio/vhost-shadow-virtqueue.c +5
-0+* qemu already has a physical address mapped +*/ +if (unlikely (!map)) { +error_report ("Invalid address 0x%" HWADDR_PRIx" given by guest" , + needle.translated_addr);This can be triggered by guest, we need use once or
log_guest_error()etc.
Ok I see the issue, I will change.
Quoted 10 lines
hw/virtio/vhost-shadow-virtqueue.c static void vhost_svq_set_notification(VhostShadowVirtqueue *svq, bool enable) · line 245
Quoted 8 lines
hw/virtio/vhost-shadow-virtqueue.c +7
-0+return false; + } + +/* +* Map->iova chunk size is ignored. What to do if descriptor +* (addr, size) does not fit is delegated to the device. +*/
Quoted 4 lines
I think we need at least check the size and fail if the size doesn't match here. Or is it possible that we have a buffer that may cross two memory regions?
It should be impossible, since both iova_tree and VirtQueue should be in sync regarding the memory regions updates. If a VirtQueue buffer crosses many memory regions, iovec has more entries.
I can add a return false, but I'm not able to trigger that situation even with a malformed driver.
Quoted 26 lines
Quoted 26 lines
hw/virtio/vhost-shadow-virtqueue.c +9
-1+ off = needle.translated_addr - map->translated_addr; + addrs[i] = (void *)(map->iova + off); + } + +return true; +} +static void vhost_vring_write_descs (VhostShadowVirtqueue *svq, +void *const *vaddr_sg,const struct iovec *iovec,size_t num,bool more_descs,bool write) {@@ -224,7 +270,7 @@ static void vhost_vring_write_descs(VhostShadowVirtqueue *svq, }else { descs[i].flags = flags; } - descs[i].addr =cpu_to_le64 ((hwaddr )iovec[n].iov_base); + descs[i].addr =cpu_to_le64 ((hwaddr )vaddr_sg[n]); descs[i].len =cpu_to_le32 (iovec[n].iov_len); last = i;@@ -234,42 +280,60 @@ static void vhost_vring_write_descs(VhostShadowVirtqueue *svq, svq->free_head =le16_to_cpu (descs[last].next); }
Quoted 10 lines
hw/virtio/vhost-shadow-virtqueue.c static void vhost_vring_write_descs(VhostShadowVirtqueue *svq, · line 285
Quoted 5 lines
hw/virtio/vhost-shadow-virtqueue.c +3
-2-static unsigned vhost_svq_add_split (VhostShadowVirtqueue *svq, -VirtQueueElement *elem) +static bool vhost_svq_add_split (VhostShadowVirtqueue *svq, +VirtQueueElement *elem, +unsigned *head)I'd suggest to make it returns bool since the patch that introduces this function.
Ok I will do it from the start.
Quoted 114 lines
Quoted 114 lines
hw/virtio/vhost-shadow-virtqueue.c +34
-10{ -int head;unsigned avail_idx;vring_avail_t *avail = svq->vring.avail; +bool ok; + g_autofree void **sgs = g_new(void *, MAX(elem->out_num, elem->in_num)); - head = svq->free_head; + *head = svq->free_head;/* We need some descriptors here */ assert (elem->out_num || elem->in_num); -vhost_vring_write_descs (svq, elem->out_sg, elem->out_num, + ok =vhost_svq_translate_addr (svq, sgs, elem->out_sg, elem->out_num); +if (unlikely (!ok)) { +return false; + } +vhost_vring_write_descs (svq, sgs, elem->out_sg, elem->out_num, elem->in_num >0 , false); -vhost_vring_write_descs (svq, elem->in_sg, elem->in_num, false, true); + + + ok =vhost_svq_translate_addr (svq, sgs, elem->in_sg, elem->in_num); +if (unlikely (!ok)) { +return false; + } + +vhost_vring_write_descs (svq, sgs, elem->in_sg, elem->in_num, false, true);/* * Put entry in available array (but don't update avail->idx until they * do sync). */ avail_idx = svq->avail_idx_shadow & (svq->vring.num -1 ); - avail->ring[avail_idx] =cpu_to_le16 (head); + avail->ring[avail_idx] =cpu_to_le16 (*head); svq->avail_idx_shadow++;/* Update avail index after the descriptor is wrote */ smp_wmb (); avail->idx =cpu_to_le16 (svq->avail_idx_shadow); -return head; +return true; } -static void vhost_svq_add (VhostShadowVirtqueue *svq,VirtQueueElement *elem) +static bool vhost_svq_add (VhostShadowVirtqueue *svq,VirtQueueElement *elem) { -unsigned qemu_head =vhost_svq_add_split (svq, elem); +unsigned qemu_head; +bool ok =vhost_svq_add_split (svq, elem, &qemu_head); +if (unlikely (!ok)) { +return false; + } svq->ring_id_maps[qemu_head] = elem; +return true; }static void vhost_svq_kick (VhostShadowVirtqueue *svq)@@ -309,6 +373,7 @@ static void vhost_handle_guest_kick(VhostShadowVirtqueue *svq)while (true) {VirtQueueElement *elem; +bool ok;if (svq->next_guest_avail_elem) { elem =g_steal_pointer (&svq->next_guest_avail_elem);@@ -337,7 +402,11 @@ static void vhost_handle_guest_kick(VhostShadowVirtqueue *svq)return ; } -vhost_svq_add (svq, elem); + ok =vhost_svq_add (svq, elem); +if (unlikely (!ok)) { +/* VQ is broken, just return and ignore any other kicks */ +return ; + }vhost_svq_kick (svq); }@@ -619,12 +688,13 @@ void vhost_svq_stop(VhostShadowVirtqueue *svq)* methods and file descriptors. * * @qsize Shadow VirtQueue size +* @iova_tree Tree to perform descriptors translations * * Returns the new virtqueue or NULL. * * In case of error, reason is reported through error_report. */ -VhostShadowVirtqueue *vhost_svq_new (uint16_t qsize) +VhostShadowVirtqueue *vhost_svq_new (uint16_t qsize,VhostIOVATree *iova_tree) {size_t desc_size =sizeof (vring_desc_t) * qsize;size_t device_size, driver_size;@@ -656,6 +726,7 @@ VhostShadowVirtqueue *vhost_svq_new(uint16_t qsize)memset (svq->vring.desc,0 , driver_size); svq->vring.used =qemu_memalign (qemu_real_host_page_size, device_size);memset (svq->vring.used,0 , device_size); + svq->iova_tree = iova_tree; svq->ring_id_maps = g_new0(VirtQueueElement *, qsize);event_notifier_set_handler (&svq->hdev_call, vhost_svq_handle_call);return g_steal_pointer (&svq);hw/virtio/vhost-vdpa.c +0
-0@@ -209,6 +209,18 @@ static void vhost_vdpa_listener_region_add(MemoryListener *listener, vaddr, section->readonly);
Quoted 14 lines
hw/virtio/vhost-vdpa.c static void vhost_vdpa_listener_region_add(MemoryListener *listener, · line 220
Quoted 10 lines
hw/virtio/vhost-vdpa.c +9
-0llsize =int128_sub (llend,int128_make64 (iova)); +if (v->shadow_vqs_enabled) { +DMAMap mem_region = { + .translated_addr = (hwaddr )vaddr, + .size =int128_get64 (llsize) -1 , + .perm =IOMMU_ACCESS_FLAG (true, section->readonly), + }; + +int r =vhost_iova_tree_map_alloc (v->iova_tree, &mem_region); +assert (r ==IOVA_OK );It's better to fail or warn here.
Sure, a warning is possible.
Quoted 222 lines
Quoted 222 lines
hw/virtio/vhost-vdpa.c +124
-19+ + iova = mem_region.iova; + }vhost_vdpa_iotlb_batch_begin_once (v); ret = vhost_vdpa_dma_map(v, iova, int128_get64(llsize),@@ -261,6 +273,20 @@ static void vhost_vdpa_listener_region_del(MemoryListener *listener, llsize =int128_sub (llend,int128_make64 (iova)); +if (v->shadow_vqs_enabled) { +const DMAMap *result; +const void *vaddr =memory_region_get_ram_ptr (section->mr) + + section->offset_within_region + + (iova - section->offset_within_address_space); +DMAMap mem_region = { + .translated_addr = (hwaddr )vaddr, + .size =int128_get64 (llsize) -1 , + }; + + result =vhost_iova_tree_find_iova (v->iova_tree, &mem_region); + iova = result->iova; +vhost_iova_tree_remove (v->iova_tree, &mem_region); + }vhost_vdpa_iotlb_batch_begin_once (v); ret =vhost_vdpa_dma_unmap (v, iova,int128_get64 (llsize));if (ret) {@@ -783,33 +809,70 @@ static int vhost_vdpa_svq_set_fds(struct vhost_dev *dev,/** * Unmap SVQ area in the device */ -static bool vhost_vdpa_svq_unmap_ring (struct vhost_vdpa *v,hwaddr iova, -hwaddr size) +static bool vhost_vdpa_svq_unmap_ring (struct vhost_vdpa *v, +const DMAMap *needle) { +const DMAMap *result =vhost_iova_tree_find_iova (v->iova_tree, needle); +hwaddr size;int r; - size =ROUND_UP (size, qemu_real_host_page_size); - r =vhost_vdpa_dma_unmap (v, iova, size); +if (unlikely (!result)) { +error_report ("Unable to find SVQ address to unmap" ); +return false; + } + + size =ROUND_UP (result->size, qemu_real_host_page_size); + r =vhost_vdpa_dma_unmap (v, result->iova, size);return r ==0 ; }static bool vhost_vdpa_svq_unmap_rings (struct vhost_dev *dev,const VhostShadowVirtqueue *svq) { +DMAMap needle;struct vhost_vdpa *v = dev->opaque;struct vhost_vring_addr svq_addr; -size_t device_size =vhost_svq_device_area_size (svq); -size_t driver_size =vhost_svq_driver_area_size (svq);bool ok;vhost_svq_get_vring_addr (svq, &svq_addr); - ok =vhost_vdpa_svq_unmap_ring (v, svq_addr.desc_user_addr, driver_size); + needle = (DMAMap ) { + .translated_addr = svq_addr.desc_user_addr, + }; + ok =vhost_vdpa_svq_unmap_ring (v, &needle);if (unlikely (!ok)) {return false; } -return vhost_vdpa_svq_unmap_ring (v, svq_addr.used_user_addr, device_size); + needle = (DMAMap ) { + .translated_addr = svq_addr.used_user_addr, + }; +return vhost_vdpa_svq_unmap_ring (v, &needle); +} + +/** +* Map SVQ area in the device +* +* @v Vhost-vdpa device +* @needle The area to search iova +* @readonly Permissions of the area +*/ +static bool vhost_vdpa_svq_map_ring (struct vhost_vdpa *v,const DMAMap *needle, +bool readonly) +{ +hwaddr off; +const DMAMap *result =vhost_iova_tree_find_iova (v->iova_tree, needle); +int r; + +if (unlikely (!result)) { +error_report ("Can't locate SVQ ring" ); +return false; + } + + off = needle->translated_addr - result->translated_addr; + r =vhost_vdpa_dma_map (v, result->iova + off, needle->size, + (void *)needle->translated_addr, readonly); +return r ==0 ; }/** @@ -821,23 +884,29 @@ static bool vhost_vdpa_svq_unmap_rings(struct vhost_dev *dev,static bool vhost_vdpa_svq_map_rings (struct vhost_dev *dev,const VhostShadowVirtqueue *svq) { +DMAMap needle;struct vhost_vdpa *v = dev->opaque;struct vhost_vring_addr svq_addr;size_t device_size =vhost_svq_device_area_size (svq);size_t driver_size =vhost_svq_driver_area_size (svq); -int r; +bool ok;vhost_svq_get_vring_addr (svq, &svq_addr); - r =vhost_vdpa_dma_map (v, svq_addr.desc_user_addr, driver_size, - (void *)svq_addr.desc_user_addr, true); -if (unlikely (r !=0 )) { + needle = (DMAMap ) { + .translated_addr = svq_addr.desc_user_addr, + .size = driver_size, + }; + ok =vhost_vdpa_svq_map_ring (v, &needle, true); +if (unlikely (!ok)) {return false; } - r =vhost_vdpa_dma_map (v, svq_addr.used_user_addr, device_size, - (void *)svq_addr.used_user_addr, false); -return r ==0 ; + needle = (DMAMap ) { + .translated_addr = svq_addr.used_user_addr, + .size = device_size, + }; +return vhost_vdpa_svq_map_ring (v, &needle, false); } static bool vhost_vdpa_svq_setup(struct vhost_dev *dev,@@ -1006,6 +1075,23 @@ static int vhost_vdpa_set_owner(struct vhost_dev *dev)return vhost_vdpa_call (dev,VHOST_SET_OWNER ,NULL ); } +static bool vhost_vdpa_svq_get_vq_region (struct vhost_vdpa *v, +unsigned long long addr, +uint64_t *iova_addr) +{ +const DMAMap needle = { + .translated_addr = addr, + }; +const DMAMap *translation =vhost_iova_tree_find_iova (v->iova_tree, + &needle); +if (!translation) { +return false; + } + + *iova_addr = translation->iova + (addr - translation->translated_addr); +return true; +} +static void vhost_vdpa_vq_get_guest_addr (struct vhost_vring_addr *addr,struct vhost_virtqueue *vq) {@@ -1023,10 +1109,23 @@ static int vhost_vdpa_vq_get_addr(struct vhost_dev *dev,assert (dev->vhost_ops->backend_type ==VHOST_BACKEND_TYPE_VDPA );if (v->shadow_vqs_enabled) { +struct vhost_vring_addr svq_addr;int idx =vhost_vdpa_get_vq_index (dev, addr->index);VhostShadowVirtqueue *svq =g_ptr_array_index (v->shadow_vqs, idx); -vhost_svq_get_vring_addr (svq, addr); +vhost_svq_get_vring_addr (svq, &svq_addr); +if (!vhost_vdpa_svq_get_vq_region (v, svq_addr.desc_user_addr, + &addr->desc_user_addr)) { +return -1 ; + } +if (!vhost_vdpa_svq_get_vq_region (v, svq_addr.avail_user_addr, + &addr->avail_user_addr)) { +return -1 ; + } +if (!vhost_vdpa_svq_get_vq_region (v, svq_addr.used_user_addr, + &addr->used_user_addr)) { +return -1 ; + } }else {vhost_vdpa_vq_get_guest_addr (addr, vq); }@@ -1095,13 +1194,37 @@ static int vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v, shadow_vqs =g_ptr_array_new_full (hdev->nvqs, vhost_psvq_free);for (unsigned n =0 ; n < hdev->nvqs; ++n) { -VhostShadowVirtqueue *svq =vhost_svq_new (qsize); - +DMAMap device_region, driver_region; +struct vhost_vring_addr addr; +VhostShadowVirtqueue *svq =vhost_svq_new (qsize, v->iova_tree);if (unlikely (!svq)) {error_setg (errp,"Cannot create svq %u" , n);return -1 ; } -g_ptr_array_add (v->shadow_vqs, svq); + +vhost_svq_get_vring_addr (svq, &addr); + driver_region = (DMAMap ) { + .translated_addr = (hwaddr )addr.desc_user_addr, + +/* +* DMAMAp.size include the last byte included in the range, while +* sizeof marks one past it. Substract one byte to make them match. +*/ + .size =vhost_svq_driver_area_size (svq) -1 , + .perm =VHOST_ACCESS_RO , + }; + device_region = (DMAMap ) { + .translated_addr = (hwaddr )addr.used_user_addr, + .size =vhost_svq_device_area_size (svq) -1 ,
Quoted 9 lines
hw/virtio/vhost-vdpa.c static int vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v, · line 1223
Quoted 5 lines
hw/virtio/vhost-vdpa.c +5
-0+ .perm = VHOST_ACCESS_RW, + }; + + r =vhost_iova_tree_map_alloc (v->iova_tree, &driver_region); +assert (r ==IOVA_OK );Let's fail instead of assert here.
Sure, I see how we must not assert here too.
Thanks!
Quoted 11 lines
Thanks
Quoted 7 lines
hw/virtio/vhost-vdpa.c +4
-0+ r =vhost_iova_tree_map_alloc (v->iova_tree, &device_region); +assert (r ==IOVA_OK ); + +g_ptr_array_add (shadow_vqs, svq); } out:
Eugenio Perez Martin
maintainerRe: [PATCH 11/31] vhost: Add vhost_svq_valid_device_features to shadow vq
In reply to Eugenio Perez MartinQuoted 86 lines · Eugenio Perez Martin
Quoted 54 lines · Jason Wang
Quoted 51 lines · Eugenio Pérez
This allows SVQ to negotiate features with the device. For the device, SVQ is a driver. While this function needs to bypass all non-transport features, it needs to disable the features that SVQ does not support when forwarding buffers. This includes packed vq layout, indirect descriptors or event idx.
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-shadow-virtqueue.h | 2 ++ hw/virtio/vhost-shadow-virtqueue.c | 44 ++++++++++++++++++++++++++++++ hw/virtio/vhost-vdpa.c | 21 ++++++++++++++ 3 files changed, 67 insertions(+)hw/virtio/vhost-shadow-virtqueue.h +2
-0@@ -15,6 +15,8 @@typedef struct VhostShadowVirtqueue VhostShadowVirtqueue ; +bool vhost_svq_valid_device_features (uint64_t *features); +void vhost_svq_set_svq_kick_fd (VhostShadowVirtqueue *svq,int svq_kick_fd);void vhost_svq_set_guest_call_notifier (VhostShadowVirtqueue *svq,int call_fd); const EventNotifier *vhost_svq_get_dev_kick_notifier(hw/virtio/vhost-shadow-virtqueue.c +17
-0@@ -45,6 +45,50 @@ const EventNotifier *vhost_svq_get_dev_kick_notifier(return &svq->hdev_kick; } +/** +* Validate the transport device features that SVQ can use with the device +* +* @dev_features The device features. If success, the acknowledged features. +* +* Returns true if SVQ can go with a subset of these, false otherwise. +*/ +bool vhost_svq_valid_device_features (uint64_t *dev_features) +{ +bool r = true; + +for (uint64_t b =VIRTIO_TRANSPORT_F_START ; b <=VIRTIO_TRANSPORT_F_END ; + ++b) { +switch (b) { +case VIRTIO_F_NOTIFY_ON_EMPTY : +case VIRTIO_F_ANY_LAYOUT : +continue ;Quoted 9 lines
hw/virtio/vhost-shadow-virtqueue.c const EventNotifier *vhost_svq_get_dev_kick_notifier( · line 67
Quoted 3 lines
hw/virtio/vhost-shadow-virtqueue.c +3
-0+ +case VIRTIO_F_ACCESS_PLATFORM : +/* SVQ does not know how to translate addresses */ I may miss something but any reason that we need to disable
ACCESS_PLATFORM? I'd expect the vring helper we used for shadow virtqueue can deal with vIOMMU perfectly.This function is validating SVQ <-> Device communications features, that may or may not be the same as guest <-> SVQ. These feature flags are valid for guest <-> SVQ communication, same as with indirect descriptors one.
Having said that, there is a point in the series where
VIRTIO_F_ACCESS_PLATFORMis actually mandatory, so I think we could use the latter addition of x-svq cmdline parameter and delay the feature validations where it makes more sense.Quoted 10 lines
Quoted 8 lines
hw/virtio/vhost-shadow-virtqueue.c +7
-0+if (*dev_features &BIT_ULL (b)) { +clear_bit (b, dev_features); + r = false; + } +break ; + +case VIRTIO_F_VERSION_1 :
Quoted 7 lines
Quoted 2 lines
I had the same question here.
For
VERSION_1it's easier to assume that guest is little endian at some points, but we could try harder to support both endianness if needed.
Re-thinking the SVQ feature isolation stuff for this first iteration based on your comments.
Maybe it's easier to simply fail if the device does not *match* the expected feature set, and add all of the "feature isolation" later. While a lot of guest <-> SVQ communication details are already solved for free with qemu's VirtQueue (indirect, packed, ...), we may simplify this series in particular and add the support for it later.
For example, at this moment would be valid for the device to export indirect descriptors feature flag, and SVQ simply forward that feature flag offering to the guest. So the guest <-> SVQ communication could have indirect descriptors (qemu's VirtQueue code handles it for free), but SVQ would not acknowledge it for the device. As a side note, to negotiate it would have been harmless actually, but it's not the case of packed vq.
So maybe for the v2 we can simply force the device to just export the strictly needed features and nothing else with qemu cmdline, and then enable the feature negotiation isolation for each side of SVQ?
Thanks!
Quoted 62 lines
Thanks!
Quoted 60 lines
Thanks
Quoted 56 lines
hw/virtio/vhost-shadow-virtqueue.c +17
-0+/* SVQ trust that guest vring is little endian */ +if (!(*dev_features &BIT_ULL (b))) { +set_bit (b, dev_features); + r = false; + } +continue ; + +default : +if (*dev_features &BIT_ULL (b)) { +clear_bit (b, dev_features); + } + } + } + +return r; +} +/* Forward guest notifications */ static void vhost_handle_guest_kick (EventNotifier *n) {hw/virtio/vhost-vdpa.c +21
-0@@ -855,10 +855,31 @@ static int vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v,size_t n_svqs = v->shadow_vqs_enabled ? hdev->nvqs :0 ;g_autoptr (GPtrArray) shadow_vqs =g_ptr_array_new_full (n_svqs, vhost_psvq_free); +uint64_t dev_features; +uint64_t svq_features; +int r; +bool ok; +if (!v->shadow_vqs_enabled) { goto out; } + r =vhost_vdpa_get_features (hdev, &dev_features); +if (r !=0 ) { +error_setg (errp,"Can't get vdpa device features, got (%d)" , r); +return r; + } + + svq_features = dev_features; + ok =vhost_svq_valid_device_features (&svq_features); +if (unlikely (!ok)) { +error_setg (errp, +"SVQ Invalid device feature flags, offer: 0x%" PRIx64", ok: 0x%" PRIx64, + hdev->features, svq_features); +return -1 ; + } + + shadow_vqs =g_ptr_array_new_full (hdev->nvqs, vhost_psvq_free);for (unsigned n =0 ; n < hdev->nvqs; ++n) {VhostShadowVirtqueue *svq =vhost_svq_new ();
Eugenio Perez Martin
maintainerRe: [PATCH 28/31] vdpa: Expose VHOST_F_LOG_ALL on SVQ
In reply to Jason WangQuoted 39 lines · Jason Wang
Quoted 36 lines · Eugenio Pérez
SVQ is able to log the dirty bits by itself, so let's use it to not block migration.
Also, ignore set and clear of
VHOST_F_LOG_ALLonset_featuresif SVQ is enabled. Even if the device supports it, the reports would be nonsense because SVQ memory is in the qemu region.The log region is still allocated. Future changes might skip that, but this series is already long enough.
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-vdpa.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)hw/virtio/vhost-vdpa.c +4
-0@@ -1022,6 +1022,9 @@ static int vhost_vdpa_get_features(struct vhost_dev *dev, uint64_t *features)if (ret ==0 && v->shadow_vqs_enabled) {/* Filter only features that SVQ can offer to guest */ vhost_svq_valid_guest_features (features); + +/* Add SVQ logging capabilities */ + *features |=BIT_ULL (VHOST_F_LOG_ALL ); }return ret;@@ -1039,8 +1042,25 @@ static int vhost_vdpa_set_features(struct vhost_dev *dev,if (v->shadow_vqs_enabled) {uint64_t dev_features, svq_features, acked_features; +uint8_t status =0 ;bool ok;
Quoted 15 lines
hw/virtio/vhost-vdpa.c static int vhost_vdpa_set_features(struct vhost_dev *dev, · line 1057
Quoted 10 lines
hw/virtio/vhost-vdpa.c +10
-0+ ret =vhost_vdpa_call (dev,VHOST_VDPA_GET_STATUS , &status); +if (unlikely (ret)) { +return ret; + } + +if (status &VIRTIO_CONFIG_S_DRIVER_OK ) { +/* +* vhost is trying to enable or disable _F_LOG, and the device +* would report wrong dirty pages. SVQ handles it. +*/ I fail to understand this comment, I'd think there's no way to disable dirty page tracking for SVQ.
vhost_log_global_{start,stop} are called at the beginning and end of migration. To inform the device that it should start logging, they set or clean VHOST_F_LOG_ALL at vhost_dev_set_log.
While SVQ does not use VHOST_F_LOG_ALL, it exports the feature bit so vhost does not block migration. Maybe we need to look for another way to do this?
Thanks!
Quoted 13 lines
Thanks
Quoted 9 lines
hw/virtio/vhost-vdpa.c +6
-0+return 0 ; + } + +/* We must not ack _F_LOG if SVQ is enabled */ + features &= ~BIT_ULL (VHOST_F_LOG_ALL ); + ret =vhost_vdpa_get_dev_features (dev, &dev_features);if (ret !=0 ) {error_report ("Can't get vdpa device features, got (%d)" , ret);
Eugenio Perez Martin
maintainerRe: [PATCH 18/31] vhost: Shadow virtqueue buffers forwarding
In reply to Jason WangQuoted 19 lines · Jason Wang
hw/virtio/vhost-shadow-virtqueue.c void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd) · line 600
Quoted 12 lines · Eugenio Pérez
hw/virtio/vhost-shadow-virtqueue.c +8
-0@@ -272,6 +590,28 @@ void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd)void vhost_svq_stop (VhostShadowVirtqueue *svq) {event_notifier_set_handler (&svq->svq_kick,NULL ); + g_autofree VirtQueueElement *next_avail_elem = NULL; + +if (!svq->vq) { +return ; + } + +/* Send all pending used descriptors to guest */ +vhost_svq_flush (svq, false);Do we need to wait for all the pending descriptors to be completed here?
No, this function does not wait, it only completes the forwarding of the *used* descriptors.
The best example is the net rx queue in my opinion. This call will check SVQ's vring used_idx and will forward the last used descriptors if any, but all available descriptors will remain as available for qemu's VQ code.
To skip it would miss those last rx descriptors in migration.
Thanks!
Quoted 19 lines
Thanks
Quoted 15 lines
hw/virtio/vhost-shadow-virtqueue.c +14
-0+ +for (unsigned i =0 ; i < svq->vring.num; ++i) { + g_autofree VirtQueueElement *elem = NULL; + elem =g_steal_pointer (&svq->ring_id_maps[i]); +if (elem) { +virtqueue_detach_element (svq->vq, elem, elem->len); + } + } + + next_avail_elem =g_steal_pointer (&svq->next_guest_avail_elem); +if (next_avail_elem) { +virtqueue_detach_element (svq->vq, next_avail_elem, + next_avail_elem->len); + } }
Eugenio Perez Martin
maintainerRe: [PATCH 18/31] vhost: Shadow virtqueue buffers forwarding
In reply to Jason WangQuoted 165 lines · Jason Wang
Quoted 162 lines · Eugenio Pérez
Initial version of shadow virtqueue that actually forward buffers. There is no iommu support at the moment, and that will be addressed in future patches of this series. Since all vhost-vdpa devices use forced IOMMU, this means that SVQ is not usable at this point of the series on any device.
For simplicity it only supports modern devices, that expects vring in little endian, with split ring and no event idx or indirect descriptors. Support for them will not be added in this series.
It reuses the VirtQueue code for the device part. The driver part is based on Linux's
virtio_ringdriver, but with stripped functionality and optimizations so it's easier to review.However, forwarding buffers have some particular pieces: One of the most unexpected ones is that a guest's buffer can expand through more than one descriptor in SVQ. While this is handled gracefully by qemu's emulated virtio devices, it may cause unexpected SVQ queue full. This patch also solves it by checking for this condition at both guest's kicks and device's calls. The code may be more elegant in the future if SVQ code runs in its own iocontext.
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-shadow-virtqueue.h | 2 + hw/virtio/vhost-shadow-virtqueue.c | 365 ++++++++++++++++++++++++++++- hw/virtio/vhost-vdpa.c | 111 ++++++++- 3 files changed, 462 insertions(+), 16 deletions(-)hw/virtio/vhost-shadow-virtqueue.h +2
-0@@ -33,6 +33,8 @@ uint16_t vhost_svq_get_num(const VhostShadowVirtqueue *svq);size_t vhost_svq_driver_area_size (const VhostShadowVirtqueue *svq);size_t vhost_svq_device_area_size (const VhostShadowVirtqueue *svq); +void vhost_svq_start (VhostShadowVirtqueue *svq,VirtIODevice *vdev, +VirtQueue *vq);void vhost_svq_stop (VhostShadowVirtqueue *svq);VhostShadowVirtqueue *vhost_svq_new (uint16_t qsize);hw/virtio/vhost-shadow-virtqueue.c +91
-5@@ -9,6 +9,8 @@#include "qemu/osdep.h" #include "hw/virtio/vhost-shadow-virtqueue.h" +#include "hw/virtio/vhost.h" +#include "hw/virtio/virtio-access.h" #include "standard-headers/linux/vhost_types.h" #include "qemu/error-report.h" @@ -36,6 +38,33 @@ typedef struct VhostShadowVirtqueue {/* Guest's call notifier, where SVQ calls guest. */ EventNotifier svq_call; + +/* Virtio queue shadowing */ +VirtQueue *vq; + +/* Virtio device */ +VirtIODevice *vdev; + +/* Map for returning guest's descriptors */ +VirtQueueElement **ring_id_maps; + +/* Next VirtQueue element that guest made available */ +VirtQueueElement *next_guest_avail_elem; + +/* Next head to expose to device */ +uint16_t avail_idx_shadow; + +/* Next free descriptor */ +uint16_t free_head; + +/* Last seen used idx */ +uint16_t shadow_used_idx; + +/* Next head to consume from device */ +uint16_t last_used_idx; + +/* Cache for the exposed notification flag */ +bool notification; } VhostShadowVirtqueue;#define INVALID_SVQ_KICK_FD -1@@ -148,30 +177,294 @@ bool vhost_svq_ack_guest_features(uint64_t dev_features,return true; } -/* Forward guest notifications */ -static void vhost_handle_guest_kick (EventNotifier *n) +/** +* Number of descriptors that SVQ can make available from the guest. +* +* @svq The svq +*/ +static uint16_t vhost_svq_available_slots (const VhostShadowVirtqueue *svq) { -VhostShadowVirtqueue *svq =container_of (n, VhostShadowVirtqueue, - svq_kick); +return svq->vring.num - (svq->avail_idx_shadow - svq->shadow_used_idx); +} + +static void vhost_svq_set_notification (VhostShadowVirtqueue *svq,bool enable) +{ +uint16_t notification_flag; -if (unlikely (!event_notifier_test_and_clear (n))) { +if (svq->notification == enable) { +return ; + } + + notification_flag =cpu_to_le16 (VRING_AVAIL_F_NO_INTERRUPT ); + + svq->notification = enable; +if (enable) { + svq->vring.avail->flags &= ~notification_flag; + }else { + svq->vring.avail->flags |= notification_flag; + } +} + +static void vhost_vring_write_descs (VhostShadowVirtqueue *svq, +const struct iovec *iovec, +size_t num,bool more_descs,bool write) +{ +uint16_t i = svq->free_head, last = svq->free_head; +unsigned n; +uint16_t flags = write ?cpu_to_le16 (VRING_DESC_F_WRITE ) :0 ; +vring_desc_t *descs = svq->vring.desc; + +if (num ==0 ) { +return ; + } + +for (n =0 ; n < num; n++) { +if (more_descs || (n +1 < num)) { + descs[i].flags = flags |cpu_to_le16 (VRING_DESC_F_NEXT ); + }else { + descs[i].flags = flags; + } + descs[i].addr =cpu_to_le64 ((hwaddr )iovec[n].iov_base); + descs[i].len =cpu_to_le32 (iovec[n].iov_len); + + last = i; + i =cpu_to_le16 (descs[i].next); + } + + svq->free_head =le16_to_cpu (descs[last].next); +} + +static unsigned vhost_svq_add_split (VhostShadowVirtqueue *svq, +VirtQueueElement *elem) +{ +int head; +unsigned avail_idx; +vring_avail_t *avail = svq->vring.avail; +
Quoted 9 lines
hw/virtio/vhost-shadow-virtqueue.c bool vhost_svq_ack_guest_features(uint64_t dev_features, · line 247
Quoted 4 lines
hw/virtio/vhost-shadow-virtqueue.c +4
-0+ head = svq->free_head; + +/* We need some descriptors here */ +assert (elem->out_num || elem->in_num);Looks like this could be triggered by guest, we need fail instead assert here.
My understanding was that virtqueue_pop already sanitized that case, but I'm not able to find where now. I will recheck and, in case it's not, I will move to a failure.
Quoted 58 lines
Quoted 58 lines
hw/virtio/vhost-shadow-virtqueue.c +50
-1+ +vhost_vring_write_descs (svq, elem->out_sg, elem->out_num, + elem->in_num >0 , false); +vhost_vring_write_descs (svq, elem->in_sg, elem->in_num, false, true); + +/* +* Put entry in available array (but don't update avail->idx until they +* do sync). +*/ + avail_idx = svq->avail_idx_shadow & (svq->vring.num -1 ); + avail->ring[avail_idx] =cpu_to_le16 (head); + svq->avail_idx_shadow++; + +/* Update avail index after the descriptor is wrote */ +smp_wmb (); + avail->idx =cpu_to_le16 (svq->avail_idx_shadow); + +return head; +} + +static void vhost_svq_add (VhostShadowVirtqueue *svq,VirtQueueElement *elem) +{ +unsigned qemu_head =vhost_svq_add_split (svq, elem); + + svq->ring_id_maps[qemu_head] = elem; +} + +static void vhost_svq_kick (VhostShadowVirtqueue *svq) +{ +/* We need to expose available array entries before checking used flags */ +smp_mb (); +if (svq->vring.used->flags &VRING_USED_F_NO_NOTIFY ) {return ; }event_notifier_set (&svq->hdev_kick); } -/* Forward vhost notifications */ +/** +* Forward available buffers. +* +* @svq Shadow VirtQueue +* +* Note that this function does not guarantee that all guest's available +* buffers are available to the device in SVQ avail ring. The guest may have +* exposed a GPA / GIOVA congiuous buffer, but it may not be contiguous in qemu +* vaddr. +* +* If that happens, guest's kick notifications will be disabled until device +* makes some buffers used. +*/ +static void vhost_handle_guest_kick (VhostShadowVirtqueue *svq) +{ +/* Clear event notifier */ +event_notifier_test_and_clear (&svq->svq_kick); +
Quoted 9 lines
hw/virtio/vhost-shadow-virtqueue.c bool vhost_svq_ack_guest_features(uint64_t dev_features, · line 307
Quoted 4 lines
hw/virtio/vhost-shadow-virtqueue.c +4
-0+/* Make available as many buffers as possible */ + do { + if (virtio_queue_get_notification(svq->vq)) { + virtio_queue_set_notification(svq->vq, false);This looks like an optimization the should belong to
virtio_queue_set_notification()itself.
Sure we can move.
Quoted 119 lines
Quoted 119 lines
hw/virtio/vhost-shadow-virtqueue.c +118
-0+ } + +while (true) { +VirtQueueElement *elem; + +if (svq->next_guest_avail_elem) { + elem =g_steal_pointer (&svq->next_guest_avail_elem); + }else { + elem =virtqueue_pop (svq->vq,sizeof (*elem)); + } + +if (!elem) { +break ; + } + +if (elem->out_num + elem->in_num > +vhost_svq_available_slots (svq)) { +/* +* This condition is possible since a contiguous buffer in GPA +* does not imply a contiguous buffer in qemu's VA +* scatter-gather segments. If that happen, the buffer exposed +* to the device needs to be a chain of descriptors at this +* moment. +* +* SVQ cannot hold more available buffers if we are here: +* queue the current guest descriptor and ignore further kicks +* until some elements are used. +*/ + svq->next_guest_avail_elem = elem; +return ; + } + +vhost_svq_add (svq, elem); +vhost_svq_kick (svq); + } + +virtio_queue_set_notification (svq->vq, true); + }while (!virtio_queue_empty (svq->vq)); +} + +/** +* Handle guest's kick. +* +* @n guest kick event notifier, the one that guest set to notify svq. +*/ +static void vhost_handle_guest_kick_notifier (EventNotifier *n) +{ +VhostShadowVirtqueue *svq =container_of (n, VhostShadowVirtqueue, + svq_kick); +vhost_handle_guest_kick (svq); +} + +static bool vhost_svq_more_used (VhostShadowVirtqueue *svq) +{ +if (svq->last_used_idx != svq->shadow_used_idx) { +return true; + } + + svq->shadow_used_idx =cpu_to_le16 (svq->vring.used->idx); + +return svq->last_used_idx != svq->shadow_used_idx; +} + +static VirtQueueElement *vhost_svq_get_buf (VhostShadowVirtqueue *svq) +{ +vring_desc_t *descs = svq->vring.desc; +const vring_used_t *used = svq->vring.used; +vring_used_elem_t used_elem; +uint16_t last_used; + +if (!vhost_svq_more_used (svq)) { +return NULL ; + } + +/* Only get used array entries after they have been exposed by dev */ +smp_rmb (); + last_used = svq->last_used_idx & (svq->vring.num -1 ); + used_elem.id =le32_to_cpu (used->ring[last_used].id); + used_elem.len =le32_to_cpu (used->ring[last_used].len); + + svq->last_used_idx++; +if (unlikely (used_elem.id >= svq->vring.num)) { +error_report ("Device %s says index %u is used" , svq->vdev->name, + used_elem.id); +return NULL ; + } + +if (unlikely (!svq->ring_id_maps[used_elem.id])) { +error_report ( +"Device %s says index %u is used, but it was not available" , + svq->vdev->name, used_elem.id); +return NULL ; + } + + descs[used_elem.id].next = svq->free_head; + svq->free_head = used_elem.id; + + svq->ring_id_maps[used_elem.id]->len = used_elem.len; +return g_steal_pointer (&svq->ring_id_maps[used_elem.id]); +} + +static void vhost_svq_flush (VhostShadowVirtqueue *svq, +bool check_for_avail_queue) +{ +VirtQueue *vq = svq->vq; + +/* Make as many buffers as possible used. */ + do { + unsigned i = 0; + + vhost_svq_set_notification(svq, false); + while (true) { + g_autofree VirtQueueElement *elem = vhost_svq_get_buf(svq); + if (!elem) { + break; + } + + if (unlikely(i >= svq->vring.num)) {
Quoted 9 lines
hw/virtio/vhost-shadow-virtqueue.c bool vhost_svq_ack_guest_features(uint64_t dev_features, · line 430
Quoted 5 lines
hw/virtio/vhost-shadow-virtqueue.c +5
-0+virtio_error (svq->vdev, +"More than %u used buffers obtained in a %u size SVQ" , + i, svq->vring.num); +virtqueue_fill (vq, elem, elem->len, i); +virtqueue_flush (vq, i);Let's simply use
virtqueue_push()here?
virtqueue_push support to fill and flush only one element, instead of batch. I'm fine with either but I think the less updates to the used idx, the better.
Quoted 6 lines
hw/virtio/vhost-shadow-virtqueue.c bool vhost_svq_ack_guest_features(uint64_t dev_features, · line 431
Quoted 2 lines
hw/virtio/vhost-shadow-virtqueue.c +1
-0+ i =0 ;Do we need to bail out here?
Yes I guess we can simply return.
Quoted 18 lines
Quoted 4 lines
hw/virtio/vhost-shadow-virtqueue.c +3
-0+ } +virtqueue_fill (vq, elem, elem->len, i++); + }hw/virtio/vhost-shadow-virtqueue.c bool vhost_svq_ack_guest_features(uint64_t dev_features, · line 444
Quoted 10 lines
hw/virtio/vhost-shadow-virtqueue.c +10
-0+ +virtqueue_flush (vq, i); +event_notifier_set (&svq->svq_call); + +if (check_for_avail_queue && svq->next_guest_avail_elem) { +/* +* Avail ring was full when vhost_svq_flush was called, so it's a +* good moment to make more descriptors available if possible +*/ +vhost_handle_guest_kick (svq);Is there better to have a similar check as
vhost_handle_guest_kick()did?
Quoted 3 lines
if (elem->out_num + elem->in_num >
vhost_svq_available_slots(svq)) {It will be duplicated when we call vhost_handle_guest_kick, won't it?
Quoted 9 lines
hw/virtio/vhost-shadow-virtqueue.c bool vhost_svq_ack_guest_features(uint64_t dev_features, · line 447
Quoted 4 lines
hw/virtio/vhost-shadow-virtqueue.c +3
-0+ } + +vhost_svq_set_notification (svq, true);A
mb()is needed here? Otherwise we may lost a call here (wherevhost_svq_more_used()is run beforevhost_svq_set_notification()).
I'm confused here then, I thought you said this is just a hint so there was no need? [1]. I think the memory barrier is needed too.
Quoted 17 lines
Quoted 17 lines
hw/virtio/vhost-shadow-virtqueue.c +11
-0+ }while (vhost_svq_more_used (svq)); +} + +/** +* Forward used buffers. +* +* @n hdev call event notifier, the one that device set to notify svq. +* +* Note that we are not making any buffers available in the loop, there is no +* way that it runs more than virtqueue size times. +*/ static void vhost_svq_handle_call (EventNotifier *n) {VhostShadowVirtqueue *svq =container_of (n, VhostShadowVirtqueue, hdev_call);
Quoted 9 lines
hw/virtio/vhost-shadow-virtqueue.c bool vhost_svq_ack_guest_features(uint64_t dev_features, · line 465
Quoted 5 lines
hw/virtio/vhost-shadow-virtqueue.c +2
-3-if (unlikely (!event_notifier_test_and_clear (n))) { -return ; - } +/* Clear event notifier */ +event_notifier_test_and_clear (n);Any reason that we remove the above check?
This comes from the previous versions, where this made sure we missed no used buffers in the process of switching to SVQ mode.
If we enable SVQ from the beginning I think we can rely on getting all the device's used buffer notifications, so let me think a little bit and I can move to check the eventfd.
Quoted 178 lines
Quoted 178 lines
hw/virtio/vhost-shadow-virtqueue.c +51
-3-event_notifier_set (&svq->svq_call); +vhost_svq_flush (svq, true); }/** @@ -258,13 +551,38 @@ void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd)* need to explicitely check for them. */ event_notifier_init_fd (&svq->svq_kick, svq_kick_fd); -event_notifier_set_handler (&svq->svq_kick, vhost_handle_guest_kick); +event_notifier_set_handler (&svq->svq_kick, + vhost_handle_guest_kick_notifier);if (!check_old ||event_notifier_test_and_clear (&tmp)) {event_notifier_set (&svq->hdev_kick); } } +/** +* Start shadow virtqueue operation. +* +* @svq Shadow Virtqueue +* @vdev VirtIO device +* @vq Virtqueue to shadow +*/ +void vhost_svq_start (VhostShadowVirtqueue *svq,VirtIODevice *vdev, +VirtQueue *vq) +{ + svq->next_guest_avail_elem =NULL ; + svq->avail_idx_shadow =0 ; + svq->shadow_used_idx =0 ; + svq->last_used_idx =0 ; + svq->vdev = vdev; + svq->vq = vq; + +memset (svq->vring.avail,0 ,sizeof (*svq->vring.avail)); +memset (svq->vring.used,0 ,sizeof (*svq->vring.avail)); +for (unsigned i =0 ; i < svq->vring.num -1 ; i++) { + svq->vring.desc[i].next =cpu_to_le16 (i +1 ); + } +} +/** * Stop shadow virtqueue operation. * @svq Shadow Virtqueue @@ -272,6 +590,28 @@ void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd)void vhost_svq_stop (VhostShadowVirtqueue *svq) {event_notifier_set_handler (&svq->svq_kick,NULL ); + g_autofree VirtQueueElement *next_avail_elem = NULL; + +if (!svq->vq) { +return ; + } + +/* Send all pending used descriptors to guest */ +vhost_svq_flush (svq, false); + +for (unsigned i =0 ; i < svq->vring.num; ++i) { + g_autofree VirtQueueElement *elem = NULL; + elem =g_steal_pointer (&svq->ring_id_maps[i]); +if (elem) { +virtqueue_detach_element (svq->vq, elem, elem->len); + } + } + + next_avail_elem =g_steal_pointer (&svq->next_guest_avail_elem); +if (next_avail_elem) { +virtqueue_detach_element (svq->vq, next_avail_elem, + next_avail_elem->len); + } }/** @@ -316,7 +656,7 @@ VhostShadowVirtqueue *vhost_svq_new(uint16_t qsize)memset (svq->vring.desc,0 , driver_size); svq->vring.used =qemu_memalign (qemu_real_host_page_size, device_size);memset (svq->vring.used,0 , device_size); - + svq->ring_id_maps = g_new0(VirtQueueElement *, qsize);event_notifier_set_handler (&svq->hdev_call, vhost_svq_handle_call);return g_steal_pointer (&svq);@@ -335,6 +675,7 @@ void vhost_svq_free(VhostShadowVirtqueue *vq)event_notifier_cleanup (&vq->hdev_kick);event_notifier_set_handler (&vq->hdev_call,NULL );event_notifier_cleanup (&vq->hdev_call); +g_free (vq->ring_id_maps);qemu_vfree (vq->vring.desc);qemu_vfree (vq->vring.used);g_free (vq);hw/virtio/vhost-vdpa.c +59
-4@@ -752,9 +752,9 @@ static int vhost_vdpa_set_vring_call(struct vhost_dev *dev,* Note that this function does not rewind kick file descriptor if cannot set * call one. */ -static bool vhost_vdpa_svq_setup (struct vhost_dev *dev, -VhostShadowVirtqueue *svq, -unsigned idx) +static int vhost_vdpa_svq_set_fds (struct vhost_dev *dev, +VhostShadowVirtqueue *svq, +unsigned idx) {struct vhost_vring_file file = { .index = dev->vq_index + idx,@@ -767,7 +767,7 @@ static bool vhost_vdpa_svq_setup(struct vhost_dev *dev, r =vhost_vdpa_set_vring_dev_kick (dev, &file);if (unlikely (r !=0 )) {error_report ("Can't set device kick fd (%d)" , -r); -return false; +return r; } event_notifier =vhost_svq_get_svq_call_notifier (svq);@@ -777,6 +777,99 @@ static bool vhost_vdpa_svq_setup(struct vhost_dev *dev,error_report ("Can't set device call fd (%d)" , -r); } +return r; +} + +/** +* Unmap SVQ area in the device +*/ +static bool vhost_vdpa_svq_unmap_ring (struct vhost_vdpa *v,hwaddr iova, +hwaddr size) +{ +int r; + + size =ROUND_UP (size, qemu_real_host_page_size); + r =vhost_vdpa_dma_unmap (v, iova, size); +return r ==0 ; +} + +static bool vhost_vdpa_svq_unmap_rings (struct vhost_dev *dev, +const VhostShadowVirtqueue *svq) +{ +struct vhost_vdpa *v = dev->opaque; +struct vhost_vring_addr svq_addr; +size_t device_size =vhost_svq_device_area_size (svq); +size_t driver_size =vhost_svq_driver_area_size (svq); +bool ok; + +vhost_svq_get_vring_addr (svq, &svq_addr); + + ok =vhost_vdpa_svq_unmap_ring (v, svq_addr.desc_user_addr, driver_size); +if (unlikely (!ok)) { +return false; + } + +return vhost_vdpa_svq_unmap_ring (v, svq_addr.used_user_addr, device_size); +} + +/** +* Map shadow virtqueue rings in device +* +* @dev The vhost device +* @svq The shadow virtqueue +*/ +static bool vhost_vdpa_svq_map_rings (struct vhost_dev *dev, +const VhostShadowVirtqueue *svq) +{ +struct vhost_vdpa *v = dev->opaque; +struct vhost_vring_addr svq_addr; +size_t device_size =vhost_svq_device_area_size (svq); +size_t driver_size =vhost_svq_driver_area_size (svq); +int r; + +vhost_svq_get_vring_addr (svq, &svq_addr); + + r =vhost_vdpa_dma_map (v, svq_addr.desc_user_addr, driver_size, + (void *)svq_addr.desc_user_addr, true); +if (unlikely (r !=0 )) {
Quoted 9 lines
hw/virtio/vhost-vdpa.c static bool vhost_vdpa_svq_setup(struct vhost_dev *dev, · line 839
Quoted 5 lines
hw/virtio/vhost-vdpa.c +5
-0+return false; + } + + r =vhost_vdpa_dma_map (v, svq_addr.used_user_addr, device_size, + (void *)svq_addr.used_user_addr, false);Do we need unmap the driver area if we fail here?
Yes, this used to trust in unmap them at the disabling of SVQ. Now I think we need to unmap as you say.
Thanks!
[1] https://lists.linuxfoundation.org/pipermail/virtualization/2021-March/053322.html
Quoted 65 lines
Thanks
Quoted 61 lines
hw/virtio/vhost-vdpa.c +43
-0+return r ==0 ; +} + +static bool vhost_vdpa_svq_setup (struct vhost_dev *dev, +VhostShadowVirtqueue *svq, +unsigned idx) +{ +uint16_t vq_index = dev->vq_index + idx; +struct vhost_vring_state s = { + .index = vq_index, + }; +int r; +bool ok; + + r =vhost_vdpa_set_dev_vring_base (dev, &s); +if (unlikely (r)) { +error_report ("Can't set vring base (%d)" , r); +return false; + } + + s.num =vhost_svq_get_num (svq); + r =vhost_vdpa_set_dev_vring_num (dev, &s); +if (unlikely (r)) { +error_report ("Can't set vring num (%d)" , r); +return false; + } + + ok =vhost_vdpa_svq_map_rings (dev, svq); +if (unlikely (!ok)) { +return false; + } + + r =vhost_vdpa_svq_set_fds (dev, svq, idx);return r ==0 ; }@@ -788,14 +881,24 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)if (started) {vhost_vdpa_host_notifiers_init (dev);for (unsigned i =0 ; i < v->shadow_vqs->len; ++i) { +VirtQueue *vq =virtio_get_queue (dev->vdev, dev->vq_index + i);VhostShadowVirtqueue *svq =g_ptr_array_index (v->shadow_vqs, i);bool ok =vhost_vdpa_svq_setup (dev, svq, i);if (unlikely (!ok)) {return -1 ; } +vhost_svq_start (svq, dev->vdev, vq); }vhost_vdpa_set_vring_ready (dev); }else { +for (unsigned i =0 ; i < v->shadow_vqs->len; ++i) { +VhostShadowVirtqueue *svq =g_ptr_array_index (v->shadow_vqs, + i); +bool ok =vhost_vdpa_svq_unmap_rings (dev, svq); +if (unlikely (!ok)) { +return -1 ; + } + }vhost_vdpa_host_notifiers_uninit (dev, dev->nvqs); }
Eugenio Perez Martin
maintainerRe: [PATCH 22/31] vhost: Add VhostIOVATree
In reply to Jason WangQuoted 13 lines · Jason Wang
Quoted 4 lines · Eugenio Pérez
This tree is able to look for a translated address from an IOVA address.
At first glance it is similar to util/iova-tree. However, SVQ working on devices with limited IOVA space need more capabilities,
So did the IOVA tree (e.g l2 vtd can only work in the range of GAW and without RMRRs).
Quoted 7 lines
Quoted 2 lines
like allocating
IOVA chunks or performing reverse translations (qemu addresses to iova).This looks like a general request as well. So I wonder if we can simply extend iova tree instead.
While both are true, I don't see code that performs allocations or qemu vaddr to iova translations. But if the changes can be integrated into iova-tree that would be great for sure.
The main drawback I see is the need to maintain two trees instead of one for users of iova-tree. While complexity does not grow, it needs to double the amount of work needed.
Thanks!
Quoted 232 lines
Thanks
Quoted 228 lines
The allocation capability, as "assign a free IOVA address to this chunk of memory in qemu's address space" allows shadow virtqueue to create a new address space that is not restricted by guest's addressable one, so we can allocate shadow vqs vrings outside of it.
It duplicates the tree so it can search efficiently both directions, and it will signal overlap if iova or the translated address is present in any tree.
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-iova-tree.h | 27 +++++++ hw/virtio/vhost-iova-tree.c | 157 ++++++++++++++++++++++++++++++++++++ hw/virtio/meson.build | 2 +- 3 files changed, 185 insertions(+), 1 deletion(-)create mode 100644
hw/virtio/vhost-iova-tree.h
create mode 100644hw/virtio/vhost-iova-tree.chw/virtio/vhost-iova-tree.h new file +27
-0@@ -0,0 +1,27 @@ +/* +* vhost software live migration ring +* +* SPDX-FileCopyrightText: Red Hat, Inc. 2021 +* SPDX-FileContributor: Author: Eugenio Pérez <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef HW_VIRTIO_VHOST_IOVA_TREE_H +#define HW_VIRTIO_VHOST_IOVA_TREE_H + +#include "qemu/iova-tree.h" +#include "exec/memory.h" + +typedef struct VhostIOVATree VhostIOVATree ; + +VhostIOVATree *vhost_iova_tree_new (uint64_t iova_first,uint64_t iova_last); +void vhost_iova_tree_delete (VhostIOVATree *iova_tree); +G_DEFINE_AUTOPTR_CLEANUP_FUNC (VhostIOVATree, vhost_iova_tree_delete); + +const DMAMap *vhost_iova_tree_find_iova (const VhostIOVATree *iova_tree, +const DMAMap *map); +int vhost_iova_tree_map_alloc (VhostIOVATree *iova_tree,DMAMap *map); +void vhost_iova_tree_remove (VhostIOVATree *iova_tree,const DMAMap *map); + +#endif hw/virtio/vhost-iova-tree.c new file +157
-0@@ -0,0 +1,157 @@ +/* +* vhost software live migration ring +* +* SPDX-FileCopyrightText: Red Hat, Inc. 2021 +* SPDX-FileContributor: Author: Eugenio Pérez <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "qemu/osdep.h" +#include "qemu/iova-tree.h" +#include "vhost-iova-tree.h" + +#define iova_min_addr qemu_real_host_page_size + +/** +* VhostIOVATree, able to: +* - Translate iova address +* - Reverse translate iova address (from translated to iova) +* - Allocate IOVA regions for translated range (potentially slow operation) +* +* Note that it cannot remove nodes. +*/ +struct VhostIOVATree { +/* First addresable iova address in the device */ +uint64_t iova_first; + +/* Last addressable iova address in the device */ +uint64_t iova_last; + +/* IOVA address to qemu memory maps. */ +IOVATree *iova_taddr_map; + +/* QEMU virtual memory address to iova maps */ +GTree *taddr_iova_map; +}; + +static gint vhost_iova_tree_cmp_taddr (gconstpointer a,gconstpointer b, +gpointer data) +{ +const DMAMap *m1 = a, *m2 = b; + +if (m1->translated_addr > m2->translated_addr + m2->size) { +return 1 ; + } + +if (m1->translated_addr + m1->size < m2->translated_addr) { +return -1 ; + } + +/* Overlapped */ +return 0 ; +} + +/** +* Create a new IOVA tree +* +* Returns the new IOVA tree +*/ +VhostIOVATree *vhost_iova_tree_new (hwaddr iova_first,hwaddr iova_last) +{ +VhostIOVATree *tree =g_new (VhostIOVATree,1 ); + +/* Some devices does not like 0 addresses */ + tree->iova_first =MAX (iova_first, iova_min_addr); + tree->iova_last = iova_last; + + tree->iova_taddr_map =iova_tree_new (); + tree->taddr_iova_map =g_tree_new_full (vhost_iova_tree_cmp_taddr,NULL , +NULL , g_free); +return tree; +} + +/** +* Delete an iova tree +*/ +void vhost_iova_tree_delete (VhostIOVATree *iova_tree) +{ +iova_tree_destroy (iova_tree->iova_taddr_map); +g_tree_unref (iova_tree->taddr_iova_map); +g_free (iova_tree); +} + +/** +* Find the IOVA address stored from a memory address +* +* @tree The iova tree +* @map The map with the memory address +* +* Return the stored mapping, or NULL if not found. +*/ +const DMAMap *vhost_iova_tree_find_iova (const VhostIOVATree *tree, +const DMAMap *map) +{ +return g_tree_lookup (tree->taddr_iova_map, map); +} + +/** +* Allocate a new mapping +* +* @tree The iova tree +* @map The iova map +* +* Returns: +* - IOVA_OK if the map fits in the container +* - IOVA_ERR_INVALID if the map does not make sense (like size overflow) +* - IOVA_ERR_OVERLAP if the tree already contains that map +* - IOVA_ERR_NOMEM if tree cannot allocate more space. +* +* It returns assignated iova in map->iova if return value is VHOST_DMA_MAP_OK. +*/ +int vhost_iova_tree_map_alloc (VhostIOVATree *tree,DMAMap *map) +{ +/* Some vhost devices does not like addr 0. Skip first page */ +hwaddr iova_first = tree->iova_first ?: qemu_real_host_page_size; +DMAMap *new; +int r; + +if (map->translated_addr + map->size < map->translated_addr || + map->perm ==IOMMU_NONE ) { +return IOVA_ERR_INVALID ; + } + +/* Check for collisions in translated addresses */ +if (vhost_iova_tree_find_iova (tree, map)) { +return IOVA_ERR_OVERLAP ; + } + +/* Allocate a node in IOVA address */ + r =iova_tree_alloc (tree->iova_taddr_map, map, iova_first, + tree->iova_last); +if (r !=IOVA_OK ) { +return r; + } + +/* Allocate node in qemu -> iova translations */ + new =g_malloc (sizeof (*new)); +memcpy (new, map,sizeof (*new)); +g_tree_insert (tree->taddr_iova_map, new, new); +return IOVA_OK ; +} + +/** +* Remove existing mappings from iova tree +* +* @param iova_tree The vhost iova tree +* @param map The map to remove +*/ +void vhost_iova_tree_remove (VhostIOVATree *iova_tree,const DMAMap *map) +{ +const DMAMap *overlap; + +iova_tree_remove (iova_tree->iova_taddr_map, map); +while ((overlap =vhost_iova_tree_find_iova (iova_tree, map))) { +g_tree_remove (iova_tree->taddr_iova_map, overlap); + } +}hw/virtio/meson.build +1
-1@@ -11,7 +11,7 @@ softmmu_ss.add(when: 'CONFIG_ALL', if_true: files('vhost-stub.c')) virtio_ss = ss.source_set() virtio_ss.add(files('virtio.c')) -virtio_ss.add(when: 'CONFIG_VHOST', if_true: files('vhost.c', 'vhost-backend.c', 'vhost-shadow-virtqueue.c')) +virtio_ss.add(when: 'CONFIG_VHOST', if_true: files('vhost.c', 'vhost-backend.c', 'vhost-shadow-virtqueue.c', 'vhost-iova-tree.c')) virtio_ss.add(when: 'CONFIG_VHOST_USER', if_true: files('vhost-user.c')) virtio_ss.add(when: 'CONFIG_VHOST_VDPA', if_true: files('vhost-vdpa.c')) virtio_ss.add(when: 'CONFIG_VIRTIO_BALLOON', if_true: files('virtio-balloon.c'))
Eugenio Perez Martin
maintainerRe: [PATCH 29/31] vdpa: Make ncs autofree
In reply to Jason WangQuoted 11 lines · Jason Wang
Quoted 3 lines · Eugenio Pérez
Simplifying memory management.
Signed-off-by Eugenio Pérez <[email protected]>To reduce the size of this series. This can be sent as an separate patch if I was not wrong.
Sure, I'll send separately to trivial maillist.
Thanks!
Quoted 38 lines
Thanks
Quoted 34 lines
--- net/vhost-vdpa.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)net/vhost-vdpa.c +2
-3@@ -264,7 +264,8 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name, {const NetdevVhostVDPAOptions *opts;int vdpa_device_fd; -NetClientState **ncs, *nc; + g_autofree NetClientState **ncs = NULL; +NetClientState *nc;int queue_pairs, i, has_cvq =0 ;assert (netdev->type ==NET_CLIENT_DRIVER_VHOST_VDPA );@@ -302,7 +303,6 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name, goto err; } -g_free (ncs);return 0 ; err:@@ -310,7 +310,6 @@ err:qemu_del_net_client (ncs[0 ]); }qemu_close (vdpa_device_fd); -g_free (ncs);return -1 ; }
Eugenio Perez Martin
maintainerRe: [PATCH 30/31] vdpa: Move vhost_vdpa_get_iova_range to net/vhost-vdpa.c
In reply to Jason WangQuoted 26 lines · Jason Wang
Quoted 8 lines · Eugenio Pérez
Since it's a device property, it can be done in
net/. This helps SVQ to allocate the rings in vdpa device initialization, rather than delay that.Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-vdpa.c | 15 --------------- net/vhost-vdpa.c | 32 ++++++++++++++++++++++++--------I don't understand here, since we will support device other than net?
Quoted 10 lines
2 files changed, 24 insertions(+), 23 deletions(-)hw/virtio/vhost-vdpa.c +0
-0@@ -350,19 +350,6 @@ static int vhost_vdpa_add_status(struct vhost_dev *dev, uint8_t status)return 0 ; }
Quoted 16 lines
hw/virtio/vhost-vdpa.c static int vhost_vdpa_add_status(struct vhost_dev *dev, uint8_t status) · line 364
Quoted 12 lines
hw/virtio/vhost-vdpa.c +0
-12-static void vhost_vdpa_get_iova_range (struct vhost_vdpa *v) -{ -int ret =vhost_vdpa_call (v->dev,VHOST_VDPA_GET_IOVA_RANGE , - &v->iova_range); -if (ret !=0 ) { - v->iova_range.first =0 ; - v->iova_range.last =UINT64_MAX ; - } - -trace_vhost_vdpa_get_iova_range (v->dev, v->iova_range.first, - v->iova_range.last); -}Let's just export this instead?
Yes, I see now that export is a better solution than moving.
Thanks!
Quoted 100 lines
Thanks
Quoted 96 lines
hw/virtio/vhost-vdpa.c +0
-3-static bool vhost_vdpa_one_time_request (struct vhost_dev *dev) {struct vhost_vdpa *v = dev->opaque;@@ -1295,8 +1282,6 @@ static int vhost_vdpa_init(struct vhost_dev *dev, void *opaque, Error **errp) goto err; } -vhost_vdpa_get_iova_range (v); -if (vhost_vdpa_one_time_request (dev)) {return 0 ; }net/vhost-vdpa.c +24
-8@@ -22,6 +22,7 @@#include <sys/ioctl.h> #include <err.h> #include "standard-headers/linux/virtio_net.h" +#include "standard-headers/linux/vhost_types.h" #include "monitor/monitor.h" #include "hw/virtio/vhost.h" @@ -187,13 +188,25 @@ static NetClientInfo net_vhost_vdpa_info = { .check_peer_type = vhost_vdpa_check_peer_type, }; +static void vhost_vdpa_get_iova_range (int fd, +struct vhost_vdpa_iova_range *iova_range) +{ +int ret =ioctl (fd,VHOST_VDPA_GET_IOVA_RANGE , iova_range); + +if (ret <0 ) { + iova_range->first =0 ; + iova_range->last =UINT64_MAX ; + } +} +static NetClientState *net_vhost_vdpa_init (NetClientState *peer, -const char *device, -const char *name, -int vdpa_device_fd, -int queue_pair_index, -int nvqs, -bool is_datapath) +const char *device, +const char *name, +int vdpa_device_fd, +int queue_pair_index, +int nvqs, +bool is_datapath, +struct vhost_vdpa_iova_range iova_range) {NetClientState *nc =NULL ;VhostVDPAState *s;@@ -211,6 +224,7 @@ static NetClientState *net_vhost_vdpa_init(NetClientState *peer, s->vhost_vdpa.device_fd = vdpa_device_fd; s->vhost_vdpa.index = queue_pair_index; + s->vhost_vdpa.iova_range = iova_range; ret =vhost_vdpa_add (nc, (void *)&s->vhost_vdpa, queue_pair_index, nvqs);if (ret) {qemu_del_net_client (nc);@@ -267,6 +281,7 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name, g_autofree NetClientState **ncs = NULL;NetClientState *nc;int queue_pairs, i, has_cvq =0 ; +struct vhost_vdpa_iova_range iova_range;assert (netdev->type ==NET_CLIENT_DRIVER_VHOST_VDPA ); opts = &netdev->u.vhost_vdpa;@@ -286,19 +301,20 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,qemu_close (vdpa_device_fd);return queue_pairs; } +vhost_vdpa_get_iova_range (vdpa_device_fd, &iova_range); ncs =g_malloc0 (sizeof (*ncs) * queue_pairs);for (i =0 ; i < queue_pairs; i++) { ncs[i] =net_vhost_vdpa_init (peer,TYPE_VHOST_VDPA , name, - vdpa_device_fd, i,2 , true); + vdpa_device_fd, i,2 , true, iova_range);if (!ncs[i]) goto err; }if (has_cvq) { nc =net_vhost_vdpa_init (peer,TYPE_VHOST_VDPA , name, - vdpa_device_fd, i,1 , false); + vdpa_device_fd, i,1 , false, iova_range);if (!nc) goto err; }
Re: [PATCH 09/31] vhost-vdpa: Take into account SVQ in vhost_vdpa_set_vring_call
In reply to Eugenio Perez MartinQuoted 49 lines · Eugenio Perez Martin
Quoted 37 lines · Jason Wang
Quoted 22 lines · Eugenio Pérez
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-vdpa.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)hw/virtio/vhost-vdpa.c +2
-2@@ -687,13 +687,29 @@ static int vhost_vdpa_set_vring_kick(struct vhost_dev *dev, } } -static int vhost_vdpa_set_vring_call (struct vhost_dev *dev, -struct vhost_vring_file *file) +static int vhost_vdpa_set_vring_dev_call (struct vhost_dev *dev, +struct vhost_vring_file *file) {trace_vhost_vdpa_set_vring_call (dev, file->index, file->fd);return vhost_vdpa_call (dev,VHOST_SET_VRING_CALL , file); }hw/virtio/vhost-vdpa.c static int vhost_vdpa_set_vring_kick(struct vhost_dev *dev, · line 706
Quoted 10 lines
hw/virtio/vhost-vdpa.c +10
-0+static int vhost_vdpa_set_vring_call (struct vhost_dev *dev, +struct vhost_vring_file *file) +{ +struct vhost_vdpa *v = dev->opaque; + +if (v->shadow_vqs_enabled) { +int vdpa_idx =vhost_vdpa_get_vq_index (dev, file->index); +VhostShadowVirtqueue *svq =g_ptr_array_index (v->shadow_vqs, vdpa_idx); + +vhost_svq_set_guest_call_notifier (svq, file->fd);Two questions here (had similar questions for vring kick):
Quoted 3 lines
1) Any reason that we setup the eventfd for vhost-vdpa in
vhost_vdpa_svq_setup()not here?I'm not sure what you mean.
The
guest->SVQcall and kick fds are set here and atvhost_vdpa_set_vring_kick. The event notifier handler of the guest -> SVQkick_fdis set atvhost_vdpa_set_vring_kick/vhost_svq_set_svq_kick_fd. The guest -> SVQ call fd has no event notifier handler since we don't poll it.
Quoted 5 lines
On the other hand, the connection SVQ <-> device uses the same fds from the beginning to the end, and they will not change with, for example, call fd masking. That's why it's setup from
vhost_vdpa_svq_setup. Delaying tovhost_vdpa_set_vring_callwould make us add way more logic there.
More logic in general shadow vq code but less codes for vhost-vdpa specific code I think.
E.g for we can move the kick set logic from vhost_vdpa_svq_set_fds() to here.
Thanks
Quoted 13 lines
Quoted 4 lines
2) The call could be disabled by using -1 as the fd, I don't see any code to deal with that.
Right, I didn't take that into account. vhost-kernel takes also -1 as
kick_fdto unbind, so SVQ can be reworked to take that into account for sure.Thanks!
Quoted 3 lines
Thanks
Quoted 9 lines
Quoted 9 lines
hw/virtio/vhost-vdpa.c static int vhost_vdpa_set_vring_kick(struct vhost_dev *dev, · line 715
Quoted 9 lines
hw/virtio/vhost-vdpa.c +6
-0+return 0 ; + }else { +return vhost_vdpa_set_vring_dev_call (dev, file); + } +} +/** * Set shadow virtqueue descriptors to the device *
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH 11/31] vhost: Add vhost_svq_valid_device_features to shadow vq
In reply to Eugenio Perez MartinQuoted 105 lines · Eugenio Perez Martin
Quoted 80 lines · Eugenio Perez Martin
Quoted 52 lines · Jason Wang
Quoted 50 lines · Eugenio Pérez
This allows SVQ to negotiate features with the device. For the device, SVQ is a driver. While this function needs to bypass all non-transport features, it needs to disable the features that SVQ does not support when forwarding buffers. This includes packed vq layout, indirect descriptors or event idx.
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-shadow-virtqueue.h | 2 ++ hw/virtio/vhost-shadow-virtqueue.c | 44 ++++++++++++++++++++++++++++++ hw/virtio/vhost-vdpa.c | 21 ++++++++++++++ 3 files changed, 67 insertions(+)hw/virtio/vhost-shadow-virtqueue.h +2
-0@@ -15,6 +15,8 @@typedef struct VhostShadowVirtqueue VhostShadowVirtqueue ; +bool vhost_svq_valid_device_features (uint64_t *features); +void vhost_svq_set_svq_kick_fd (VhostShadowVirtqueue *svq,int svq_kick_fd);void vhost_svq_set_guest_call_notifier (VhostShadowVirtqueue *svq,int call_fd); const EventNotifier *vhost_svq_get_dev_kick_notifier(hw/virtio/vhost-shadow-virtqueue.c +16
-0@@ -45,6 +45,50 @@ const EventNotifier *vhost_svq_get_dev_kick_notifier(return &svq->hdev_kick; } +/** +* Validate the transport device features that SVQ can use with the device +* +* @dev_features The device features. If success, the acknowledged features. +* +* Returns true if SVQ can go with a subset of these, false otherwise. +*/ +bool vhost_svq_valid_device_features (uint64_t *dev_features) +{ +bool r = true; + +for (uint64_t b =VIRTIO_TRANSPORT_F_START ; b <=VIRTIO_TRANSPORT_F_END ; + ++b) { +switch (b) { +case VIRTIO_F_NOTIFY_ON_EMPTY : +case VIRTIO_F_ANY_LAYOUT :Quoted 9 lines
hw/virtio/vhost-shadow-virtqueue.c const EventNotifier *vhost_svq_get_dev_kick_notifier( · line 67
Quoted 4 lines
hw/virtio/vhost-shadow-virtqueue.c +4
-0+continue ; + +case VIRTIO_F_ACCESS_PLATFORM : +/* SVQ does not know how to translate addresses */ I may miss something but any reason that we need to disable
ACCESS_PLATFORM? I'd expect the vring helper we used for shadow virtqueue can deal with vIOMMU perfectly.This function is validating SVQ <-> Device communications features, that may or may not be the same as guest <-> SVQ. These feature flags are valid for guest <-> SVQ communication, same as with indirect descriptors one.
Having said that, there is a point in the series where
VIRTIO_F_ACCESS_PLATFORMis actually mandatory, so I think we could use the latter addition of x-svq cmdline parameter and delay the feature validations where it makes more sense.Quoted 8 lines
Quoted 7 lines
hw/virtio/vhost-shadow-virtqueue.c +7
-0+if (*dev_features &BIT_ULL (b)) { +clear_bit (b, dev_features); + r = false; + } +break ; + +case VIRTIO_F_VERSION_1 :Quoted 6 lines
Quoted 2 lines
I had the same question here.
For
VERSION_1it's easier to assume that guest is little endian at some points, but we could try harder to support both endianness if needed.Re-thinking the SVQ feature isolation stuff for this first iteration based on your comments.
Maybe it's easier to simply fail if the device does not *match* the expected feature set, and add all of the "feature isolation" later. While a lot of guest <-> SVQ communication details are already solved for free with qemu's VirtQueue (indirect, packed, ...), we may simplify this series in particular and add the support for it later.
For example, at this moment would be valid for the device to export indirect descriptors feature flag, and SVQ simply forward that feature flag offering to the guest. So the guest <-> SVQ communication could have indirect descriptors (qemu's VirtQueue code handles it for free), but SVQ would not acknowledge it for the device. As a side note, to negotiate it would have been harmless actually, but it's not the case of packed vq.
Quoted 3 lines
So maybe for the v2 we can simply force the device to just export the strictly needed features and nothing else with qemu cmdline, and then enable the feature negotiation isolation for each side of SVQ?
Yes, that's exactly my point.
Thanks
Quoted 57 lines
Thanks!
Quoted 53 lines
Thanks!
Quoted 51 lines
Thanks
Quoted 48 lines
hw/virtio/vhost-shadow-virtqueue.c +17
-0+/* SVQ trust that guest vring is little endian */ +if (!(*dev_features &BIT_ULL (b))) { +set_bit (b, dev_features); + r = false; + } +continue ; + +default : +if (*dev_features &BIT_ULL (b)) { +clear_bit (b, dev_features); + } + } + } + +return r; +} +/* Forward guest notifications */ static void vhost_handle_guest_kick (EventNotifier *n) {hw/virtio/vhost-vdpa.c +16
-0@@ -855,10 +855,31 @@ static int vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v,size_t n_svqs = v->shadow_vqs_enabled ? hdev->nvqs :0 ;g_autoptr (GPtrArray) shadow_vqs =g_ptr_array_new_full (n_svqs, vhost_psvq_free); +uint64_t dev_features; +uint64_t svq_features; +int r; +bool ok; +if (!v->shadow_vqs_enabled) { goto out; } + r =vhost_vdpa_get_features (hdev, &dev_features); +if (r !=0 ) { +error_setg (errp,"Can't get vdpa device features, got (%d)" , r); +return r; + } + + svq_features = dev_features; + ok =vhost_svq_valid_device_features (&svq_features); +if (unlikely (!ok)) { + error_setg(errp, + "SVQ Invalid device feature flags, offer: 0x%"PRIx64", ok: 0x%"PRIx64,
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost-vdpa.c static int vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v, · line 885
Quoted 8 lines
hw/virtio/vhost-vdpa.c +5
-0+ hdev->features, svq_features); +return -1 ; + } + + shadow_vqs =g_ptr_array_new_full (hdev->nvqs, vhost_psvq_free);for (unsigned n =0 ; n < hdev->nvqs; ++n) {VhostShadowVirtqueue *svq =vhost_svq_new ();
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH 17/31] vdpa: adapt vhost_ops callbacks to svq
In reply to Eugenio Perez MartinQuoted 60 lines · Eugenio Perez Martin
Quoted 59 lines · Jason Wang
Quoted 49 lines · Eugenio Pérez
First half of the buffers forwarding part, preparing vhost-vdpa callbacks to SVQ to offer it. QEMU cannot enable it at this moment, so this is effectively dead code at the moment, but it helps to reduce patch size.
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-shadow-virtqueue.h | 2 +- hw/virtio/vhost-shadow-virtqueue.c | 21 ++++- hw/virtio/vhost-vdpa.c | 133 ++++++++++++++++++++++++++--- 3 files changed, 143 insertions(+), 13 deletions(-)hw/virtio/vhost-shadow-virtqueue.h +1
-1@@ -35,7 +35,7 @@ size_t vhost_svq_device_area_size(const VhostShadowVirtqueue *svq);void vhost_svq_stop (VhostShadowVirtqueue *svq); -VhostShadowVirtqueue *vhost_svq_new (void ); +VhostShadowVirtqueue *vhost_svq_new (uint16_t qsize);void vhost_svq_free (VhostShadowVirtqueue *vq);hw/virtio/vhost-shadow-virtqueue.c +9
-1@@ -277,9 +277,17 @@ void vhost_svq_stop(VhostShadowVirtqueue *svq)/** * Creates vhost shadow virtqueue, and instruct vhost device to use the shadow * methods and file descriptors. +* +* @qsize Shadow VirtQueue size +* +* Returns the new virtqueue or NULL. +* +* In case of error, reason is reported through error_report. */ -VhostShadowVirtqueue *vhost_svq_new (void ) +VhostShadowVirtqueue *vhost_svq_new (uint16_t qsize) { +size_t desc_size =sizeof (vring_desc_t) * qsize; +size_t device_size, driver_size; g_autofree VhostShadowVirtqueue *svq = g_new0(VhostShadowVirtqueue, 1);int r;hw/virtio/vhost-shadow-virtqueue.c VhostShadowVirtqueue *vhost_svq_new(void) · line 311
Quoted 4 lines
hw/virtio/vhost-shadow-virtqueue.c +1
-0@@ -300,6 +308,15 @@ VhostShadowVirtqueue *vhost_svq_new(void)/* Placeholder descriptor, it should be deleted at set_kick_fd */ event_notifier_init_fd (&svq->svq_kick,INVALID_SVQ_KICK_FD ); + svq->vring.num = qsize;I wonder if this is the best. E.g some hardware can support up to 32K queue size. So this will probably end up with:
Quoted 5 lines
Quoted 3 lines
1) SVQ use 32K queue size
2) hardware queue uses 256In that case SVQ vring queue size will be 32K and guest's vring can negotiate any number with SVQ equal or less than 32K,
Sorry for being unclear what I meant is actually
1) SVQ uses 32K queue size
2) guest vq uses 256
This looks like a burden that needs extra logic and may damage the performance.
And this can lead other interesting situation:
1) SVQ uses 256
2) guest vq uses 1024
Where a lot of more SVQ logic is needed.
Quoted 2 lines
including 256.
Is that what you mean?
I mean, it looks to me the logic will be much more simplified if we just allocate the shadow virtqueue with the size what guest can see (guest vring).
Then we don't need to think if the difference of the queue size can have any side effects.
Quoted 17 lines
If with hardware queues you mean guest's vring, not sure why it is "probably 256". I'd say that in that case with the virtio-net kernel driver the ring size will be the same as the device export, for example, isn't it?
The implementation should support any combination of sizes, but the ring size exposed to the guest is never bigger than hardware one.
Quoted 3 lines
? Or we SVQ can stick to 256 but this will this cause trouble if we want to add event index support?
I think we should not have any problem with event idx. If you mean that the guest could mark more buffers available than SVQ vring's size, that should not happen because there must be less entries in the guest than SVQ.
Quoted 7 lines
But if I understood you correctly, a similar situation could happen if a guest's contiguous buffer is scattered across many qemu's VA chunks. Even if that would happen, the situation should be ok too: SVQ knows the guest's avail idx and, if SVQ is full, it will continue forwarding avail buffers when the device uses more buffers.
Does that make sense to you?
Yes.
Thanks
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH 16/31] vhost: pass queue index to vhost_vq_get_addr
In reply to Eugenio Perez MartinQuoted 35 lines · Eugenio Perez Martin
Quoted 24 lines · Jason Wang
Quoted 22 lines · Eugenio Pérez
Doing that way allows vhost backend to know what address to return.
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)hw/virtio/vhost.c +3
-2@@ -798,9 +798,10 @@ static int vhost_virtqueue_set_addr(struct vhost_dev *dev,struct vhost_virtqueue *vq, unsigned idx, bool enable_log) { - struct vhost_vring_addr addr; + struct vhost_vring_addr addr = { + .index = idx, + };int r; -memset (&addr,0 ,sizeof (struct vhost_vring_addr ));Quoted 10 lines
hw/virtio/vhost.c static int vhost_virtqueue_set_addr(struct vhost_dev *dev, · line 816
Quoted 6 lines
hw/virtio/vhost.c +0
-0if (dev->vhost_ops->vhost_vq_get_addr) { r = dev->vhost_ops->vhost_vq_get_addr (dev, &addr, vq);@@ -813,7 +814,6 @@ static int vhost_virtqueue_set_addr(struct vhost_dev *dev, addr.avail_user_addr = (uint64_t )(unsigned long )vq->avail; addr.used_user_addr = (uint64_t )(unsigned long )vq->used; }I'm a bit lost in the logic above, any reason we need call
vhost_vq_get_addr():) ?
Quoted 3 lines
It's the way
vhost_virtqueue_set_addrworks if the backend has avhost_vq_get_addroperation (currently, only vhost-vdpa). vhost first ask the address to the back end and then set it.
Right it's because vhost-vdpa doesn't use VA but GPA. But I'm not sure it's worth a dedicated vhost_ops. But consider we introduce shadow virtqueue stuffs, it should be ok now.
(In the future, we may consider to generalize non vhost-vdpa specific stuffs to VhostShadowVirtqueue, then we can get rid of this vhost_ops.
Quoted 7 lines
Previously, index was not needed because all the information was in
vhost_virtqueue. However to extract queue index fromvhost_virtqueueis tricky, so I think it's easier to simply have that information at request, something similar toget_baseorget_numwhen asking vdpa device. We can extract the index from vq -dev->vqsor something similar if it's prefered.
It looks odd for the caller to tell the index consider vhost_virtqueue is already passed. So I think we need deduce it from vhost_virtqueue as you mentioned here.
Thanks
Quoted 10 lines
Thanks!
Quoted 7 lines
Thanks
Quoted 4 lines
hw/virtio/vhost.c +0
-1- addr.index = idx; addr.log_guest_addr = vq->used_phys; addr.flags = enable_log ? (1 <<VHOST_VRING_F_LOG ) :0 ; r = dev->vhost_ops->vhost_set_vring_addr (dev, &addr);
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH 18/31] vhost: Shadow virtqueue buffers forwarding
In reply to Eugenio Perez MartinQuoted 370 lines · Eugenio Perez Martin
Quoted 163 lines · Jason Wang
Quoted 161 lines · Eugenio Pérez
Initial version of shadow virtqueue that actually forward buffers. There is no iommu support at the moment, and that will be addressed in future patches of this series. Since all vhost-vdpa devices use forced IOMMU, this means that SVQ is not usable at this point of the series on any device.
For simplicity it only supports modern devices, that expects vring in little endian, with split ring and no event idx or indirect descriptors. Support for them will not be added in this series.
It reuses the VirtQueue code for the device part. The driver part is based on Linux's
virtio_ringdriver, but with stripped functionality and optimizations so it's easier to review.However, forwarding buffers have some particular pieces: One of the most unexpected ones is that a guest's buffer can expand through more than one descriptor in SVQ. While this is handled gracefully by qemu's emulated virtio devices, it may cause unexpected SVQ queue full. This patch also solves it by checking for this condition at both guest's kicks and device's calls. The code may be more elegant in the future if SVQ code runs in its own iocontext.
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-shadow-virtqueue.h | 2 + hw/virtio/vhost-shadow-virtqueue.c | 365 ++++++++++++++++++++++++++++- hw/virtio/vhost-vdpa.c | 111 ++++++++- 3 files changed, 462 insertions(+), 16 deletions(-)hw/virtio/vhost-shadow-virtqueue.h +2
-0@@ -33,6 +33,8 @@ uint16_t vhost_svq_get_num(const VhostShadowVirtqueue *svq);size_t vhost_svq_driver_area_size (const VhostShadowVirtqueue *svq);size_t vhost_svq_device_area_size (const VhostShadowVirtqueue *svq); +void vhost_svq_start (VhostShadowVirtqueue *svq,VirtIODevice *vdev, +VirtQueue *vq);void vhost_svq_stop (VhostShadowVirtqueue *svq);VhostShadowVirtqueue *vhost_svq_new (uint16_t qsize);hw/virtio/vhost-shadow-virtqueue.c +90
-5@@ -9,6 +9,8 @@#include "qemu/osdep.h" #include "hw/virtio/vhost-shadow-virtqueue.h" +#include "hw/virtio/vhost.h" +#include "hw/virtio/virtio-access.h" #include "standard-headers/linux/vhost_types.h" #include "qemu/error-report.h" @@ -36,6 +38,33 @@ typedef struct VhostShadowVirtqueue {/* Guest's call notifier, where SVQ calls guest. */ EventNotifier svq_call; + +/* Virtio queue shadowing */ +VirtQueue *vq; + +/* Virtio device */ +VirtIODevice *vdev; + +/* Map for returning guest's descriptors */ +VirtQueueElement **ring_id_maps; + +/* Next VirtQueue element that guest made available */ +VirtQueueElement *next_guest_avail_elem; + +/* Next head to expose to device */ +uint16_t avail_idx_shadow; + +/* Next free descriptor */ +uint16_t free_head; + +/* Last seen used idx */ +uint16_t shadow_used_idx; + +/* Next head to consume from device */ +uint16_t last_used_idx; + +/* Cache for the exposed notification flag */ +bool notification; } VhostShadowVirtqueue;#define INVALID_SVQ_KICK_FD -1@@ -148,30 +177,294 @@ bool vhost_svq_ack_guest_features(uint64_t dev_features,return true; } -/* Forward guest notifications */ -static void vhost_handle_guest_kick (EventNotifier *n) +/** +* Number of descriptors that SVQ can make available from the guest. +* +* @svq The svq +*/ +static uint16_t vhost_svq_available_slots (const VhostShadowVirtqueue *svq) { -VhostShadowVirtqueue *svq =container_of (n, VhostShadowVirtqueue, - svq_kick); +return svq->vring.num - (svq->avail_idx_shadow - svq->shadow_used_idx); +} + +static void vhost_svq_set_notification (VhostShadowVirtqueue *svq,bool enable) +{ +uint16_t notification_flag; -if (unlikely (!event_notifier_test_and_clear (n))) { +if (svq->notification == enable) { +return ; + } + + notification_flag =cpu_to_le16 (VRING_AVAIL_F_NO_INTERRUPT ); + + svq->notification = enable; +if (enable) { + svq->vring.avail->flags &= ~notification_flag; + }else { + svq->vring.avail->flags |= notification_flag; + } +} + +static void vhost_vring_write_descs (VhostShadowVirtqueue *svq, +const struct iovec *iovec, +size_t num,bool more_descs,bool write) +{ +uint16_t i = svq->free_head, last = svq->free_head; +unsigned n; +uint16_t flags = write ?cpu_to_le16 (VRING_DESC_F_WRITE ) :0 ; +vring_desc_t *descs = svq->vring.desc; + +if (num ==0 ) { +return ; + } + +for (n =0 ; n < num; n++) { +if (more_descs || (n +1 < num)) { + descs[i].flags = flags |cpu_to_le16 (VRING_DESC_F_NEXT ); + }else { + descs[i].flags = flags; + } + descs[i].addr =cpu_to_le64 ((hwaddr )iovec[n].iov_base); + descs[i].len =cpu_to_le32 (iovec[n].iov_len); + + last = i; + i =cpu_to_le16 (descs[i].next); + } + + svq->free_head =le16_to_cpu (descs[last].next); +} + +static unsigned vhost_svq_add_split (VhostShadowVirtqueue *svq, +VirtQueueElement *elem) +{ +int head; +unsigned avail_idx; +vring_avail_t *avail = svq->vring.avail;Quoted 9 lines
hw/virtio/vhost-shadow-virtqueue.c bool vhost_svq_ack_guest_features(uint64_t dev_features, · line 247
Quoted 5 lines
hw/virtio/vhost-shadow-virtqueue.c +5
-0+ + head = svq->free_head; + +/* We need some descriptors here */ +assert (elem->out_num || elem->in_num);Looks like this could be triggered by guest, we need fail instead assert here.
My understanding was that
virtqueue_popalready sanitized that case, but I'm not able to find where now. I will recheck and, in case it's not, I will move to a failure.Quoted 56 lines
Quoted 56 lines
hw/virtio/vhost-shadow-virtqueue.c +49
-1+ +vhost_vring_write_descs (svq, elem->out_sg, elem->out_num, + elem->in_num >0 , false); +vhost_vring_write_descs (svq, elem->in_sg, elem->in_num, false, true); + +/* +* Put entry in available array (but don't update avail->idx until they +* do sync). +*/ + avail_idx = svq->avail_idx_shadow & (svq->vring.num -1 ); + avail->ring[avail_idx] =cpu_to_le16 (head); + svq->avail_idx_shadow++; + +/* Update avail index after the descriptor is wrote */ +smp_wmb (); + avail->idx =cpu_to_le16 (svq->avail_idx_shadow); + +return head; +} + +static void vhost_svq_add (VhostShadowVirtqueue *svq,VirtQueueElement *elem) +{ +unsigned qemu_head =vhost_svq_add_split (svq, elem); + + svq->ring_id_maps[qemu_head] = elem; +} + +static void vhost_svq_kick (VhostShadowVirtqueue *svq) +{ +/* We need to expose available array entries before checking used flags */ +smp_mb (); +if (svq->vring.used->flags &VRING_USED_F_NO_NOTIFY ) {return ; }event_notifier_set (&svq->hdev_kick); } -/* Forward vhost notifications */ +/** +* Forward available buffers. +* +* @svq Shadow VirtQueue +* +* Note that this function does not guarantee that all guest's available +* buffers are available to the device in SVQ avail ring. The guest may have +* exposed a GPA / GIOVA congiuous buffer, but it may not be contiguous in qemu +* vaddr. +* +* If that happens, guest's kick notifications will be disabled until device +* makes some buffers used. +*/ +static void vhost_handle_guest_kick (VhostShadowVirtqueue *svq) +{ +/* Clear event notifier */ +event_notifier_test_and_clear (&svq->svq_kick);Quoted 9 lines
hw/virtio/vhost-shadow-virtqueue.c bool vhost_svq_ack_guest_features(uint64_t dev_features, · line 307
Quoted 5 lines
hw/virtio/vhost-shadow-virtqueue.c +5
-0+ +/* Make available as many buffers as possible */ + do { + if (virtio_queue_get_notification(svq->vq)) { + virtio_queue_set_notification(svq->vq, false);This looks like an optimization the should belong to
virtio_queue_set_notification()itself.Sure we can move.
Quoted 117 lines
Quoted 117 lines
hw/virtio/vhost-shadow-virtqueue.c +117
-0+ } + +while (true) { +VirtQueueElement *elem; + +if (svq->next_guest_avail_elem) { + elem =g_steal_pointer (&svq->next_guest_avail_elem); + }else { + elem =virtqueue_pop (svq->vq,sizeof (*elem)); + } + +if (!elem) { +break ; + } + +if (elem->out_num + elem->in_num > +vhost_svq_available_slots (svq)) { +/* +* This condition is possible since a contiguous buffer in GPA +* does not imply a contiguous buffer in qemu's VA +* scatter-gather segments. If that happen, the buffer exposed +* to the device needs to be a chain of descriptors at this +* moment. +* +* SVQ cannot hold more available buffers if we are here: +* queue the current guest descriptor and ignore further kicks +* until some elements are used. +*/ + svq->next_guest_avail_elem = elem; +return ; + } + +vhost_svq_add (svq, elem); +vhost_svq_kick (svq); + } + +virtio_queue_set_notification (svq->vq, true); + }while (!virtio_queue_empty (svq->vq)); +} + +/** +* Handle guest's kick. +* +* @n guest kick event notifier, the one that guest set to notify svq. +*/ +static void vhost_handle_guest_kick_notifier (EventNotifier *n) +{ +VhostShadowVirtqueue *svq =container_of (n, VhostShadowVirtqueue, + svq_kick); +vhost_handle_guest_kick (svq); +} + +static bool vhost_svq_more_used (VhostShadowVirtqueue *svq) +{ +if (svq->last_used_idx != svq->shadow_used_idx) { +return true; + } + + svq->shadow_used_idx =cpu_to_le16 (svq->vring.used->idx); + +return svq->last_used_idx != svq->shadow_used_idx; +} + +static VirtQueueElement *vhost_svq_get_buf (VhostShadowVirtqueue *svq) +{ +vring_desc_t *descs = svq->vring.desc; +const vring_used_t *used = svq->vring.used; +vring_used_elem_t used_elem; +uint16_t last_used; + +if (!vhost_svq_more_used (svq)) { +return NULL ; + } + +/* Only get used array entries after they have been exposed by dev */ +smp_rmb (); + last_used = svq->last_used_idx & (svq->vring.num -1 ); + used_elem.id =le32_to_cpu (used->ring[last_used].id); + used_elem.len =le32_to_cpu (used->ring[last_used].len); + + svq->last_used_idx++; +if (unlikely (used_elem.id >= svq->vring.num)) { +error_report ("Device %s says index %u is used" , svq->vdev->name, + used_elem.id); +return NULL ; + } + +if (unlikely (!svq->ring_id_maps[used_elem.id])) { +error_report ( +"Device %s says index %u is used, but it was not available" , + svq->vdev->name, used_elem.id); +return NULL ; + } + + descs[used_elem.id].next = svq->free_head; + svq->free_head = used_elem.id; + + svq->ring_id_maps[used_elem.id]->len = used_elem.len; +return g_steal_pointer (&svq->ring_id_maps[used_elem.id]); +} + +static void vhost_svq_flush (VhostShadowVirtqueue *svq, +bool check_for_avail_queue) +{ +VirtQueue *vq = svq->vq; + +/* Make as many buffers as possible used. */ + do { + unsigned i = 0; + + vhost_svq_set_notification(svq, false); + while (true) { + g_autofree VirtQueueElement *elem = vhost_svq_get_buf(svq); + if (!elem) { + break; + } +Quoted 9 lines
hw/virtio/vhost-shadow-virtqueue.c bool vhost_svq_ack_guest_features(uint64_t dev_features, · line 430
Quoted 6 lines
hw/virtio/vhost-shadow-virtqueue.c +6
-0+if (unlikely (i >= svq->vring.num)) { +virtio_error (svq->vdev, +"More than %u used buffers obtained in a %u size SVQ" , + i, svq->vring.num); +virtqueue_fill (vq, elem, elem->len, i); +virtqueue_flush (vq, i);Let's simply use
virtqueue_push()here?
Quoted 3 lines
virtqueue_pushsupport to fill and flush only one element, instead of batch. I'm fine with either but I think the less updates to the used idx, the better.
Fine.
Quoted 23 lines
Quoted 5 lines
hw/virtio/vhost-shadow-virtqueue.c bool vhost_svq_ack_guest_features(uint64_t dev_features, · line 431
Quoted 2 lines
hw/virtio/vhost-shadow-virtqueue.c +1
-0+ i =0 ;Do we need to bail out here?
Yes I guess we can simply return.
Quoted 16 lines
hw/virtio/vhost-shadow-virtqueue.c bool vhost_svq_ack_guest_features(uint64_t dev_features, · line 444
Quoted 13 lines
hw/virtio/vhost-shadow-virtqueue.c +13
-0+ } +virtqueue_fill (vq, elem, elem->len, i++); + } + +virtqueue_flush (vq, i); +event_notifier_set (&svq->svq_call); + +if (check_for_avail_queue && svq->next_guest_avail_elem) { +/* +* Avail ring was full when vhost_svq_flush was called, so it's a +* good moment to make more descriptors available if possible +*/ +vhost_handle_guest_kick (svq);Is there better to have a similar check as
vhost_handle_guest_kick()did?
Quoted 4 lines
Quoted 3 lines
if (elem->out_num + elem->in_num > vhost_svq_available_slots(svq)) {It will be duplicated when we call
vhost_handle_guest_kick, won't it?
Right, I mis-read the code.
Quoted 10 lines
Quoted 8 lines
hw/virtio/vhost-shadow-virtqueue.c bool vhost_svq_ack_guest_features(uint64_t dev_features, · line 447
Quoted 4 lines
hw/virtio/vhost-shadow-virtqueue.c +3
-0+ } + +vhost_svq_set_notification (svq, true);A
mb()is needed here? Otherwise we may lost a call here (wherevhost_svq_more_used()is run beforevhost_svq_set_notification()).I'm confused here then, I thought you said this is just a hint so there was no need? [1]. I think the memory barrier is needed too.
Yes, it's a hint but:
1) When we disable the notification, consider the notification disable is just a hint, device can still raise an interrupt, so the ordering is meaningless and a memory barrier is not necessary (the vhost_svq_set_notification(svq, false))
2) When we enable the notification, though it's a hint, the device can choose to implement it by enabling the interrupt, in this case, the notification enable should be done before checking the used. Otherwise, the checking of more used might be done before enable the notification:
1) driver check more used
2) device add more used but no notification
3) driver enable the notification then we lost a notification here
Quoted 24 lines
Quoted 16 lines
Quoted 16 lines
hw/virtio/vhost-shadow-virtqueue.c +11
-0+ }while (vhost_svq_more_used (svq)); +} + +/** +* Forward used buffers. +* +* @n hdev call event notifier, the one that device set to notify svq. +* +* Note that we are not making any buffers available in the loop, there is no +* way that it runs more than virtqueue size times. +*/ static void vhost_svq_handle_call (EventNotifier *n) {VhostShadowVirtqueue *svq =container_of (n, VhostShadowVirtqueue, hdev_call);Quoted 8 lines
hw/virtio/vhost-shadow-virtqueue.c bool vhost_svq_ack_guest_features(uint64_t dev_features, · line 465
Quoted 5 lines
hw/virtio/vhost-shadow-virtqueue.c +2
-3-if (unlikely (!event_notifier_test_and_clear (n))) { -return ; - } +/* Clear event notifier */ +event_notifier_test_and_clear (n);Any reason that we remove the above check?
Quoted 2 lines
This comes from the previous versions, where this made sure we missed no used buffers in the process of switching to SVQ mode.
I'm not sure I get here. Even if for the switching, it should be more safe the handle the flush unconditionally?
Thanks
Quoted 252 lines
If we enable SVQ from the beginning I think we can rely on getting all the device's used buffer notifications, so let me think a little bit and I can move to check the eventfd.
Quoted 175 lines
Quoted 175 lines
hw/virtio/vhost-shadow-virtqueue.c +51
-3-event_notifier_set (&svq->svq_call); +vhost_svq_flush (svq, true); }/** @@ -258,13 +551,38 @@ void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd)* need to explicitely check for them. */ event_notifier_init_fd (&svq->svq_kick, svq_kick_fd); -event_notifier_set_handler (&svq->svq_kick, vhost_handle_guest_kick); +event_notifier_set_handler (&svq->svq_kick, + vhost_handle_guest_kick_notifier);if (!check_old ||event_notifier_test_and_clear (&tmp)) {event_notifier_set (&svq->hdev_kick); } } +/** +* Start shadow virtqueue operation. +* +* @svq Shadow Virtqueue +* @vdev VirtIO device +* @vq Virtqueue to shadow +*/ +void vhost_svq_start (VhostShadowVirtqueue *svq,VirtIODevice *vdev, +VirtQueue *vq) +{ + svq->next_guest_avail_elem =NULL ; + svq->avail_idx_shadow =0 ; + svq->shadow_used_idx =0 ; + svq->last_used_idx =0 ; + svq->vdev = vdev; + svq->vq = vq; + +memset (svq->vring.avail,0 ,sizeof (*svq->vring.avail)); +memset (svq->vring.used,0 ,sizeof (*svq->vring.avail)); +for (unsigned i =0 ; i < svq->vring.num -1 ; i++) { + svq->vring.desc[i].next =cpu_to_le16 (i +1 ); + } +} +/** * Stop shadow virtqueue operation. * @svq Shadow Virtqueue @@ -272,6 +590,28 @@ void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd)void vhost_svq_stop (VhostShadowVirtqueue *svq) {event_notifier_set_handler (&svq->svq_kick,NULL ); + g_autofree VirtQueueElement *next_avail_elem = NULL; + +if (!svq->vq) { +return ; + } + +/* Send all pending used descriptors to guest */ +vhost_svq_flush (svq, false); + +for (unsigned i =0 ; i < svq->vring.num; ++i) { + g_autofree VirtQueueElement *elem = NULL; + elem =g_steal_pointer (&svq->ring_id_maps[i]); +if (elem) { +virtqueue_detach_element (svq->vq, elem, elem->len); + } + } + + next_avail_elem =g_steal_pointer (&svq->next_guest_avail_elem); +if (next_avail_elem) { +virtqueue_detach_element (svq->vq, next_avail_elem, + next_avail_elem->len); + } }/** @@ -316,7 +656,7 @@ VhostShadowVirtqueue *vhost_svq_new(uint16_t qsize)memset (svq->vring.desc,0 , driver_size); svq->vring.used =qemu_memalign (qemu_real_host_page_size, device_size);memset (svq->vring.used,0 , device_size); - + svq->ring_id_maps = g_new0(VirtQueueElement *, qsize);event_notifier_set_handler (&svq->hdev_call, vhost_svq_handle_call);return g_steal_pointer (&svq);@@ -335,6 +675,7 @@ void vhost_svq_free(VhostShadowVirtqueue *vq)event_notifier_cleanup (&vq->hdev_kick);event_notifier_set_handler (&vq->hdev_call,NULL );event_notifier_cleanup (&vq->hdev_call); +g_free (vq->ring_id_maps);qemu_vfree (vq->vring.desc);qemu_vfree (vq->vring.used);g_free (vq);hw/virtio/vhost-vdpa.c +58
-4@@ -752,9 +752,9 @@ static int vhost_vdpa_set_vring_call(struct vhost_dev *dev,* Note that this function does not rewind kick file descriptor if cannot set * call one. */ -static bool vhost_vdpa_svq_setup (struct vhost_dev *dev, -VhostShadowVirtqueue *svq, -unsigned idx) +static int vhost_vdpa_svq_set_fds (struct vhost_dev *dev, +VhostShadowVirtqueue *svq, +unsigned idx) {struct vhost_vring_file file = { .index = dev->vq_index + idx,@@ -767,7 +767,7 @@ static bool vhost_vdpa_svq_setup(struct vhost_dev *dev, r =vhost_vdpa_set_vring_dev_kick (dev, &file);if (unlikely (r !=0 )) {error_report ("Can't set device kick fd (%d)" , -r); -return false; +return r; } event_notifier =vhost_svq_get_svq_call_notifier (svq);@@ -777,6 +777,99 @@ static bool vhost_vdpa_svq_setup(struct vhost_dev *dev,error_report ("Can't set device call fd (%d)" , -r); } +return r; +} + +/** +* Unmap SVQ area in the device +*/ +static bool vhost_vdpa_svq_unmap_ring (struct vhost_vdpa *v,hwaddr iova, +hwaddr size) +{ +int r; + + size =ROUND_UP (size, qemu_real_host_page_size); + r =vhost_vdpa_dma_unmap (v, iova, size); +return r ==0 ; +} + +static bool vhost_vdpa_svq_unmap_rings (struct vhost_dev *dev, +const VhostShadowVirtqueue *svq) +{ +struct vhost_vdpa *v = dev->opaque; +struct vhost_vring_addr svq_addr; +size_t device_size =vhost_svq_device_area_size (svq); +size_t driver_size =vhost_svq_driver_area_size (svq); +bool ok; + +vhost_svq_get_vring_addr (svq, &svq_addr); + + ok =vhost_vdpa_svq_unmap_ring (v, svq_addr.desc_user_addr, driver_size); +if (unlikely (!ok)) { +return false; + } + +return vhost_vdpa_svq_unmap_ring (v, svq_addr.used_user_addr, device_size); +} + +/** +* Map shadow virtqueue rings in device +* +* @dev The vhost device +* @svq The shadow virtqueue +*/ +static bool vhost_vdpa_svq_map_rings (struct vhost_dev *dev, +const VhostShadowVirtqueue *svq) +{ +struct vhost_vdpa *v = dev->opaque; +struct vhost_vring_addr svq_addr; +size_t device_size =vhost_svq_device_area_size (svq); +size_t driver_size =vhost_svq_driver_area_size (svq); +int r; + +vhost_svq_get_vring_addr (svq, &svq_addr); + + r =vhost_vdpa_dma_map (v, svq_addr.desc_user_addr, driver_size, + (void *)svq_addr.desc_user_addr, true);Quoted 9 lines
hw/virtio/vhost-vdpa.c static bool vhost_vdpa_svq_setup(struct vhost_dev *dev, · line 839
Quoted 6 lines
hw/virtio/vhost-vdpa.c +6
-0+if (unlikely (r !=0 )) { +return false; + } + + r =vhost_vdpa_dma_map (v, svq_addr.used_user_addr, device_size, + (void *)svq_addr.used_user_addr, false);Do we need unmap the driver area if we fail here?
Yes, this used to trust in unmap them at the disabling of SVQ. Now I think we need to unmap as you say.
Thanks!
[1] https://lists.linuxfoundation.org/pipermail/virtualization/2021-March/053322.html
Quoted 56 lines
Thanks
Quoted 53 lines
hw/virtio/vhost-vdpa.c +38
-0+return r ==0 ; +} + +static bool vhost_vdpa_svq_setup (struct vhost_dev *dev, +VhostShadowVirtqueue *svq, +unsigned idx) +{ +uint16_t vq_index = dev->vq_index + idx; +struct vhost_vring_state s = { + .index = vq_index, + }; +int r; +bool ok; + + r =vhost_vdpa_set_dev_vring_base (dev, &s); +if (unlikely (r)) { +error_report ("Can't set vring base (%d)" , r); +return false; + } + + s.num =vhost_svq_get_num (svq); + r =vhost_vdpa_set_dev_vring_num (dev, &s); +if (unlikely (r)) { +error_report ("Can't set vring num (%d)" , r); +return false; + } + + ok =vhost_vdpa_svq_map_rings (dev, svq); +if (unlikely (!ok)) { +return false; + } + + r =vhost_vdpa_svq_set_fds (dev, svq, idx);return r ==0 ; }@@ -788,14 +881,24 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)if (started) {vhost_vdpa_host_notifiers_init (dev);for (unsigned i =0 ; i < v->shadow_vqs->len; ++i) { +VirtQueue *vq =virtio_get_queue (dev->vdev, dev->vq_index + i);VhostShadowVirtqueue *svq =g_ptr_array_index (v->shadow_vqs, i);bool ok =vhost_vdpa_svq_setup (dev, svq, i);if (unlikely (!ok)) {return -1 ; } +vhost_svq_start (svq, dev->vdev, vq); }vhost_vdpa_set_vring_ready (dev); }else { +for (unsigned i =0 ; i < v->shadow_vqs->len; ++i) { +VhostShadowVirtqueue *svq =g_ptr_array_index (v->shadow_vqs, + i);
Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost-vdpa.c static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started) · line 904
Quoted 8 lines
hw/virtio/vhost-vdpa.c +5
-0+bool ok =vhost_vdpa_svq_unmap_rings (dev, svq); +if (unlikely (!ok)) { +return -1 ; + } + }vhost_vdpa_host_notifiers_uninit (dev, dev->nvqs); }
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH 18/31] vhost: Shadow virtqueue buffers forwarding
In reply to Eugenio Perez MartinQuoted 21 lines · Eugenio Perez Martin
Quoted 17 lines · Jason Wang
hw/virtio/vhost-shadow-virtqueue.c void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd) · line 600
Quoted 12 lines · Eugenio Pérez
hw/virtio/vhost-shadow-virtqueue.c +8
-0@@ -272,6 +590,28 @@ void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd)void vhost_svq_stop (VhostShadowVirtqueue *svq) {event_notifier_set_handler (&svq->svq_kick,NULL ); + g_autofree VirtQueueElement *next_avail_elem = NULL; + +if (!svq->vq) { +return ; + } + +/* Send all pending used descriptors to guest */ +vhost_svq_flush (svq, false);Do we need to wait for all the pending descriptors to be completed here?
No, this function does not wait, it only completes the forwarding of the *used* descriptors.
Quoted 8 lines
The best example is the net rx queue in my opinion. This call will check SVQ's vring
used_idxand will forward the last used descriptors if any, but all available descriptors will remain as available for qemu's VQ code.To skip it would miss those last rx descriptors in migration.
Thanks!
So it's probably to not the best place to ask. It's more about the inflight descriptors so it should be TX instead of RX.
I can imagine the migration last phase, we should stop the vhost-vDPA before calling vhost_svq_stop(). Then we should be fine regardless of inflight descriptors.
Thanks
Quoted 4 lines
Quoted 4 lines
Thanks
Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
hw/virtio/vhost-shadow-virtqueue.c +7
-0+ +for (unsigned i =0 ; i < svq->vring.num; ++i) { + g_autofree VirtQueueElement *elem = NULL; + elem =g_steal_pointer (&svq->ring_id_maps[i]); +if (elem) { +virtqueue_detach_element (svq->vq, elem, elem->len); + }Quoted 8 lines
hw/virtio/vhost-shadow-virtqueue.c void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd) · line 615
Quoted 8 lines
hw/virtio/vhost-shadow-virtqueue.c +7
-0+ } + + next_avail_elem =g_steal_pointer (&svq->next_guest_avail_elem); +if (next_avail_elem) { +virtqueue_detach_element (svq->vq, next_avail_elem, + next_avail_elem->len); + } }
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH 22/31] vhost: Add VhostIOVATree
In reply to Eugenio Perez MartinQuoted 18 lines · Eugenio Perez Martin
Quoted 11 lines · Jason Wang
Quoted 4 lines · Eugenio Pérez
This tree is able to look for a translated address from an IOVA address.
At first glance it is similar to util/iova-tree. However, SVQ working on devices with limited IOVA space need more capabilities,
So did the IOVA tree (e.g l2 vtd can only work in the range of GAW and without RMRRs).
Quoted 6 lines
Quoted 2 lines
like allocating
IOVA chunks or performing reverse translations (qemu addresses to iova).This looks like a general request as well. So I wonder if we can simply extend iova tree instead.
Quoted 7 lines
While both are true, I don't see code that performs allocations or qemu vaddr to iova translations. But if the changes can be integrated into iova-tree that would be great for sure.
The main drawback I see is the need to maintain two trees instead of one for users of iova-tree. While complexity does not grow, it needs to double the amount of work needed.
If you care about the performance, we can disable the reverse mapping during the allocation. For vIOMMU users it won't notice any performance penalty.
Thanks
Quoted 226 lines
Thanks!
Quoted 223 lines
Thanks
Quoted 220 lines
The allocation capability, as "assign a free IOVA address to this chunk of memory in qemu's address space" allows shadow virtqueue to create a new address space that is not restricted by guest's addressable one, so we can allocate shadow vqs vrings outside of it.
It duplicates the tree so it can search efficiently both directions, and it will signal overlap if iova or the translated address is present in any tree.
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-iova-tree.h | 27 +++++++ hw/virtio/vhost-iova-tree.c | 157 ++++++++++++++++++++++++++++++++++++ hw/virtio/meson.build | 2 +- 3 files changed, 185 insertions(+), 1 deletion(-)create mode 100644
hw/virtio/vhost-iova-tree.h
create mode 100644hw/virtio/vhost-iova-tree.chw/virtio/vhost-iova-tree.h new file +27
-0@@ -0,0 +1,27 @@ +/* +* vhost software live migration ring +* +* SPDX-FileCopyrightText: Red Hat, Inc. 2021 +* SPDX-FileContributor: Author: Eugenio Pérez <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef HW_VIRTIO_VHOST_IOVA_TREE_H +#define HW_VIRTIO_VHOST_IOVA_TREE_H + +#include "qemu/iova-tree.h" +#include "exec/memory.h" + +typedef struct VhostIOVATree VhostIOVATree ; + +VhostIOVATree *vhost_iova_tree_new (uint64_t iova_first,uint64_t iova_last); +void vhost_iova_tree_delete (VhostIOVATree *iova_tree); +G_DEFINE_AUTOPTR_CLEANUP_FUNC (VhostIOVATree, vhost_iova_tree_delete); + +const DMAMap *vhost_iova_tree_find_iova (const VhostIOVATree *iova_tree, +const DMAMap *map); +int vhost_iova_tree_map_alloc (VhostIOVATree *iova_tree,DMAMap *map); +void vhost_iova_tree_remove (VhostIOVATree *iova_tree,const DMAMap *map); + +#endif hw/virtio/vhost-iova-tree.c new file +157
-0@@ -0,0 +1,157 @@ +/* +* vhost software live migration ring +* +* SPDX-FileCopyrightText: Red Hat, Inc. 2021 +* SPDX-FileContributor: Author: Eugenio Pérez <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "qemu/osdep.h" +#include "qemu/iova-tree.h" +#include "vhost-iova-tree.h" + +#define iova_min_addr qemu_real_host_page_size + +/** +* VhostIOVATree, able to: +* - Translate iova address +* - Reverse translate iova address (from translated to iova) +* - Allocate IOVA regions for translated range (potentially slow operation) +* +* Note that it cannot remove nodes. +*/ +struct VhostIOVATree { +/* First addresable iova address in the device */ +uint64_t iova_first; + +/* Last addressable iova address in the device */ +uint64_t iova_last; + +/* IOVA address to qemu memory maps. */ +IOVATree *iova_taddr_map; + +/* QEMU virtual memory address to iova maps */ +GTree *taddr_iova_map; +}; + +static gint vhost_iova_tree_cmp_taddr (gconstpointer a,gconstpointer b, +gpointer data) +{ +const DMAMap *m1 = a, *m2 = b; + +if (m1->translated_addr > m2->translated_addr + m2->size) { +return 1 ; + } + +if (m1->translated_addr + m1->size < m2->translated_addr) { +return -1 ; + } + +/* Overlapped */ +return 0 ; +} + +/** +* Create a new IOVA tree +* +* Returns the new IOVA tree +*/ +VhostIOVATree *vhost_iova_tree_new (hwaddr iova_first,hwaddr iova_last) +{ +VhostIOVATree *tree =g_new (VhostIOVATree,1 ); + +/* Some devices does not like 0 addresses */ + tree->iova_first =MAX (iova_first, iova_min_addr); + tree->iova_last = iova_last; + + tree->iova_taddr_map =iova_tree_new (); + tree->taddr_iova_map =g_tree_new_full (vhost_iova_tree_cmp_taddr,NULL , +NULL , g_free); +return tree; +} + +/** +* Delete an iova tree +*/ +void vhost_iova_tree_delete (VhostIOVATree *iova_tree) +{ +iova_tree_destroy (iova_tree->iova_taddr_map); +g_tree_unref (iova_tree->taddr_iova_map); +g_free (iova_tree); +} + +/** +* Find the IOVA address stored from a memory address +* +* @tree The iova tree +* @map The map with the memory address +* +* Return the stored mapping, or NULL if not found. +*/ +const DMAMap *vhost_iova_tree_find_iova (const VhostIOVATree *tree, +const DMAMap *map) +{ +return g_tree_lookup (tree->taddr_iova_map, map); +} + +/** +* Allocate a new mapping +* +* @tree The iova tree +* @map The iova map +* +* Returns: +* - IOVA_OK if the map fits in the container +* - IOVA_ERR_INVALID if the map does not make sense (like size overflow) +* - IOVA_ERR_OVERLAP if the tree already contains that map +* - IOVA_ERR_NOMEM if tree cannot allocate more space. +* +* It returns assignated iova in map->iova if return value is VHOST_DMA_MAP_OK. +*/ +int vhost_iova_tree_map_alloc (VhostIOVATree *tree,DMAMap *map) +{ +/* Some vhost devices does not like addr 0. Skip first page */ +hwaddr iova_first = tree->iova_first ?: qemu_real_host_page_size; +DMAMap *new; +int r; + +if (map->translated_addr + map->size < map->translated_addr || + map->perm ==IOMMU_NONE ) { +return IOVA_ERR_INVALID ; + } + +/* Check for collisions in translated addresses */ +if (vhost_iova_tree_find_iova (tree, map)) { +return IOVA_ERR_OVERLAP ; + } + +/* Allocate a node in IOVA address */ + r =iova_tree_alloc (tree->iova_taddr_map, map, iova_first, + tree->iova_last); +if (r !=IOVA_OK ) { +return r; + } + +/* Allocate node in qemu -> iova translations */ + new =g_malloc (sizeof (*new)); +memcpy (new, map,sizeof (*new)); +g_tree_insert (tree->taddr_iova_map, new, new); +return IOVA_OK ; +} + +/** +* Remove existing mappings from iova tree +* +* @param iova_tree The vhost iova tree +* @param map The map to remove +*/ +void vhost_iova_tree_remove (VhostIOVATree *iova_tree,const DMAMap *map) +{ +const DMAMap *overlap; + +iova_tree_remove (iova_tree->iova_taddr_map, map); +while ((overlap =vhost_iova_tree_find_iova (iova_tree, map))) { +g_tree_remove (iova_tree->taddr_iova_map, overlap); + } +}hw/virtio/meson.build +0
-0@@ -11,7 +11,7 @@ softmmu_ss.add(when: 'CONFIG_ALL', if_true: files('vhost-stub.c'))
Quoted 7 lines
Quoted 7 lines
hw/virtio/meson.build softmmu_ss.add(when: 'CONFIG_ALL', if_true: files('vhost-stub.c')) · line 17
Quoted 7 lines
hw/virtio/meson.build +1
-1virtio_ss = ss.source_set() virtio_ss.add(files('virtio.c')) -virtio_ss.add(when: 'CONFIG_VHOST', if_true: files('vhost.c', 'vhost-backend.c', 'vhost-shadow-virtqueue.c')) +virtio_ss.add(when: 'CONFIG_VHOST', if_true: files('vhost.c', 'vhost-backend.c', 'vhost-shadow-virtqueue.c', 'vhost-iova-tree.c')) virtio_ss.add(when: 'CONFIG_VHOST_USER', if_true: files('vhost-user.c')) virtio_ss.add(when: 'CONFIG_VHOST_VDPA', if_true: files('vhost-vdpa.c')) virtio_ss.add(when: 'CONFIG_VIRTIO_BALLOON', if_true: files('virtio-balloon.c'))
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH 23/31] vdpa: Add custom IOTLB translations to SVQ
In reply to Eugenio Perez MartinQuoted 11 lines · Eugenio Perez Martin
Quoted 11 lines
hw/virtio/vhost-shadow-virtqueue.c static void vhost_svq_set_notification(VhostShadowVirtqueue *svq, bool enable) · line 245
Quoted 7 lines
hw/virtio/vhost-shadow-virtqueue.c +7
-0+return false; + } + +/* +* Map->iova chunk size is ignored. What to do if descriptor +* (addr, size) does not fit is delegated to the device. +*/ I think we need at least check the size and fail if the size doesn't match here. Or is it possible that we have a buffer that may cross two memory regions?
Quoted 7 lines
It should be impossible, since both
iova_treeand VirtQueue should be in sync regarding the memory regions updates. If a VirtQueue buffer crosses many memory regions, iovec has more entries.I can add a return false, but I'm not able to trigger that situation even with a malformed driver.
Ok, but it won't harm to add a warn here.
Thanks
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH 28/31] vdpa: Expose VHOST_F_LOG_ALL on SVQ
In reply to Eugenio Perez MartinQuoted 53 lines · Eugenio Perez Martin
Quoted 38 lines · Jason Wang
Quoted 36 lines · Eugenio Pérez
SVQ is able to log the dirty bits by itself, so let's use it to not block migration.
Also, ignore set and clear of
VHOST_F_LOG_ALLonset_featuresif SVQ is enabled. Even if the device supports it, the reports would be nonsense because SVQ memory is in the qemu region.The log region is still allocated. Future changes might skip that, but this series is already long enough.
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-vdpa.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)hw/virtio/vhost-vdpa.c +4
-0@@ -1022,6 +1022,9 @@ static int vhost_vdpa_get_features(struct vhost_dev *dev, uint64_t *features)if (ret ==0 && v->shadow_vqs_enabled) {/* Filter only features that SVQ can offer to guest */ vhost_svq_valid_guest_features (features); + +/* Add SVQ logging capabilities */ + *features |=BIT_ULL (VHOST_F_LOG_ALL ); }return ret;@@ -1039,8 +1042,25 @@ static int vhost_vdpa_set_features(struct vhost_dev *dev,if (v->shadow_vqs_enabled) {uint64_t dev_features, svq_features, acked_features; +uint8_t status =0 ;bool ok;Quoted 14 lines
hw/virtio/vhost-vdpa.c static int vhost_vdpa_set_features(struct vhost_dev *dev, · line 1057
Quoted 10 lines
hw/virtio/vhost-vdpa.c +10
-0+ ret =vhost_vdpa_call (dev,VHOST_VDPA_GET_STATUS , &status); +if (unlikely (ret)) { +return ret; + } + +if (status &VIRTIO_CONFIG_S_DRIVER_OK ) { +/* +* vhost is trying to enable or disable _F_LOG, and the device +* would report wrong dirty pages. SVQ handles it. +*/ I fail to understand this comment, I'd think there's no way to disable dirty page tracking for SVQ.
Quoted 3 lines
vhost_log_global_{start,stop} are called at the beginning and end of migration. To inform the device that it should start logging, they set or clean
VHOST_F_LOG_ALLatvhost_dev_set_log.
Yes, but for SVQ, we can't disable dirty page tracking, isn't it? The only thing is to ignore or filter out the F_LOG_ALL and pretend to be enabled and disabled.
Quoted 4 lines
While SVQ does not use
VHOST_F_LOG_ALL, it exports the feature bit so vhost does not block migration. Maybe we need to look for another way to do this?
I'm fine with filtering since it's much more simpler, but I fail to understand why we need to check DRIVER_OK.
Thanks
Quoted 6 lines
Thanks!
Quoted 3 lines
Thanks
Quoted 9 lines
Quoted 9 lines
hw/virtio/vhost-vdpa.c static int vhost_vdpa_set_features(struct vhost_dev *dev, · line 1066
Quoted 9 lines
hw/virtio/vhost-vdpa.c +6
-0+return 0 ; + } + +/* We must not ack _F_LOG if SVQ is enabled */ + features &= ~BIT_ULL (VHOST_F_LOG_ALL ); + ret =vhost_vdpa_get_dev_features (dev, &dev_features);if (ret !=0 ) {error_report ("Can't get vdpa device features, got (%d)" , ret);
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 55 lines · Eugenio Perez Martin
Quoted 54 lines · Jason Wang
Quoted 52 lines · Eugenio Pérez
This series enables shadow virtqueue (SVQ) for vhost-vdpa devices. This is intended as a new method of tracking the memory the devices touch during a migration process: Instead of relay on vhost device's dirty logging capability, SVQ intercepts the VQ dataplane forwarding the descriptors between VM and device. This way qemu is the effective writer of guests memory, like in qemu's emulated virtio device operation.
When SVQ is enabled qemu offers a new virtual address space to the device to read and write into, and it maps new vrings and the guest memory in it. SVQ also intercepts kicks and calls between the device and the guest. Used buffers relay would cause dirty memory being tracked, but at this RFC SVQ is not enabled on migration automatically.
Thanks of being a buffers relay system, SVQ can be used also to communicate devices and drivers with different capabilities, like devices that only support packed vring and not split and old guests with no driver packed support.
It is based on the ideas of DPDK SW assisted LM, in the series of DPDK's https://patchwork.dpdk.org/cover/48370/ . However, these does not map the shadow vq in guest's VA, but in qemu's.
This version of SVQ is limited in the amount of features it can use with guest and device, because this series is already very big otherwise. Features like indirect or
event_idxwill be addressed in future series.SVQ needs to be enabled with cmdline parameter x-svq, like:
-netdev type=vhost-vdpa,vhostdev=/dev/vhost-vdpa-0,id=vhost-vdpa0,x-svq=true
In this version it cannot be enabled or disabled in runtime. Further series will remove this limitation and will enable it only for migration time.
Some patches are intentionally very small to ease review, but they can be squashed if preferred.
Patches 1-10 prepares the SVQ and QEMU to support both guest to device and device to guest notifications forwarding, with the extra qemu hop. That part can be tested in isolation if cmdline change is reproduced.
Patches from 11 to 18 implement the actual buffer forwarding, but with no IOMMU support. It requires a vdpa device capable of addressing all qemu vaddr.
Patches 19 to 23 adds the iommu support, so the device with address range limitations can access SVQ through this new virtual address space created.
The rest of the series add the last pieces needed for migration.
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Comments are welcome.
I wonder the performance impact. So performance numbers are more than welcomed.
Sure, I'll do it for the next revision. Since this one brings a decent amount of changes, I chose to collect the feedback first.
A simple single TCP_STREAM netperf test should be sufficient to give some basic understanding about the performance impact.
Thanks
Quoted 143 lines
Thanks!
Quoted 140 lines
Thanks
Quoted 137 lines
TODO:
* Event, indirect, packed, and other features of virtio.
* To separate buffers forwarding in its own AIO context, so we can throw more threads to that task and we don't need to stop the main event loop.
* Support virtio-net control vq.
* Proper documentation.Changes from v5 RFC:
* Remove dynamic enablement of SVQ, making less dependent of the device.
* Enable live migration if SVQ is enabled.
* Fix SVQ when driver reset.
* Comments addressed, specially in the iova area.
* Rebase on latest master, adding multiqueue support (but no networking control vq processing).
v5 link:
https://lists.gnu.org/archive/html/qemu-devel/2021-10/msg07250.htmlChanges from v4 RFC:
* Support of allocating / freeing iova ranges in IOVA tree. Extending already present iova-tree for that.
* Proper validation of guest features. Now SVQ can negotiate a different set of features with the device when enabled.
* Support of host notifiers memory regions
* Handling of SVQ full queue in case guest's descriptors span to different memory regions (qemu's VA chunks).
* Flush pending used buffers at end of SVQ operation.
* QMP command now looks by NetClientState name. Other devices will need to implement it's way to enable vdpa.
* Rename QMP command to set, so it looks more like a way of working
* Better use of qemu error system
* Make a few assertions proper error-handling paths.
* Add more documentation
* Less coupling of virtio / vhost, that could cause friction on changes
* Addressed many other small comments and small fixes.Changes from v3 RFC:
* Move everything to vhost-vdpa backend. A big change, this allowed some cleanup but more code has been added in other places.
* More use of glib utilities, especially to manage memory.
v3 link:
https://lists.nongnu.org/archive/html/qemu-devel/2021-05/msg06032.htmlChanges from v2 RFC:
* Adding vhost-vdpa devices support
* Fixed some memory leaks pointed by different comments
v2 link:
https://lists.nongnu.org/archive/html/qemu-devel/2021-03/msg05600.htmlChanges from v1 RFC:
* Use QMP instead of migration to start SVQ mode.
* Only accepting IOMMU devices, closer behavior with target devices (vDPA)
* Fix invalid masking/unmasking of vhost call fd.
* Use of proper methods for synchronization.
* No need to modify VirtIO device code, all of the changes are contained in vhost code.
* Delete superfluous code.
* An intermediate RFC was sent with only the notifications forwarding changes. It can be seen in
https://patchew.org/QEMU/[email protected]/
v1 link:
https://lists.gnu.org/archive/html/qemu-devel/2020-11/msg05372.htmlEugenio Pérez (20):
virtio: AddVIRTIO_F_QUEUE_STATE
virtio-net: HonorVIRTIO_CONFIG_S_DEVICE_STOPPED
virtio: Addvirtio_queue_is_host_notifier_enabled
vhost: Make vhost_virtqueue_{start,stop} public
vhost: Add x-vhost-enable-shadow-vq qmp
vhost: Add VhostShadowVirtqueue
vdpa: Register vdpa devices in a list
vhost: Routeguest->hostnotification through shadow virtqueue Addvhost_svq_get_svq_call_notifier
Addvhost_svq_set_guest_call_notifier
vdpa: Savecall_fdin vhost-vdpa
vhost-vdpa: Take into account SVQ invhost_vdpa_set_vring_call
vhost: Routehost->guestnotification through shadow virtqueue
virtio: Addvhost_shadow_vq_get_vring_addr
vdpa: Save host and guest features
vhost: Addvhost_svq_valid_device_featuresto shadow vq
vhost: Shadow virtqueue buffers forwarding
vhost: Add VhostIOVATree
vhost: Use a tree to store memory mappings
vdpa: Add custom IOTLB translations to SVQEugenio Pérez (31):
vdpa: Reordervirtio/vhost-vdpa.cfunctions
vhost: Add VhostShadowVirtqueue
vdpa: Addvhost_svq_get_dev_kick_notifier
vdpa: Addvhost_svq_set_svq_kick_fd
vhost: Add Shadow VirtQueue kick forwarding capabilities
vhost: Routeguest->hostnotification through shadow virtqueue
vhost: ddvhost_svq_get_svq_call_notifier
vhost: Addvhost_svq_set_guest_call_notifier
vhost-vdpa: Take into account SVQ invhost_vdpa_set_vring_call
vhost: Routehost->guestnotification through shadow virtqueue
vhost: Addvhost_svq_valid_device_featuresto shadow vq
vhost: Addvhost_svq_valid_guest_featuresto shadow vq
vhost: Addvhost_svq_ack_guest_featuresto shadow vq
virtio: Addvhost_shadow_vq_get_vring_addr
vdpa: Addvhost_svq_get_num
vhost: pass queue index tovhost_vq_get_addr
vdpa: adaptvhost_opscallbacks to svq
vhost: Shadow virtqueue buffers forwarding
utils: Add internal DMAMap to iova-tree
util: Store DMA entries in a list
util: Addiova_tree_alloc
vhost: Add VhostIOVATree
vdpa: Add custom IOTLB translations to SVQ
vhost: Addvhost_svq_get_last_used_idx
vdpa: Adaptvhost_vdpa_get_vring_baseto SVQ
vdpa: ClearVHOST_VRING_F_LOGatvhost_vdpa_set_vring_addrin SVQ
vdpa: Never setlog_baseaddr if SVQ is enabled
vdpa: ExposeVHOST_F_LOG_ALLon SVQ
vdpa: Make ncs autofree
vdpa: Movevhost_vdpa_get_iova_rangetonet/vhost-vdpa.c
vdpa: Add x-svq to NetdevVhostVDPAOptionsqapi/net.json | 5 +- hw/virtio/vhost-iova-tree.h | 27 + hw/virtio/vhost-shadow-virtqueue.h | 46 ++ include/hw/virtio/vhost-vdpa.h | 7 + include/qemu/iova-tree.h | 17 + hw/virtio/vhost-iova-tree.c | 157 ++++++ hw/virtio/vhost-shadow-virtqueue.c | 761 +++++++++++++++++++++++++++++ hw/virtio/vhost-vdpa.c | 740 ++++++++++++++++++++++++---- hw/virtio/vhost.c | 6 +- net/vhost-vdpa.c | 58 ++- util/iova-tree.c | 161 +++++- hw/virtio/meson.build | 2 +- 12 files changed, 1852 insertions(+), 135 deletions(-)create mode 100644
hw/virtio/vhost-iova-tree.hcreate mode 100644hw/virtio/vhost-shadow-virtqueue.hcreate mode 100644hw/virtio/vhost-iova-tree.ccreate mode 100644hw/virtio/vhost-shadow-virtqueue.c
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH 04/31] vdpa: Add vhost_svq_set_svq_kick_fd
In reply to Eugenio Perez MartinQuoted 80 lines · Eugenio Perez Martin
Quoted 70 lines · Jason Wang
Quoted 68 lines · Eugenio Pérez
This function allows the vhost-vdpa backend to override
kick_fd.Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-shadow-virtqueue.h | 1 + hw/virtio/vhost-shadow-virtqueue.c | 45 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+)hw/virtio/vhost-shadow-virtqueue.h +1
-0@@ -15,6 +15,7 @@typedef struct VhostShadowVirtqueue VhostShadowVirtqueue ; +void vhost_svq_set_svq_kick_fd (VhostShadowVirtqueue *svq,int svq_kick_fd);const EventNotifier *vhost_svq_get_dev_kick_notifier (const VhostShadowVirtqueue *svq);hw/virtio/vhost-shadow-virtqueue.c +24
-0@@ -11,6 +11,7 @@#include "hw/virtio/vhost-shadow-virtqueue.h" #include "qemu/error-report.h" +#include "qemu/main-loop.h" /* Shadow virtqueue to relay notifications */ typedef struct VhostShadowVirtqueue {@@ -18,8 +19,20 @@ typedef struct VhostShadowVirtqueue {EventNotifier hdev_kick;/* Shadow call notifier, sent to vhost */ EventNotifier hdev_call; + +/* +* Borrowed virtqueue's guest to host notifier. +* To borrow it in this event notifier allows to register on the event +* loop and access the associated shadow virtqueue easily. If we use the +* VirtQueue, we don't have an easy way to retrieve it. +* +* So shadow virtqueue must not clean it, or we would lose VirtQueue one. +*/ +EventNotifier svq_kick; } VhostShadowVirtqueue; +#define INVALID_SVQ_KICK_FD -1 +/** * The notifier that SVQ will use to notify the device. */ @@ -29,6 +42,35 @@ const EventNotifier *vhost_svq_get_dev_kick_notifier(return &svq->hdev_kick; } +/** +* Set a new file descriptor for the guest to kick SVQ and notify for avail +* +* @svq The svq +* @svq_kick_fd The new svq kick fd +*/ +void vhost_svq_set_svq_kick_fd (VhostShadowVirtqueue *svq,int svq_kick_fd) +{ +EventNotifier tmp; +bool check_old =INVALID_SVQ_KICK_FD != +event_notifier_get_fd (&svq->svq_kick);Quoted 9 lines
hw/virtio/vhost-shadow-virtqueue.c const EventNotifier *vhost_svq_get_dev_kick_notifier( · line 60
Quoted 5 lines
hw/virtio/vhost-shadow-virtqueue.c +5
-0+ +if (check_old) { +event_notifier_set_handler (&svq->svq_kick,NULL ); +event_notifier_init_fd (&tmp,event_notifier_get_fd (&svq->svq_kick)); + }It looks to me we don't do similar things in vhost-net. Any reason for caring about the old
svq_kick?
Quoted 2 lines
Do you mean to check for old
kick_fdin case we miss notifications, and explicitly omit theINVALID_SVQ_KICK_FD?
Yes.
Quoted 6 lines
If you mean qemu's vhost-net, I guess it's because the device's kick fd is never changed in all the vhost device lifecycle, it's only set at the beginning. Previous RFC also depended on that, but you suggested better vhost and SVQ in v4 feedback if I understood correctly [1]. Or am I missing something?
No, I forgot that. But in this case we should have a better dealing with the the conversion from valid fd to -1 by disabling the handler.
Quoted 24 lines
Qemu's vhost-net does not need to use this because it is not polling it. For kernel's vhost, I guess the closest is the use of pollstop and pollstart at
vhost_vring_ioctl.In my opinion, I think that SVQ code size can benefit from now allowing to override
kick_fdfrom the start of the operation. Not from initialization, but start. But I can see the benefits of having the change into account from this moment so it's more resilient to the future.Quoted 4 lines
Quoted 4 lines
hw/virtio/vhost-shadow-virtqueue.c +4
-0+ +/* +* event_notifier_set_handler already checks for guest's notifications if +* they arrive to the new file descriptor in the switch, so there is no Quoted 9 lines
hw/virtio/vhost-shadow-virtqueue.c const EventNotifier *vhost_svq_get_dev_kick_notifier( · line 70
Quoted 6 lines
hw/virtio/vhost-shadow-virtqueue.c +6
-0+* need to explicitely check for them. +*/ +event_notifier_init_fd (&svq->svq_kick, svq_kick_fd); + +if (!check_old ||event_notifier_test_and_clear (&tmp)) { +event_notifier_set (&svq->hdev_kick);Any reason we need to kick the device directly here?
Quoted 3 lines
At this point of the series only notifications are forwarded, not buffers. If
kick_fdis set, we need to check the old one, the same way as vhost checks the masked notifier in case of change.
I meant we need to kick the svq instead of vhost-vdpa in this case?
Thanks
Quoted 20 lines
Thanks!
[1] https://lists.gnu.org/archive/html/qemu-devel/2021-10/msg03152.html
, from "I'd suggest to not depend on this since it:"Quoted 13 lines
Thanks
Quoted 10 lines
hw/virtio/vhost-shadow-virtqueue.c +3
-0+ } +} +/** * Creates vhost shadow virtqueue, and instruct vhost device to use the shadow * methods and file descriptors. @@ -52,6 +94,9 @@ VhostShadowVirtqueue *vhost_svq_new(void) goto err_init_hdev_call; }
Quoted 6 lines
Quoted 6 lines
hw/virtio/vhost-shadow-virtqueue.c VhostShadowVirtqueue *vhost_svq_new(void) · line 102
Quoted 6 lines
hw/virtio/vhost-shadow-virtqueue.c +3
-0+/* Placeholder descriptor, it should be deleted at set_kick_fd */ +event_notifier_init_fd (&svq->svq_kick,INVALID_SVQ_KICK_FD ); +return g_steal_pointer (&svq); err_init_hdev_call:
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH 06/31] vhost: Route guest->host notification through shadow virtqueue
In reply to Eugenio Perez MartinQuoted 7 lines · Eugenio Perez Martin
Quoted 6 lines · Jason Wang
Quoted 4 lines · Eugenio Pérez
At this moment no buffer forwarding will be performed in SVQ mode: Qemu just forward the guest's kicks to the device. This commit also set up SVQs in the vhost device.
Quoted 7 lines
Quoted 6 lines
Quoted 2 lines
Host memory notifiers regions are left out for simplicity, and they will not be addressed in this series.
I wonder if it's better to squash this into patch 5 since it gives us a full
guest->hostforwarding.I'm fine with that if you think it makes the review easier.
Yes please.
Quoted 168 lines
Quoted 55 lines
Quoted 55 lines
Signed-off-by Eugenio Pérez <[email protected]>--- include/hw/virtio/vhost-vdpa.h | 4 ++ hw/virtio/vhost-vdpa.c | 122 ++++++++++++++++++++++++++++++++- 2 files changed, 124 insertions(+), 2 deletions(-)include/hw/virtio/vhost-vdpa.h +4
-0@@ -12,6 +12,8 @@#ifndef HW_VIRTIO_VHOST_VDPA_H #define HW_VIRTIO_VHOST_VDPA_H +#include <gmodule.h> +#include "hw/virtio/virtio.h" #include "standard-headers/linux/vhost_types.h" @@ -27,6 +29,8 @@ typedef struct vhost_vdpa {bool iotlb_batch_begin_sent;MemoryListener listener;struct vhost_vdpa_iova_range iova_range; +bool shadow_vqs_enabled; +GPtrArray *shadow_vqs;struct vhost_dev *dev;VhostVDPAHostNotifier notifier[VIRTIO_QUEUE_MAX ]; } VhostVDPA;hw/virtio/vhost-vdpa.c +3
-0@@ -17,12 +17,14 @@#include "hw/virtio/vhost.h" #include "hw/virtio/vhost-backend.h" #include "hw/virtio/virtio-net.h" +#include "hw/virtio/vhost-shadow-virtqueue.h" #include "hw/virtio/vhost-vdpa.h" #include "exec/address-spaces.h" #include "qemu/main-loop.h" #include "cpu.h" #include "trace.h" #include "qemu-common.h" +#include "qapi/error.h" /* * Return one past the end of the end of section. Be careful with uint64_t @@ -409,8 +411,14 @@ err:static void vhost_vdpa_host_notifiers_init (struct vhost_dev *dev) { +struct vhost_vdpa *v = dev->opaque;int i;Quoted 5 lines
hw/virtio/vhost-vdpa.c err: · line 418
Quoted 2 lines
hw/virtio/vhost-vdpa.c +2
-0+if (v->shadow_vqs_enabled) { +/* SVQ is not compatible with host notifiers mr */ I guess there should be a TODO or FIXME here.
Sure I can add it.
Quoted 97 lines
Quoted 97 lines
hw/virtio/vhost-vdpa.c +52
-2+return ; + } +for (i = dev->vq_index; i < dev->vq_index + dev->nvqs; i++) {if (vhost_vdpa_host_notifier_init (dev, i)) { goto err;@@ -424,6 +432,17 @@ err:return ; } +static void vhost_vdpa_svq_cleanup (struct vhost_dev *dev) +{ +struct vhost_vdpa *v = dev->opaque; +size_t idx; + +for (idx =0 ; idx < v->shadow_vqs->len; ++idx) { +vhost_svq_stop (g_ptr_array_index (v->shadow_vqs, idx)); + } +g_ptr_array_free (v->shadow_vqs, true); +} +static int vhost_vdpa_cleanup (struct vhost_dev *dev) {struct vhost_vdpa *v;@@ -432,6 +451,7 @@ static int vhost_vdpa_cleanup(struct vhost_dev *dev)trace_vhost_vdpa_cleanup (dev, v);vhost_vdpa_host_notifiers_uninit (dev, dev->nvqs);memory_listener_unregister (&v->listener); +vhost_vdpa_svq_cleanup (dev); dev->opaque =NULL ;ram_block_discard_disable (false);@@ -507,9 +527,15 @@ static int vhost_vdpa_get_device_id(struct vhost_dev *dev,static int vhost_vdpa_reset_device (struct vhost_dev *dev) { +struct vhost_vdpa *v = dev->opaque;int ret;uint8_t status =0 ; +for (unsigned i =0 ; i < v->shadow_vqs->len; ++i) { +VhostShadowVirtqueue *svq =g_ptr_array_index (v->shadow_vqs, i); +vhost_svq_stop (svq); + } + ret =vhost_vdpa_call (dev,VHOST_VDPA_SET_STATUS , &status);trace_vhost_vdpa_reset_device (dev, status);return ret;@@ -639,13 +665,28 @@ static int vhost_vdpa_get_vring_base(struct vhost_dev *dev,return ret; } -static int vhost_vdpa_set_vring_kick (struct vhost_dev *dev, -struct vhost_vring_file *file) +static int vhost_vdpa_set_vring_dev_kick (struct vhost_dev *dev, +struct vhost_vring_file *file) {trace_vhost_vdpa_set_vring_kick (dev, file->index, file->fd);return vhost_vdpa_call (dev,VHOST_SET_VRING_KICK , file); } +static int vhost_vdpa_set_vring_kick (struct vhost_dev *dev, +struct vhost_vring_file *file) +{ +struct vhost_vdpa *v = dev->opaque; +int vdpa_idx =vhost_vdpa_get_vq_index (dev, file->index); + +if (v->shadow_vqs_enabled) { +VhostShadowVirtqueue *svq =g_ptr_array_index (v->shadow_vqs, vdpa_idx); +vhost_svq_set_svq_kick_fd (svq, file->fd); +return 0 ; + }else { +return vhost_vdpa_set_vring_dev_kick (dev, file); + } +} +static int vhost_vdpa_set_vring_call (struct vhost_dev *dev,struct vhost_vring_file *file) {@@ -653,6 +694,33 @@ static int vhost_vdpa_set_vring_call(struct vhost_dev *dev,return vhost_vdpa_call (dev,VHOST_SET_VRING_CALL , file); } +/** +* Set shadow virtqueue descriptors to the device +* +* @dev The vhost device model +* @svq The shadow virtqueue +* @idx The index of the virtqueue in the vhost device +*/ +static bool vhost_vdpa_svq_setup (struct vhost_dev *dev, +VhostShadowVirtqueue *svq, +unsigned idx) +{ +struct vhost_vring_file file = { + .index = dev->vq_index + idx, + };Quoted 9 lines
hw/virtio/vhost-vdpa.c static int vhost_vdpa_set_vring_call(struct vhost_dev *dev, · line 714
Quoted 4 lines
hw/virtio/vhost-vdpa.c +4
-0+const EventNotifier *event_notifier; +int r; + + event_notifier =vhost_svq_get_dev_kick_notifier (svq);A question, any reason for making VhostShadowVirtqueue private? If we export it in .h we don't need helper to access its member like
vhost_svq_get_dev_kick_notifier().
Quoted 3 lines
To export it it's always a possibility of course, but that direct access will not be thread safe if we decide to move SVQ to its own iothread for example.
I don't get this, maybe you can give me an example.
Quoted 21 lines
I feel it will be easier to work with it this way but it might be that I'm just used to making as much as possible private. Not like it's needed to use the helpers in the hot paths, only in the setup and teardown.
Quoted 2 lines
Note that
vhost_devis a public structure.Sure we could embed in
vhost_virtqueueif we choose to do it that way, for example.Quoted 10 lines
hw/virtio/vhost-vdpa.c static int vhost_vdpa_set_vring_call(struct vhost_dev *dev, · line 719
Quoted 5 lines
hw/virtio/vhost-vdpa.c +5
-0+ file.fd =event_notifier_get_fd (event_notifier); + r =vhost_vdpa_set_vring_dev_kick (dev, &file); +if (unlikely (r !=0 )) { +error_report ("Can't set device kick fd (%d)" , -r); + }I wonder whether or not we can generalize the logic here and
vhost_vdpa_set_vring_kick(). There's nothing vdpa specific unless thevhost_ops->set_vring_kick().
Quoted 5 lines
If we call
vhost_ops->set_vring_kickwe are settingguest->SVQkick notifier, not SVQ -> vDPA device, because theif(v->shadow_vqs_enabled). All of the modified ops callbacks are hiding the actual device from the vhost subsystem so we need to explicitly use the newly created _dev_ ones.
Ok, I'm fine to start with vhost_vdpa specific code.
Quoted 44 lines
Quoted 26 lines
Quoted 26 lines
hw/virtio/vhost-vdpa.c +11
-0+ +return r ==0 ; +} +static int vhost_vdpa_dev_start (struct vhost_dev *dev,bool started) {struct vhost_vdpa *v = dev->opaque;@@ -660,6 +728,13 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)if (started) {vhost_vdpa_host_notifiers_init (dev); +for (unsigned i =0 ; i < v->shadow_vqs->len; ++i) { +VhostShadowVirtqueue *svq =g_ptr_array_index (v->shadow_vqs, i); +bool ok =vhost_vdpa_svq_setup (dev, svq, i); +if (unlikely (!ok)) { +return -1 ; + } + }vhost_vdpa_set_vring_ready (dev); }else {vhost_vdpa_host_notifiers_uninit (dev, dev->nvqs);@@ -737,6 +812,41 @@ static bool vhost_vdpa_force_iommu(struct vhost_dev *dev)return true; }Quoted 12 lines
hw/virtio/vhost-vdpa.c static bool vhost_vdpa_force_iommu(struct vhost_dev *dev) · line 823
Quoted 9 lines
hw/virtio/vhost-vdpa.c +9
-0+/** +* Adaptor function to free shadow virtqueue through gpointer +* +* @svq The Shadow Virtqueue +*/ +static void vhost_psvq_free (gpointer svq) +{ +vhost_svq_free (svq); +}Any reason for such indirection? Can we simply use
vhost_svq_free()?GCC complains about different types. I think we could do a function type cast and it's valid for every architecture qemu supports, but the indirection seems cleaner to me, and I would be surprised if the compiler does not optimize it away in the cases that the casting are valid.
Quoted 4 lines
../hw/virtio/vhost-vdpa.c:1186:60: error: incompatible function pointer types passing 'void (VhostShadowVirtqueue *)' (aka 'void (struct VhostShadowVirtqueue *)') to parameter of type 'GDestroyNotify' (aka 'void (*)(void *)')
Or just change vhost_svq_free() to take gpointer instead? Then we don't need a cast.
Thanks
Quoted 49 lines
Thanks!
Quoted 46 lines
Thanks
Quoted 43 lines
hw/virtio/vhost-vdpa.c +30
-0+ +static int vhost_vdpa_init_svq (struct vhost_dev *hdev,struct vhost_vdpa *v, +Error **errp) +{ +size_t n_svqs = v->shadow_vqs_enabled ? hdev->nvqs :0 ; +g_autoptr (GPtrArray) shadow_vqs =g_ptr_array_new_full (n_svqs, + vhost_psvq_free); +if (!v->shadow_vqs_enabled) { + goto out; + } + +for (unsigned n =0 ; n < hdev->nvqs; ++n) { +VhostShadowVirtqueue *svq =vhost_svq_new (); + +if (unlikely (!svq)) { +error_setg (errp,"Cannot create svq %u" , n); +return -1 ; + } +g_ptr_array_add (v->shadow_vqs, svq); + } + +out: + v->shadow_vqs =g_steal_pointer (&shadow_vqs); +return 0 ; +} +static int vhost_vdpa_init (struct vhost_dev *dev,void *opaque,Error **errp) {struct vhost_vdpa *v;@@ -759,6 +869,10 @@ static int vhost_vdpa_init(struct vhost_dev *dev, void *opaque, Error **errp) dev->opaque = opaque ; v->listener = vhost_vdpa_memory_listener; v->msg_type =VHOST_IOTLB_MSG_V2 ; + ret =vhost_vdpa_init_svq (dev, v, errp); +if (ret) { + goto err; + }vhost_vdpa_get_iova_range (v);@@ -770,6 +884,10 @@ static int vhost_vdpa_init(struct vhost_dev *dev, void *opaque, Error **errp) VIRTIO_CONFIG_S_DRIVER);
Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost-vdpa.c static int vhost_vdpa_init(struct vhost_dev *dev, void *opaque, Error **errp) · line 893
Quoted 8 lines
hw/virtio/vhost-vdpa.c +4
-0return 0 ; + +err: +ram_block_discard_disable (false); +return ret; }const VhostOps vdpa_ops = {
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Eugenio Pérez
maintainerPlease review this new minimal version. It is way shorter, but this comes with a cost:
* Iteration does not stop at the end of range (but an out of range allocation never happens)
* Iteration must start from iova == 0 instead of first valid entry in the hole.
These should not be a big deal though.
Another possible optimization that comes to my mind is to allocate always at the end of range. In the case of having to allocate and deallocate frequently, this should avoid to iterate over long lived entries. Better justify this with numbers, so I left that out.
Thanks!
Eugenio Pérez
maintainerPATCH 1/1 util: Add iova_tree_alloc
In reply to Jason WangThis iova tree function allows it to look for a hole in allocated regions and return a totally new translation for a given translated address.
It's usage is mainly to allow devices to access qemu address space, remapping guest's one into a new iova space where qemu can add chunks of addresses.
--- include/qemu/iova-tree.h | 17 +++++ util/iova-tree.c | 132 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+)
include/qemu/iova-tree.h +17 -0
@@ -29,6 +29,7 @@#define IOVA_OK (0)#define IOVA_ERR_INVALID (-1)/* Invalid parameters */ #define IOVA_ERR_OVERLAP (-2)/* IOVA range overlapped */ +#define IOVA_ERR_NOMEM (-3)/* Cannot allocate */ typedef struct IOVATree IOVATree ; typedef struct DMAMap {
@@ -119,6 +120,22 @@ const DMAMap *iova_tree_find_address(const IOVATree *tree, hwaddr iova);*/ void iova_tree_foreach (IOVATree *tree,iova_tree_iterator iterator); +/** +* iova_tree_alloc: +* +* @tree: the iova tree to allocate from +* @map: the new map (as translated addr & size) to allocate in iova region +* @iova_begin: the minimum address of the allocation +* @iova_end: the maximum addressable direction of the allocation +* +* Allocates a new region of a given size, between iova_min and iova_max. +* +* Return: Same as iova_tree_insert, but cannot overlap and can be out of +* free contiguous range. Caller can get the assigned iova in map->iova. +*/ +int iova_tree_alloc_map (IOVATree *tree,DMAMap *map,hwaddr iova_begin, +hwaddr iova_end); +/** * iova_tree_destroy: *
util/iova-tree.c +132 -0
144 lines not on this page · raw message
Signature · 1 line
2.27.0
Re: [PATCH] util: Add iova_tree_alloc
Quoted 9 lines · Eugenio Pérez
This iova tree function allows it to look for a hole in allocated regions and return a totally new translation for a given translated address.
It's usage is mainly to allow devices to access qemu address space, remapping guest's one into a new iova space where qemu can add chunks of addresses.
Signed-off-by Eugenio Pérez <[email protected]>
Signature · 1 line
Peter Xu
Eugenio Perez Martin
maintainerRe: [PATCH 28/31] vdpa: Expose VHOST_F_LOG_ALL on SVQ
In reply to Jason WangQuoted 56 lines · Jason Wang
Quoted 53 lines · Eugenio Perez Martin
Quoted 52 lines · Jason Wang
Quoted 36 lines · Eugenio Pérez
SVQ is able to log the dirty bits by itself, so let's use it to not block migration.
Also, ignore set and clear of
VHOST_F_LOG_ALLonset_featuresif SVQ is enabled. Even if the device supports it, the reports would be nonsense because SVQ memory is in the qemu region.The log region is still allocated. Future changes might skip that, but this series is already long enough.
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-vdpa.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)hw/virtio/vhost-vdpa.c +4
-0@@ -1022,6 +1022,9 @@ static int vhost_vdpa_get_features(struct vhost_dev *dev, uint64_t *features)if (ret ==0 && v->shadow_vqs_enabled) {/* Filter only features that SVQ can offer to guest */ vhost_svq_valid_guest_features (features); + +/* Add SVQ logging capabilities */ + *features |=BIT_ULL (VHOST_F_LOG_ALL ); }return ret;@@ -1039,8 +1042,25 @@ static int vhost_vdpa_set_features(struct vhost_dev *dev,if (v->shadow_vqs_enabled) {uint64_t dev_features, svq_features, acked_features; +uint8_t status =0 ;bool ok;hw/virtio/vhost-vdpa.c static int vhost_vdpa_set_features(struct vhost_dev *dev, · line 1057
Quoted 10 lines
hw/virtio/vhost-vdpa.c +10
-0+ ret =vhost_vdpa_call (dev,VHOST_VDPA_GET_STATUS , &status); +if (unlikely (ret)) { +return ret; + } + +if (status &VIRTIO_CONFIG_S_DRIVER_OK ) { +/* +* vhost is trying to enable or disable _F_LOG, and the device +* would report wrong dirty pages. SVQ handles it. +*/ I fail to understand this comment, I'd think there's no way to disable dirty page tracking for SVQ.
Quoted 9 lines
Quoted 3 lines
vhost_log_global_{start,stop} are called at the beginning and end of migration. To inform the device that it should start logging, they set or clean
VHOST_F_LOG_ALLatvhost_dev_set_log.Yes, but for SVQ, we can't disable dirty page tracking, isn't it? The only thing is to ignore or filter out the
F_LOG_ALLand pretend to be enabled and disabled.
Yes, that's what this patch does.
Quoted 10 lines
Quoted 5 lines
While SVQ does not use
VHOST_F_LOG_ALL, it exports the feature bit so vhost does not block migration. Maybe we need to look for another way to do this?I'm fine with filtering since it's much more simpler, but I fail to understand why we need to check
DRIVER_OK.
Ok maybe I can make that part more clear,
Since both operations use vhost_vdpa_set_features we must just filter the one that actually sets or removes VHOST_F_LOG_ALL, without affecting other features.
In practice, that means to not forward the set features after DRIVER_OK. The device is not expecting them anymore.
Does that make more sense?
Thanks!
Quoted 19 lines
Thanks
Quoted 15 lines
Thanks!
Quoted 12 lines
Thanks
Quoted 9 lines
hw/virtio/vhost-vdpa.c +6
-0+return 0 ; + } + +/* We must not ack _F_LOG if SVQ is enabled */ + features &= ~BIT_ULL (VHOST_F_LOG_ALL ); + ret =vhost_vdpa_get_dev_features (dev, &dev_features);if (ret !=0 ) {error_report ("Can't get vdpa device features, got (%d)" , ret);
Re: [PATCH 28/31] vdpa: Expose VHOST_F_LOG_ALL on SVQ
Quoted 81 lines · Eugenio Perez Martin
Quoted 56 lines · Jason Wang
Quoted 53 lines · Eugenio Perez Martin
Quoted 52 lines · Jason Wang
Quoted 36 lines · Eugenio Pérez
SVQ is able to log the dirty bits by itself, so let's use it to not block migration.
Also, ignore set and clear of
VHOST_F_LOG_ALLonset_featuresif SVQ is enabled. Even if the device supports it, the reports would be nonsense because SVQ memory is in the qemu region.The log region is still allocated. Future changes might skip that, but this series is already long enough.
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-vdpa.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)hw/virtio/vhost-vdpa.c +4
-0@@ -1022,6 +1022,9 @@ static int vhost_vdpa_get_features(struct vhost_dev *dev, uint64_t *features)if (ret ==0 && v->shadow_vqs_enabled) {/* Filter only features that SVQ can offer to guest */ vhost_svq_valid_guest_features (features); + +/* Add SVQ logging capabilities */ + *features |=BIT_ULL (VHOST_F_LOG_ALL ); }return ret;@@ -1039,8 +1042,25 @@ static int vhost_vdpa_set_features(struct vhost_dev *dev,if (v->shadow_vqs_enabled) {uint64_t dev_features, svq_features, acked_features; +uint8_t status =0 ;bool ok;hw/virtio/vhost-vdpa.c static int vhost_vdpa_set_features(struct vhost_dev *dev, · line 1057
Quoted 10 lines
hw/virtio/vhost-vdpa.c +10
-0+ ret =vhost_vdpa_call (dev,VHOST_VDPA_GET_STATUS , &status); +if (unlikely (ret)) { +return ret; + } + +if (status &VIRTIO_CONFIG_S_DRIVER_OK ) { +/* +* vhost is trying to enable or disable _F_LOG, and the device +* would report wrong dirty pages. SVQ handles it. +*/ I fail to understand this comment, I'd think there's no way to disable dirty page tracking for SVQ.
Quoted 9 lines
Quoted 3 lines
vhost_log_global_{start,stop} are called at the beginning and end of migration. To inform the device that it should start logging, they set or clean
VHOST_F_LOG_ALLatvhost_dev_set_log.Yes, but for SVQ, we can't disable dirty page tracking, isn't it? The only thing is to ignore or filter out the
F_LOG_ALLand pretend to be enabled and disabled.Yes, that's what this patch does.
Quoted 10 lines
Quoted 5 lines
While SVQ does not use
VHOST_F_LOG_ALL, it exports the feature bit so vhost does not block migration. Maybe we need to look for another way to do this?I'm fine with filtering since it's much more simpler, but I fail to understand why we need to check
DRIVER_OK.
Quoted 8 lines
Ok maybe I can make that part more clear,
Since both operations use
vhost_vdpa_set_featureswe must just filter the one that actually sets or removesVHOST_F_LOG_ALL, without affecting other features.In practice, that means to not forward the set features after
DRIVER_OK. The device is not expecting them anymore.
I wonder what happens if we don't do this.
So kernel had this check:
/*
* It's not allowed to change the features after they have
* been negotiated.
*/
if (ops->get_status(vdpa) & VIRTIO_CONFIG_S_FEATURES_OK)
return -EBUSY;
So is it FEATURES_OK actually?
For this patch, I wonder if the thing we need to do is to see whether it is a enable/disable F_LOG_ALL and simply return.
Thanks
Quoted 25 lines
Does that make more sense?
Thanks!
Quoted 19 lines
Thanks
Quoted 15 lines
Thanks!
Quoted 12 lines
Thanks
Quoted 9 lines
hw/virtio/vhost-vdpa.c +6
-0+return 0 ; + } + +/* We must not ack _F_LOG if SVQ is enabled */ + features &= ~BIT_ULL (VHOST_F_LOG_ALL ); + ret =vhost_vdpa_get_dev_features (dev, &dev_features);if (ret !=0 ) {error_report ("Can't get vdpa device features, got (%d)" , ret);
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Eugenio Perez Martin
maintainerRe: [PATCH 28/31] vdpa: Expose VHOST_F_LOG_ALL on SVQ
Quoted 86 lines · Jason Wang
Quoted 83 lines · Eugenio Perez Martin
Quoted 56 lines · Jason Wang
Quoted 53 lines · Eugenio Perez Martin
Quoted 52 lines · Jason Wang
Quoted 36 lines · Eugenio Pérez
SVQ is able to log the dirty bits by itself, so let's use it to not block migration.
Also, ignore set and clear of
VHOST_F_LOG_ALLonset_featuresif SVQ is enabled. Even if the device supports it, the reports would be nonsense because SVQ memory is in the qemu region.The log region is still allocated. Future changes might skip that, but this series is already long enough.
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-vdpa.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)hw/virtio/vhost-vdpa.c +4
-0@@ -1022,6 +1022,9 @@ static int vhost_vdpa_get_features(struct vhost_dev *dev, uint64_t *features)if (ret ==0 && v->shadow_vqs_enabled) {/* Filter only features that SVQ can offer to guest */ vhost_svq_valid_guest_features (features); + +/* Add SVQ logging capabilities */ + *features |=BIT_ULL (VHOST_F_LOG_ALL ); }return ret;@@ -1039,8 +1042,25 @@ static int vhost_vdpa_set_features(struct vhost_dev *dev,if (v->shadow_vqs_enabled) {uint64_t dev_features, svq_features, acked_features; +uint8_t status =0 ;bool ok;hw/virtio/vhost-vdpa.c static int vhost_vdpa_set_features(struct vhost_dev *dev, · line 1057
Quoted 10 lines
hw/virtio/vhost-vdpa.c +10
-0+ ret =vhost_vdpa_call (dev,VHOST_VDPA_GET_STATUS , &status); +if (unlikely (ret)) { +return ret; + } + +if (status &VIRTIO_CONFIG_S_DRIVER_OK ) { +/* +* vhost is trying to enable or disable _F_LOG, and the device +* would report wrong dirty pages. SVQ handles it. +*/ I fail to understand this comment, I'd think there's no way to disable dirty page tracking for SVQ.
Quoted 9 lines
Quoted 3 lines
vhost_log_global_{start,stop} are called at the beginning and end of migration. To inform the device that it should start logging, they set or clean
VHOST_F_LOG_ALLatvhost_dev_set_log.Yes, but for SVQ, we can't disable dirty page tracking, isn't it? The only thing is to ignore or filter out the
F_LOG_ALLand pretend to be enabled and disabled.Yes, that's what this patch does.
Quoted 10 lines
Quoted 5 lines
While SVQ does not use
VHOST_F_LOG_ALL, it exports the feature bit so vhost does not block migration. Maybe we need to look for another way to do this?I'm fine with filtering since it's much more simpler, but I fail to understand why we need to check
DRIVER_OK.Ok maybe I can make that part more clear,
Quoted 9 lines
Quoted 6 lines
Since both operations use
vhost_vdpa_set_featureswe must just filter the one that actually sets or removesVHOST_F_LOG_ALL, without affecting other features.In practice, that means to not forward the set features after
DRIVER_OK. The device is not expecting them anymore.I wonder what happens if we don't do this.
If we simply delete the check vhost_dev_set_features will return an error, failing the start of the migration. More on this below.
Quoted 11 lines
So kernel had this check:
/*
* It's not allowed to change the features after they have
* been negotiated.
*/
if (ops->get_status(vdpa)&VIRTIO_CONFIG_S_FEATURES_OK)return -EBUSY;So is it
FEATURES_OKactually?
Yes, FEATURES_OK seems more appropriate actually so I will switch to it for the next version.
But it should be functionally equivalent, since vhost.c:vhost_dev_start sets both and the setting of _F_LOG_ALL cannot be concurrent with it.
Quoted 3 lines
For this patch, I wonder if the thing we need to do is to see whether it is a enable/disable
F_LOG_ALLand simply return.
Yes, that's the intention of the patch.
We have 4 cases here:
a) We're being called from vhost_dev_start, with enable_log = false
b) We're being called from vhost_dev_start, with enable_log = true
c) We're being called from vhost_dev_set_log, with enable_log = false
d) We're being called from vhost_dev_set_log, with enable_log = true
The way to tell the difference between a/b and c/d is to check if {FEATURES,DRIVER}_OK is set. And, as you point out in previous mails, F_LOG_ALL must be filtered unconditionally since SVQ tracks dirty memory through the memory unmapping, so we clear the bit unconditionally if we detect that VHOST_SET_FEATURES will be called (cases a and b).
Another possibility is to track if features have been set with a bool in vhost_vdpa or something like that. But it seems cleaner to me to only store that in the actual device.
Quoted 28 lines
Thanks
Quoted 25 lines
Does that make more sense?
Thanks!
Quoted 19 lines
Thanks
Quoted 15 lines
Thanks!
Quoted 12 lines
Thanks
Quoted 9 lines
hw/virtio/vhost-vdpa.c +6
-0+return 0 ; + } + +/* We must not ack _F_LOG if SVQ is enabled */ + features &= ~BIT_ULL (VHOST_F_LOG_ALL ); + ret =vhost_vdpa_get_dev_features (dev, &dev_features);if (ret !=0 ) {error_report ("Can't get vdpa device features, got (%d)" , ret);
Eugenio Perez Martin
maintainerRe: [PATCH 18/31] vhost: Shadow virtqueue buffers forwarding
In reply to Jason WangQuoted 34 lines · Jason Wang
Quoted 21 lines · Eugenio Perez Martin
Quoted 17 lines · Jason Wang
hw/virtio/vhost-shadow-virtqueue.c void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd) · line 600
Quoted 12 lines · Eugenio Pérez
hw/virtio/vhost-shadow-virtqueue.c +8
-0@@ -272,6 +590,28 @@ void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd)void vhost_svq_stop (VhostShadowVirtqueue *svq) {event_notifier_set_handler (&svq->svq_kick,NULL ); + g_autofree VirtQueueElement *next_avail_elem = NULL; + +if (!svq->vq) { +return ; + } + +/* Send all pending used descriptors to guest */ +vhost_svq_flush (svq, false);Do we need to wait for all the pending descriptors to be completed here?
No, this function does not wait, it only completes the forwarding of the *used* descriptors.
Quoted 8 lines
The best example is the net rx queue in my opinion. This call will check SVQ's vring
used_idxand will forward the last used descriptors if any, but all available descriptors will remain as available for qemu's VQ code.To skip it would miss those last rx descriptors in migration.
Thanks!
Quoted 7 lines
So it's probably to not the best place to ask. It's more about the inflight descriptors so it should be TX instead of RX.
I can imagine the migration last phase, we should stop the vhost-vDPA before calling
vhost_svq_stop(). Then we should be fine regardless of inflight descriptors.
I think I'm still missing something here.
To be on the same page. Regarding tx this could cause repeated tx frames (one at source and other at destination), but never a missed buffer not transmitted. The "stop before" could be interpreted as "SVQ is not forwarding available buffers anymore". Would that work?
Thanks!
Quoted 23 lines
Thanks
Quoted 19 lines
Quoted 19 lines
Thanks
Quoted 15 lines
hw/virtio/vhost-shadow-virtqueue.c +14
-0+ +for (unsigned i =0 ; i < svq->vring.num; ++i) { + g_autofree VirtQueueElement *elem = NULL; + elem =g_steal_pointer (&svq->ring_id_maps[i]); +if (elem) { +virtqueue_detach_element (svq->vq, elem, elem->len); + } + } + + next_avail_elem =g_steal_pointer (&svq->next_guest_avail_elem); +if (next_avail_elem) { +virtqueue_detach_element (svq->vq, next_avail_elem, + next_avail_elem->len); + } }
Eugenio Perez Martin
maintainerRe: [PATCH 17/31] vdpa: adapt vhost_ops callbacks to svq
In reply to Jason WangQuoted 70 lines · Jason Wang
Quoted 60 lines · Eugenio Perez Martin
Quoted 59 lines · Jason Wang
Quoted 49 lines · Eugenio Pérez
First half of the buffers forwarding part, preparing vhost-vdpa callbacks to SVQ to offer it. QEMU cannot enable it at this moment, so this is effectively dead code at the moment, but it helps to reduce patch size.
Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-shadow-virtqueue.h | 2 +- hw/virtio/vhost-shadow-virtqueue.c | 21 ++++- hw/virtio/vhost-vdpa.c | 133 ++++++++++++++++++++++++++--- 3 files changed, 143 insertions(+), 13 deletions(-)hw/virtio/vhost-shadow-virtqueue.h +1
-1@@ -35,7 +35,7 @@ size_t vhost_svq_device_area_size(const VhostShadowVirtqueue *svq);void vhost_svq_stop (VhostShadowVirtqueue *svq); -VhostShadowVirtqueue *vhost_svq_new (void ); +VhostShadowVirtqueue *vhost_svq_new (uint16_t qsize);void vhost_svq_free (VhostShadowVirtqueue *vq);hw/virtio/vhost-shadow-virtqueue.c +9
-1@@ -277,9 +277,17 @@ void vhost_svq_stop(VhostShadowVirtqueue *svq)/** * Creates vhost shadow virtqueue, and instruct vhost device to use the shadow * methods and file descriptors. +* +* @qsize Shadow VirtQueue size +* +* Returns the new virtqueue or NULL. +* +* In case of error, reason is reported through error_report. */ -VhostShadowVirtqueue *vhost_svq_new (void ) +VhostShadowVirtqueue *vhost_svq_new (uint16_t qsize) { +size_t desc_size =sizeof (vring_desc_t) * qsize; +size_t device_size, driver_size; g_autofree VhostShadowVirtqueue *svq = g_new0(VhostShadowVirtqueue, 1);int r;hw/virtio/vhost-shadow-virtqueue.c VhostShadowVirtqueue *vhost_svq_new(void) · line 311
Quoted 4 lines
hw/virtio/vhost-shadow-virtqueue.c +1
-0@@ -300,6 +308,15 @@ VhostShadowVirtqueue *vhost_svq_new(void)/* Placeholder descriptor, it should be deleted at set_kick_fd */ event_notifier_init_fd (&svq->svq_kick,INVALID_SVQ_KICK_FD ); + svq->vring.num = qsize;I wonder if this is the best. E.g some hardware can support up to 32K queue size. So this will probably end up with:
Quoted 5 lines
Quoted 3 lines
1) SVQ use 32K queue size
2) hardware queue uses 256In that case SVQ vring queue size will be 32K and guest's vring can negotiate any number with SVQ equal or less than 32K,
Quoted 9 lines
Sorry for being unclear what I meant is actually
1) SVQ uses 32K queue size
2) guest vq uses 256
This looks like a burden that needs extra logic and may damage the performance.
Still not getting this point.
An available guest buffer, although contiguous in GPA/GVA, can expand in multiple buffers if it's not contiguous in qemu's VA (by the while loop in virtqueue_map_desc [1]). In that scenario it is better to have "plenty" of SVQ buffers.
I'm ok if we decide to put an upper limit though, or if we decide not to handle this situation. But we would leave out valid virtio drivers. Maybe to set a fixed upper limit (1024?)? To add another parameter (x-svq-size-n=N)?
If you mean we lose performance because memory gets more sparse I think the only possibility is to limit that way.
Quoted 8 lines
And this can lead other interesting situation:
1) SVQ uses 256
2) guest vq uses 1024
Where a lot of more SVQ logic is needed.
If we agree that a guest descriptor can expand in multiple SVQ descriptors, this should be already handled by the previous logic too.
But this should only happen in case that qemu is launched with a "bad" cmdline, isn't it?
If I run that example with vp_vdpa, L0 qemu will happily accept 1024 as a queue size [2]. But if the vdpa device maximum queue size is effectively 256, this will result in an error: We're not exposing it to the guest at any moment but with qemu's cmdline.
Quoted 5 lines
Quoted 3 lines
including 256.
Is that what you mean?
Quoted 7 lines
I mean, it looks to me the logic will be much more simplified if we just allocate the shadow virtqueue with the size what guest can see (guest vring).
Then we don't need to think if the difference of the queue size can have any side effects.
I think that we cannot avoid that extra logic unless we force GPA to be contiguous in IOVA. If we are sure the guest's buffers cannot be at more than one descriptor in SVQ, then yes, we can simplify things. If not, I think we are forced to carry all of it.
But if we prove it I'm not opposed to simplifying things and making head at SVQ == head at guest.
Thanks!
[1] https://gitlab.com/qemu-project/qemu/-/blob/17e31340/hw/virtio/virtio.c#L1297
[2] But that's not the whole story: I've been running limited in tx descriptors because of virtio_net_max_tx_queue_size, which predates vdpa. I'll send a patch to also un-limit it.
Quoted 31 lines
Quoted 18 lines
If with hardware queues you mean guest's vring, not sure why it is "probably 256". I'd say that in that case with the virtio-net kernel driver the ring size will be the same as the device export, for example, isn't it?
The implementation should support any combination of sizes, but the ring size exposed to the guest is never bigger than hardware one.
Quoted 3 lines
? Or we SVQ can stick to 256 but this will this cause trouble if we want to add event index support?
I think we should not have any problem with event idx. If you mean that the guest could mark more buffers available than SVQ vring's size, that should not happen because there must be less entries in the guest than SVQ.
Quoted 7 lines
But if I understood you correctly, a similar situation could happen if a guest's contiguous buffer is scattered across many qemu's VA chunks. Even if that would happen, the situation should be ok too: SVQ knows the guest's avail idx and, if SVQ is full, it will continue forwarding avail buffers when the device uses more buffers.
Does that make sense to you?
Yes.
Thanks