vDPA shadow virtqueue iova tree fixes.
v2 cover vDPA shadow virtqueue iova tree fixes.
Collection of iova tree fixes detected preparing live migration with real devices and multiqueue.
These cannot be triggered in simple setups (vdpa_sim_net, no display, no device reset with different features) but it's possible to trigger them with real devices or if the kernel fails some step like memory mapping / unmapping.
First two patches are already in the list at [1]. Last one is not a fix by itself but a straightforward merge of the same code.
[1] https://lists.nongnu.org/archive/html/qemu-devel/2022-08/msg00773.html
v2:
* Accept iova_tree_remove map arg by value
* Add error message on unmap fail
Eugenio Pérez (7):
vdpa: Skip the maps not in the iova tree
vdpa: do not save failed dma maps in SVQ iova tree
util: accept iova_tree_remove_parameter by value
vdpa: Remove SVQ vring from iova_tree at shutdown
vdpa: Make SVQ vring unmapping return void
vhost: Always store new kick fd on vhost_svq_set_svq_kick_fd
vdpa: Use ring hwaddr at vhost_vdpa_svq_unmap_ring
hw/virtio/vhost-iova-tree.h | 2 +- include/qemu/iova-tree.h | 2 +- hw/i386/intel_iommu.c | 6 +-- hw/virtio/vhost-iova-tree.c | 2 +- hw/virtio/vhost-shadow-virtqueue.c | 4 +- hw/virtio/vhost-vdpa.c | 75 ++++++++++++++++-------------- net/vhost-vdpa.c | 4 +- util/iova-tree.c | 4 +- 8 files changed, 51 insertions(+), 48 deletions(-)
Signature · 1 line
2.31.1
PATCH v2 1/7 vdpa: Skip the maps not in the iova tree
Next patch will skip the registering of dma maps that the vdpa device rejects in the iova tree. We need to consider that here or we cause a SIGSEGV accessing result.
--- hw/virtio/vhost-vdpa.c | 4 ++++ 1 file changed, 4 insertions(+)
hw/virtio/vhost-vdpa.c +4 -0
@@ -289,6 +289,10 @@ static void vhost_vdpa_listener_region_del(MemoryListener *listener, }; result =vhost_iova_tree_find_iova (v->iova_tree, &mem_region); +if (!result) { +/* The memory listener map wasn't mapped */ +return ; + } iova = result->iova;vhost_iova_tree_remove (v->iova_tree, result); }
Signature · 1 line
2.31.1
PATCH v2 2/7 vdpa: do not save failed dma maps in SVQ iova tree
In reply to Eugenio PérezIf a map fails for whatever reason, it must not be saved in the tree. Otherwise, qemu will try to unmap it in cleanup, leaving to more errors.
--- hw/virtio/vhost-vdpa.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-)
hw/virtio/vhost-vdpa.c +13 -7
@@ -176,6 +176,7 @@ static void vhost_vdpa_listener_commit(MemoryListener *listener)static void vhost_vdpa_listener_region_add (MemoryListener *listener,MemoryRegionSection *section) { +DMAMap mem_region = {}; struct vhost_vdpa *v = container_of(listener, struct vhost_vdpa, listener);hwaddr iova;Int128 llend, llsize;
@@ -212,13 +213,13 @@ static void vhost_vdpa_listener_region_add(MemoryListener *listener, llsize =int128_sub (llend,int128_make64 (iova));if (v->shadow_vqs_enabled) { -DMAMap mem_region = { - .translated_addr = (hwaddr )(uintptr_t )vaddr, - .size =int128_get64 (llsize) -1 , - .perm =IOMMU_ACCESS_FLAG (true, section->readonly), - }; +int r; -int r =vhost_iova_tree_map_alloc (v->iova_tree, &mem_region); + mem_region.translated_addr = (hwaddr )(uintptr_t )vaddr, + mem_region.size =int128_get64 (llsize) -1 , + mem_region.perm =IOMMU_ACCESS_FLAG (true, section->readonly), + + r =vhost_iova_tree_map_alloc (v->iova_tree, &mem_region);if (unlikely (r !=IOVA_OK )) {error_report ("Can't allocate a mapping (%d)" , r); goto fail;
@@ -232,11 +233,16 @@ static void vhost_vdpa_listener_region_add(MemoryListener *listener, vaddr, section->readonly);if (ret) {error_report ("vhost vdpa map fail!" ); - goto fail; + goto fail_map; }return ; +fail_map: +if (v->shadow_vqs_enabled) { +vhost_iova_tree_remove (v->iova_tree, &mem_region); + } + fail:/* * On the initfn path, store the first error in the container so we
Signature · 1 line
2.31.1
PATCH v2 3/7 util: accept iova_tree_remove_parameter by value
In reply to Eugenio PérezIt's convenient to call iova_tree_remove from a map returned from iova_tree_find or iova_tree_find_iova. With the current code this is not possible, since we will free it, and then we will try to search for it again.
Fix it making accepting the map by value, forcing a copy of the argument. Not applying a fixes tag, since there is no use like that at the moment.
---
v2: Accept map parameter by value instead of make a copy
--- hw/virtio/vhost-iova-tree.h | 2 +- include/qemu/iova-tree.h | 2 +- hw/i386/intel_iommu.c | 6 +++--- hw/virtio/vhost-iova-tree.c | 2 +- hw/virtio/vhost-vdpa.c | 6 +++--- net/vhost-vdpa.c | 4 ++-- util/iova-tree.c | 4 ++-- 7 files changed, 13 insertions(+), 13 deletions(-)
hw/virtio/vhost-iova-tree.h +1 -1
@@ -22,6 +22,6 @@ 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); +void vhost_iova_tree_remove (VhostIOVATree *iova_tree,DMAMap map);#endif
include/qemu/iova-tree.h +1 -1
@@ -73,7 +73,7 @@ int iova_tree_insert(IOVATree *tree, const DMAMap *map);* all the mappings that are included in the provided range will be * removed from the tree. Here map->translated_addr is meaningless. */ -void iova_tree_remove (IOVATree *tree,const DMAMap *map); +void iova_tree_remove (IOVATree *tree,DMAMap map);/** * iova_tree_find:
hw/i386/intel_iommu.c +3 -3
@@ -1187,7 +1187,7 @@ static int vtd_page_walk_one(IOMMUTLBEvent *event, vtd_page_walk_info *info)return ret; }/* Drop any existing mapping */ -iova_tree_remove (as->iova_tree, &target); +iova_tree_remove (as->iova_tree, target);/* Recover the correct type */ event->type =IOMMU_NOTIFIER_MAP ; entry->perm = cache_perm;
@@ -1200,7 +1200,7 @@ static int vtd_page_walk_one(IOMMUTLBEvent *event, vtd_page_walk_info *info)trace_vtd_page_walk_one_skip_unmap (entry->iova, entry->addr_mask);return 0 ; } -iova_tree_remove (as->iova_tree, &target); +iova_tree_remove (as->iova_tree, target); } trace_vtd_page_walk_one(info->domain_id, entry->iova,
@@ -3563,7 +3563,7 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) map.iova = n->start; map.size = size; -iova_tree_remove (as->iova_tree, &map); +iova_tree_remove (as->iova_tree, map); }static void vtd_address_space_unmap_all (IntelIOMMUState *s)
hw/virtio/vhost-iova-tree.c +1 -1
@@ -104,7 +104,7 @@ int vhost_iova_tree_map_alloc(VhostIOVATree *tree, DMAMap *map)* @iova_tree: The vhost iova tree * @map: The map to remove */ -void vhost_iova_tree_remove (VhostIOVATree *iova_tree,const DMAMap *map) +void vhost_iova_tree_remove (VhostIOVATree *iova_tree,DMAMap map) {iova_tree_remove (iova_tree->iova_taddr_map, map); }
hw/virtio/vhost-vdpa.c +3 -3
@@ -240,7 +240,7 @@ static void vhost_vdpa_listener_region_add(MemoryListener *listener, fail_map:if (v->shadow_vqs_enabled) { -vhost_iova_tree_remove (v->iova_tree, &mem_region); +vhost_iova_tree_remove (v->iova_tree, mem_region); } fail:
@@ -300,7 +300,7 @@ static void vhost_vdpa_listener_region_del(MemoryListener *listener,return ; } iova = result->iova; -vhost_iova_tree_remove (v->iova_tree, result); +vhost_iova_tree_remove (v->iova_tree, *result); }vhost_vdpa_iotlb_batch_begin_once (v); ret =vhost_vdpa_dma_unmap (v, iova,int128_get64 (llsize));
@@ -944,7 +944,7 @@ static bool vhost_vdpa_svq_map_ring(struct vhost_vdpa *v, DMAMap *needle, needle->perm == IOMMU_RO);if (unlikely (r !=0 )) {error_setg_errno (errp, -r,"Cannot map region to device" ); -vhost_iova_tree_remove (v->iova_tree, needle); +vhost_iova_tree_remove (v->iova_tree, *needle); }return r ==0 ;
net/vhost-vdpa.c +2 -2
@@ -244,7 +244,7 @@ static void vhost_vdpa_cvq_unmap_buf(struct vhost_vdpa *v, void *addr)error_report ("Device cannot unmap: %s(%d)" ,g_strerror (r), r); } -vhost_iova_tree_remove (tree, map); +vhost_iova_tree_remove (tree, *map); }static size_t vhost_vdpa_net_cvq_cmd_len (void )
@@ -297,7 +297,7 @@ static bool vhost_vdpa_cvq_map_buf(struct vhost_vdpa *v,return true; dma_map_err: -vhost_iova_tree_remove (v->iova_tree, &map); +vhost_iova_tree_remove (v->iova_tree, map);return false; }
util/iova-tree.c +2 -2
@@ -164,11 +164,11 @@ void iova_tree_foreach(IOVATree *tree, iova_tree_iterator iterator)g_tree_foreach (tree->tree, iova_tree_traverse, iterator); } -void iova_tree_remove (IOVATree *tree,const DMAMap *map) +void iova_tree_remove (IOVATree *tree,DMAMap map) {const DMAMap *overlap; -while ((overlap =iova_tree_find (tree, map))) { +while ((overlap =iova_tree_find (tree, &map))) {g_tree_remove (tree->tree, overlap); } }
Signature · 1 line
2.31.1
PATCH v2 4/7 vdpa: Remove SVQ vring from iova_tree at shutdown
In reply to Eugenio PérezAlthough the device will be reset before usage, the right thing to do is to clean it.
---
v2:
* Call vhost_iova_tree_remove with the map as value.
* report_error on vhost_vdpa_dma_unmap fail
--- hw/virtio/vhost-vdpa.c | 6 ++++++ 1 file changed, 6 insertions(+)
hw/virtio/vhost-vdpa.c +6 -0
@@ -898,6 +898,12 @@ static bool vhost_vdpa_svq_unmap_ring(struct vhost_vdpa *v, size =ROUND_UP (result->size,qemu_real_host_page_size ()); r =vhost_vdpa_dma_unmap (v, result->iova, size); +if (unlikely (r <0 )) { +error_report ("Unable to unmap SVQ vring: %s (%d)" ,g_strerror (-r), -r); +return false; + } + +vhost_iova_tree_remove (v->iova_tree, *result);return r ==0 ; }
Signature · 1 line
2.31.1
PATCH v2 5/7 vdpa: Make SVQ vring unmapping return void
In reply to Eugenio PérezNothing actually reads the return value, but an error in cleaning some entries could cause device stop to abort, making a restart impossible. Better ignore explicitely the return value.
--- hw/virtio/vhost-vdpa.c | 32 ++++++++++---------------------- 1 file changed, 10 insertions(+), 22 deletions(-)
hw/virtio/vhost-vdpa.c +10 -22
@@ -884,7 +884,7 @@ static int vhost_vdpa_svq_set_fds(struct vhost_dev *dev,/** * Unmap a SVQ area in the device */ -static bool vhost_vdpa_svq_unmap_ring (struct vhost_vdpa *v, +static void vhost_vdpa_svq_unmap_ring (struct vhost_vdpa *v,const DMAMap *needle) {const DMAMap *result =vhost_iova_tree_find_iova (v->iova_tree, needle);
@@ -893,38 +893,33 @@ static bool vhost_vdpa_svq_unmap_ring(struct vhost_vdpa *v,if (unlikely (!result)) {error_report ("Unable to find SVQ address to unmap" ); -return false; +return ; } size =ROUND_UP (result->size,qemu_real_host_page_size ()); r =vhost_vdpa_dma_unmap (v, result->iova, size);if (unlikely (r <0 )) {error_report ("Unable to unmap SVQ vring: %s (%d)" ,g_strerror (-r), -r); -return false; +return ; }vhost_iova_tree_remove (v->iova_tree, *result); -return r ==0 ; } -static bool vhost_vdpa_svq_unmap_rings (struct vhost_dev *dev, +static void 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; -bool ok;vhost_svq_get_vring_addr (svq, &svq_addr); needle.translated_addr = svq_addr.desc_user_addr; - ok =vhost_vdpa_svq_unmap_ring (v, &needle); -if (unlikely (!ok)) { -return false; - } +vhost_vdpa_svq_unmap_ring (v, &needle); needle.translated_addr = svq_addr.used_user_addr; -return vhost_vdpa_svq_unmap_ring (v, &needle); +vhost_vdpa_svq_unmap_ring (v, &needle); }/**
@@ -1095,26 +1090,22 @@ err:return false; } -static bool vhost_vdpa_svqs_stop (struct vhost_dev *dev) +static void vhost_vdpa_svqs_stop (struct vhost_dev *dev) {struct vhost_vdpa *v = dev->opaque;if (!v->shadow_vqs) { -return true; +return ; }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 false; - } +vhost_vdpa_svq_unmap_rings (dev, svq); }if (v->migration_blocker) {migrate_del_blocker (v->migration_blocker); } -return true; }static int vhost_vdpa_dev_start (struct vhost_dev *dev,bool started)
@@ -1131,10 +1122,7 @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started) }vhost_vdpa_set_vring_ready (dev); }else { - ok =vhost_vdpa_svqs_stop (dev); -if (unlikely (!ok)) { -return -1 ; - } +vhost_vdpa_svqs_stop (dev);vhost_vdpa_host_notifiers_uninit (dev, dev->nvqs); }
Signature · 1 line
2.31.1
PATCH v2 6/7 vhost: Always store new kick fd on vhost_svq_set_svq_kick_fd
In reply to Eugenio PérezWe can unbind twice a file descriptor if we call twice vhost_svq_set_svq_kick_fd because of this. Since it comes from vhost and not from SVQ, that file descriptor could be a different thing that guest's vhost notifier.
Likewise, it can happens the same if a guest start and stop the device multiple times.
--- hw/virtio/vhost-shadow-virtqueue.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
hw/virtio/vhost-shadow-virtqueue.c +2 -2
@@ -602,13 +602,13 @@ void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd)event_notifier_set_handler (svq_kick,NULL ); } +event_notifier_init_fd (svq_kick, svq_kick_fd);/* * event_notifier_set_handler already checks for guest's notifications if * they arrive at the new file descriptor in the switch, so there is no * need to explicitly check for them. */ if (poll_start) { -event_notifier_init_fd (svq_kick, svq_kick_fd);event_notifier_set (svq_kick);event_notifier_set_handler (svq_kick, vhost_handle_guest_kick_notifier); }
@@ -655,7 +655,7 @@ void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIODevice *vdev,*/ void vhost_svq_stop (VhostShadowVirtqueue *svq) { -event_notifier_set_handler (&svq->svq_kick,NULL ); +vhost_svq_set_svq_kick_fd (svq,VHOST_FILE_UNBIND ); g_autofree VirtQueueElement *next_avail_elem = NULL;if (!svq->vq) {
Signature · 1 line
2.31.1
PATCH v2 7/7 vdpa: Use ring hwaddr at vhost_vdpa_svq_unmap_ring
In reply to Eugenio PérezReduce code duplication.
--- hw/virtio/vhost-vdpa.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-)
hw/virtio/vhost-vdpa.c +8 -9
@@ -884,10 +884,12 @@ static int vhost_vdpa_svq_set_fds(struct vhost_dev *dev,/** * Unmap a SVQ area in the device */ -static void vhost_vdpa_svq_unmap_ring (struct vhost_vdpa *v, -const DMAMap *needle) +static void vhost_vdpa_svq_unmap_ring (struct vhost_vdpa *v,hwaddr addr) { -const DMAMap *result =vhost_iova_tree_find_iova (v->iova_tree, needle); +const DMAMap needle = { + .translated_addr = addr, + }; +const DMAMap *result =vhost_iova_tree_find_iova (v->iova_tree, &needle);hwaddr size;int r;
@@ -909,17 +911,14 @@ static void vhost_vdpa_svq_unmap_ring(struct vhost_vdpa *v,static void 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;vhost_svq_get_vring_addr (svq, &svq_addr); - needle.translated_addr = svq_addr.desc_user_addr; -vhost_vdpa_svq_unmap_ring (v, &needle); +vhost_vdpa_svq_unmap_ring (v, svq_addr.desc_user_addr); - needle.translated_addr = svq_addr.used_user_addr; -vhost_vdpa_svq_unmap_ring (v, &needle); +vhost_vdpa_svq_unmap_ring (v, svq_addr.used_user_addr); }/**
@@ -997,7 +996,7 @@ static bool vhost_vdpa_svq_map_rings(struct vhost_dev *dev, ok =vhost_vdpa_svq_map_ring (v, &device_region, errp);if (unlikely (!ok)) {error_prepend (errp,"Cannot create vq device region: " ); -vhost_vdpa_svq_unmap_ring (v, &driver_region); +vhost_vdpa_svq_unmap_ring (v, driver_region.translated_addr); } addr->used_user_addr = device_region.iova;
Signature · 1 line
2.31.1