vDPA shadow virtqueue iova tree fixes.
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
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: make a copy of iova_tree_remove_parameter
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-shadow-virtqueue.c | 4 +- hw/virtio/vhost-vdpa.c | 70 +++++++++++++++--------------- util/iova-tree.c | 4 +- 3 files changed, 41 insertions(+), 37 deletions(-)
Signature · 1 line
2.31.1
PATCH 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 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 3/7 util: make a copy of iova_tree_remove_parameter
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 a copy of the argument. Not applying a fixes tag, since there is no use like that at the moment.
--- util/iova-tree.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
util/iova-tree.c +3 -1
@@ -166,9 +166,11 @@ void iova_tree_foreach(IOVATree *tree, iova_tree_iterator iterator)void iova_tree_remove (IOVATree *tree,const DMAMap *map) { +/* Just in case caller is calling iova_tree_remove from a result of find */ +const DMAMap needle = *map;const DMAMap *overlap; -while ((overlap =iova_tree_find (tree, map))) { +while ((overlap =iova_tree_find (tree, &needle))) {g_tree_remove (tree->tree, overlap); } }
Signature · 1 line
2.31.1
Re: [PATCH 3/7] util: make a copy of iova_tree_remove_parameter
Quoted 2 lines · Eugenio Pérez
It's convenient to call
iova_tree_removefrom a map returned fromiova_tree_findoriova_tree_find_iova.
The looks like a hint of the defect of current API.
Quoted 17 lines
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 a copy of the argument. Not applying a fixes tag, since there is no use like that at the moment.
Signed-off-by Eugenio Pérez <[email protected]>--- util/iova-tree.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)util/iova-tree.c +0
-0
util/iova-tree.c void iova_tree_foreach(IOVATree *tree, iova_tree_iterator iterator) · line 170
Quoted 5 lines
util/iova-tree.c +2
-0@@ -166,9 +166,11 @@ void iova_tree_foreach(IOVATree *tree, iova_tree_iterator iterator)void iova_tree_remove (IOVATree *tree,const DMAMap *map) { +/* Just in case caller is calling iova_tree_remove from a result of find */ +const DMAMap needle = *map;
Then let's simply make iova_tree_remove() accept const DMAMap instead of the pointer to it.
util/iova-tree.c void iova_tree_foreach(IOVATree *tree, iova_tree_iterator iterator) · line 175
Quoted 6 lines
util/iova-tree.c +1
-1const DMAMap *overlap; -while ((overlap =iova_tree_find (tree, map))) { +while ((overlap =iova_tree_find (tree, &needle))) {g_tree_remove (tree->tree, overlap); }
So we had following in iova_tree_insert():
/* We don't allow to insert range that overlaps with existings */ if (iova_tree_find(tree, map)) {
return IOVA_ERR_OVERLAP;
}
I wonder how overlap can happen?
Thanks
Quoted 1 line
util/iova-tree.c +0
-0}
Re: [PATCH 3/7] util: make a copy of iova_tree_remove_parameter
Quoted 28 lines · Jason Wang
Quoted 2 lines · Eugenio Pérez
It's convenient to call
iova_tree_removefrom a map returned fromiova_tree_findoriova_tree_find_iova.The looks like a hint of the defect of current API.
Quoted 18 lines
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 a copy of the argument. Not applying a fixes tag, since there is no use like that at the moment.
Signed-off-by Eugenio Pérez <[email protected]>--- util/iova-tree.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)util/iova-tree.c +0
-0@@ -166,9 +166,11 @@ void iova_tree_foreach(IOVATree *tree, iova_tree_iterator iterator)
Quoted 9 lines
util/iova-tree.c void iova_tree_foreach(IOVATree *tree, iova_tree_iterator iterator) · line 170
Quoted 4 lines
util/iova-tree.c +2
-0void iova_tree_remove (IOVATree *tree,const DMAMap *map) { +/* Just in case caller is calling iova_tree_remove from a result of find */ +const DMAMap needle = *map;Then let's simply make
iova_tree_remove()accept const DMAMap instead of the pointer to it.
Do you mean to accept DMAMap by value, isn't it? (no need for const it then, isn't it?).
Quoted 9 lines
util/iova-tree.c void iova_tree_foreach(IOVATree *tree, iova_tree_iterator iterator) · line 175
Quoted 7 lines
util/iova-tree.c +1
-1const DMAMap *overlap; -while ((overlap =iova_tree_find (tree, map))) { +while ((overlap =iova_tree_find (tree, &needle))) {g_tree_remove (tree->tree, overlap); }
Quoted 9 lines
So we had following in
iova_tree_insert():/* We don't allow to insert range that overlaps with existings */ if (iova_tree_find(tree, map)) { return IOVA_ERR_OVERLAP; }I wonder how overlap can happen?
I don't get this part. The problem is that iova_tree_find returns a pointer to an internal structure, there is no need to insert multiple times overlapping maps.
Thanks!
PATCH 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.
--- hw/virtio/vhost-vdpa.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
hw/virtio/vhost-vdpa.c +6 -1
@@ -898,7 +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); -return r ==0 ; +if (unlikely (r <0 )) { +return false; + } + +vhost_iova_tree_remove (v->iova_tree, result); +return 0 ; } static bool vhost_vdpa_svq_unmap_rings(struct vhost_dev *dev,
Signature · 1 line
2.31.1
Re: [PATCH 4/7] vdpa: Remove SVQ vring from iova_tree at shutdown
Quoted 15 lines · Eugenio Pérez
Although the device will be reset before usage, the right thing to do is to clean it.
Reported-by Lei Yang <[email protected]>Fixes 34e3c94eda ("vdpa: Add custom IOTLB translations to SVQ")Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-vdpa.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)hw/virtio/vhost-vdpa.c +0
-0
hw/virtio/vhost-vdpa.c static bool vhost_vdpa_svq_unmap_ring(struct vhost_vdpa *v, · line 902
Quoted 6 lines
hw/virtio/vhost-vdpa.c +2
-1@@ -898,7 +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); -return r ==0 ; +if (unlikely (r <0 )) { +return false;
vhost-vdpa_svq_map_ring() will call error_report() here, should we do the same?
if (unlikely(r != 0)) {
error_setg_errno(errp, -r, "Cannot map region to device");
(Btw the error is not very informative, we should dump the map it self at least?)
Thanks
Quoted 7 lines
hw/virtio/vhost-vdpa.c +4
-0+ } + +vhost_iova_tree_remove (v->iova_tree, result); +return 0 ; } static bool vhost_vdpa_svq_unmap_rings(struct vhost_dev *dev,
Re: [PATCH 4/7] vdpa: Remove SVQ vring from iova_tree at shutdown
Quoted 19 lines · Jason Wang
Quoted 16 lines · Eugenio Pérez
Although the device will be reset before usage, the right thing to do is to clean it.
Reported-by Lei Yang <[email protected]>Fixes 34e3c94eda ("vdpa: Add custom IOTLB translations to SVQ")Signed-off-by Eugenio Pérez <[email protected]>--- hw/virtio/vhost-vdpa.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)hw/virtio/vhost-vdpa.c +0
-0@@ -898,7 +898,12 @@ static bool vhost_vdpa_svq_unmap_ring(struct vhost_vdpa *v,
Quoted 10 lines
hw/virtio/vhost-vdpa.c static bool vhost_vdpa_svq_unmap_ring(struct vhost_vdpa *v, · line 902
Quoted 5 lines
hw/virtio/vhost-vdpa.c +2
-1size =ROUND_UP (result->size,qemu_real_host_page_size ()); r =vhost_vdpa_dma_unmap (v, result->iova, size); -return r ==0 ; +if (unlikely (r <0 )) { +return false;vhost-vdpa_svq_map_ring() will call
error_report()here, should we do the same?
We can use it unconditionally on error paths if we don't report errors. I can try to check if we use that way at this moment.
Quoted 17 lines
if (unlikely(r != 0)) { error_setg_errno(errp, -r, "Cannot map region to device");(Btw the error is not very informative, we should dump the map it self at least?)
Thanks
Quoted 7 lines
hw/virtio/vhost-vdpa.c +4
-0+ } + +vhost_iova_tree_remove (v->iova_tree, result); +return 0 ; } static bool vhost_vdpa_svq_unmap_rings(struct vhost_dev *dev,
PATCH 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,37 +893,32 @@ 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 )) { -return false; +return ; }vhost_iova_tree_remove (v->iova_tree, result); -return 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); }/**
@@ -1094,26 +1089,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)
@@ -1130,10 +1121,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
Re: [PATCH 5/7] vdpa: Make SVQ vring unmapping return void
Quoted 7 lines · Eugenio Pérez
Nothing 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.
Reported-by Lei Yang <[email protected]>Fixes 34e3c94eda ("vdpa: Add custom IOTLB translations to SVQ")Signed-off-by Eugenio Pérez <[email protected]>
Quoted 102 lines
--- 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,37 +893,32 @@ 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 )) { -return false; +return ; }vhost_iova_tree_remove (v->iova_tree, result); -return 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); }/** @@ -1094,26 +1089,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)@@ -1130,10 +1121,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); }
PATCH 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
Re: [PATCH 6/7] vhost: Always store new kick fd on vhost_svq_set_svq_kick_fd
Quoted 5 lines · Eugenio Pérez
We can unbind twice a file descriptor if we call twice
vhost_svq_set_svq_kick_fdbecause of this. Since it comes from vhost and not from SVQ, that file descriptor could be a different thing that guest's vhost notifier.
Quoted 7 lines
Likewise, it can happens the same if a guest start and stop the device multiple times.
Reported-by Lei Yang <[email protected]>Fixes dff4426fa6 ("vhost: Add Shadow VirtQueue kick forwarding capabilities")Signed-off-by Eugenio Pérez <[email protected]>---
Quoted 31 lines
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) {
PATCH 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;
@@ -908,17 +910,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); }/**
@@ -996,7 +995,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
Re: [PATCH 7/7] vdpa: Use ring hwaddr at vhost_vdpa_svq_unmap_ring
Quoted 3 lines · Eugenio Pérez
Reduce code duplication.
Signed-off-by Eugenio Pérez <[email protected]>
(In the future, we need to look for other cases where a function may use only a partial of DMAMap.)
Thanks
Quoted 53 lines
--- 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;@@ -908,17 +910,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); }/** @@ -996,7 +995,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;
Re: [PATCH 7/7] vdpa: Use ring hwaddr at vhost_vdpa_svq_unmap_ring
Quoted 5 lines · Jason Wang
Quoted 2 lines · Eugenio Pérez
Reduce code duplication.
Quoted 8 lines
Quoted 1 line
Signed-off-by Eugenio Pérez <[email protected]>Acked-by Jason Wang <[email protected]>(In the future, we need to look for other cases where a function may use only a partial of DMAMap.)
Do you have a use case in mind? The IOVA tree also trusts the memory listener will unmap and map chunks symmetrically.
Thanks!
Quoted 57 lines
Thanks
Quoted 53 lines
--- 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;@@ -908,17 +910,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); }/** @@ -996,7 +995,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;