Blame SOURCES/kvm-util-accept-iova_tree_remove_parameter-by-value.patch

586cba
From 90697579eaf598614293d75f684d6e8c55f8ab9b Mon Sep 17 00:00:00 2001
586cba
From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= <eperezma@redhat.com>
586cba
Date: Tue, 23 Aug 2022 20:20:04 +0200
586cba
Subject: [PATCH 06/23] util: accept iova_tree_remove_parameter by value
586cba
MIME-Version: 1.0
586cba
Content-Type: text/plain; charset=UTF-8
586cba
Content-Transfer-Encoding: 8bit
586cba
586cba
RH-Author: Eugenio Pérez <eperezma@redhat.com>
586cba
RH-MergeRequest: 116: vdpa: Restore device state on destination
586cba
RH-Bugzilla: 2114060
586cba
RH-Acked-by: Cindy Lu <lulu@redhat.com>
586cba
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
586cba
RH-Commit: [5/21] ddaf052789e7ab3c67a77c038347113301587ffb (eperezmartin/qemu-kvm)
586cba
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2114060
586cba
Upstream status: git@github.com:jasowang/qemu.git net-next
586cba
586cba
It's convenient to call iova_tree_remove from a map returned from
586cba
iova_tree_find or iova_tree_find_iova. With the current code this is not
586cba
possible, since we will free it, and then we will try to search for it
586cba
again.
586cba
586cba
Fix it making accepting the map by value, forcing a copy of the
586cba
argument. Not applying a fixes tag, since there is no use like that at
586cba
the moment.
586cba
586cba
Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
586cba
Signed-off-by: Jason Wang <jasowang@redhat.com>
586cba
(cherry picked from commit d69ba6677405de86b3b617fc7688b549f84cf013)
586cba
---
586cba
 hw/i386/intel_iommu.c       | 6 +++---
586cba
 hw/virtio/vhost-iova-tree.c | 2 +-
586cba
 hw/virtio/vhost-iova-tree.h | 2 +-
586cba
 hw/virtio/vhost-vdpa.c      | 6 +++---
586cba
 include/qemu/iova-tree.h    | 2 +-
586cba
 net/vhost-vdpa.c            | 4 ++--
586cba
 util/iova-tree.c            | 4 ++--
586cba
 7 files changed, 13 insertions(+), 13 deletions(-)
586cba
586cba
diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c
586cba
index c64aa81a83..6738cf0929 100644
586cba
--- a/hw/i386/intel_iommu.c
586cba
+++ b/hw/i386/intel_iommu.c
586cba
@@ -1157,7 +1157,7 @@ static int vtd_page_walk_one(IOMMUTLBEvent *event, vtd_page_walk_info *info)
586cba
                     return ret;
586cba
                 }
586cba
                 /* Drop any existing mapping */
586cba
-                iova_tree_remove(as->iova_tree, &target);
586cba
+                iova_tree_remove(as->iova_tree, target);
586cba
                 /* Recover the correct type */
586cba
                 event->type = IOMMU_NOTIFIER_MAP;
586cba
                 entry->perm = cache_perm;
586cba
@@ -1170,7 +1170,7 @@ static int vtd_page_walk_one(IOMMUTLBEvent *event, vtd_page_walk_info *info)
586cba
             trace_vtd_page_walk_one_skip_unmap(entry->iova, entry->addr_mask);
586cba
             return 0;
586cba
         }
586cba
-        iova_tree_remove(as->iova_tree, &target);
586cba
+        iova_tree_remove(as->iova_tree, target);
586cba
     }
586cba
 
586cba
     trace_vtd_page_walk_one(info->domain_id, entry->iova,
586cba
@@ -3532,7 +3532,7 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)
586cba
 
586cba
     map.iova = n->start;
586cba
     map.size = size;
586cba
-    iova_tree_remove(as->iova_tree, &map);
586cba
+    iova_tree_remove(as->iova_tree, map);
586cba
 }
586cba
 
586cba
 static void vtd_address_space_unmap_all(IntelIOMMUState *s)
586cba
diff --git a/hw/virtio/vhost-iova-tree.c b/hw/virtio/vhost-iova-tree.c
586cba
index 55fed1fefb..1339a4de8b 100644
586cba
--- a/hw/virtio/vhost-iova-tree.c
586cba
+++ b/hw/virtio/vhost-iova-tree.c
586cba
@@ -104,7 +104,7 @@ int vhost_iova_tree_map_alloc(VhostIOVATree *tree, DMAMap *map)
586cba
  * @iova_tree: The vhost iova tree
586cba
  * @map: The map to remove
586cba
  */
586cba
-void vhost_iova_tree_remove(VhostIOVATree *iova_tree, const DMAMap *map)
586cba
+void vhost_iova_tree_remove(VhostIOVATree *iova_tree, DMAMap map)
586cba
 {
586cba
     iova_tree_remove(iova_tree->iova_taddr_map, map);
586cba
 }
586cba
diff --git a/hw/virtio/vhost-iova-tree.h b/hw/virtio/vhost-iova-tree.h
586cba
index 6a4f24e0f9..4adfd79ff0 100644
586cba
--- a/hw/virtio/vhost-iova-tree.h
586cba
+++ b/hw/virtio/vhost-iova-tree.h
586cba
@@ -22,6 +22,6 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(VhostIOVATree, vhost_iova_tree_delete);
586cba
 const DMAMap *vhost_iova_tree_find_iova(const VhostIOVATree *iova_tree,
586cba
                                         const DMAMap *map);
586cba
 int vhost_iova_tree_map_alloc(VhostIOVATree *iova_tree, DMAMap *map);
586cba
-void vhost_iova_tree_remove(VhostIOVATree *iova_tree, const DMAMap *map);
586cba
+void vhost_iova_tree_remove(VhostIOVATree *iova_tree, DMAMap map);
586cba
 
586cba
 #endif
586cba
diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c
586cba
index cc15b7d8ee..39aa70f52d 100644
586cba
--- a/hw/virtio/vhost-vdpa.c
586cba
+++ b/hw/virtio/vhost-vdpa.c
586cba
@@ -238,7 +238,7 @@ static void vhost_vdpa_listener_region_add(MemoryListener *listener,
586cba
 
586cba
 fail_map:
586cba
     if (v->shadow_vqs_enabled) {
586cba
-        vhost_iova_tree_remove(v->iova_tree, &mem_region);
586cba
+        vhost_iova_tree_remove(v->iova_tree, mem_region);
586cba
     }
586cba
 
586cba
 fail:
586cba
@@ -298,7 +298,7 @@ static void vhost_vdpa_listener_region_del(MemoryListener *listener,
586cba
             return;
586cba
         }
586cba
         iova = result->iova;
586cba
-        vhost_iova_tree_remove(v->iova_tree, result);
586cba
+        vhost_iova_tree_remove(v->iova_tree, *result);
586cba
     }
586cba
     vhost_vdpa_iotlb_batch_begin_once(v);
586cba
     ret = vhost_vdpa_dma_unmap(v, iova, int128_get64(llsize));
586cba
@@ -942,7 +942,7 @@ static bool vhost_vdpa_svq_map_ring(struct vhost_vdpa *v, DMAMap *needle,
586cba
                            needle->perm == IOMMU_RO);
586cba
     if (unlikely(r != 0)) {
586cba
         error_setg_errno(errp, -r, "Cannot map region to device");
586cba
-        vhost_iova_tree_remove(v->iova_tree, needle);
586cba
+        vhost_iova_tree_remove(v->iova_tree, *needle);
586cba
     }
586cba
 
586cba
     return r == 0;
586cba
diff --git a/include/qemu/iova-tree.h b/include/qemu/iova-tree.h
586cba
index 16bbfdf5f8..8528e5c98f 100644
586cba
--- a/include/qemu/iova-tree.h
586cba
+++ b/include/qemu/iova-tree.h
586cba
@@ -73,7 +73,7 @@ int iova_tree_insert(IOVATree *tree, const DMAMap *map);
586cba
  * all the mappings that are included in the provided range will be
586cba
  * removed from the tree.  Here map->translated_addr is meaningless.
586cba
  */
586cba
-void iova_tree_remove(IOVATree *tree, const DMAMap *map);
586cba
+void iova_tree_remove(IOVATree *tree, DMAMap map);
586cba
 
586cba
 /**
586cba
  * iova_tree_find:
586cba
diff --git a/net/vhost-vdpa.c b/net/vhost-vdpa.c
586cba
index 411e71e6c2..ba65736f83 100644
586cba
--- a/net/vhost-vdpa.c
586cba
+++ b/net/vhost-vdpa.c
586cba
@@ -244,7 +244,7 @@ static void vhost_vdpa_cvq_unmap_buf(struct vhost_vdpa *v, void *addr)
586cba
         error_report("Device cannot unmap: %s(%d)", g_strerror(r), r);
586cba
     }
586cba
 
586cba
-    vhost_iova_tree_remove(tree, map);
586cba
+    vhost_iova_tree_remove(tree, *map);
586cba
 }
586cba
 
586cba
 static size_t vhost_vdpa_net_cvq_cmd_len(void)
586cba
@@ -297,7 +297,7 @@ static bool vhost_vdpa_cvq_map_buf(struct vhost_vdpa *v,
586cba
     return true;
586cba
 
586cba
 dma_map_err:
586cba
-    vhost_iova_tree_remove(v->iova_tree, &map);
586cba
+    vhost_iova_tree_remove(v->iova_tree, map);
586cba
     return false;
586cba
 }
586cba
 
586cba
diff --git a/util/iova-tree.c b/util/iova-tree.c
586cba
index fee530a579..536789797e 100644
586cba
--- a/util/iova-tree.c
586cba
+++ b/util/iova-tree.c
586cba
@@ -164,11 +164,11 @@ void iova_tree_foreach(IOVATree *tree, iova_tree_iterator iterator)
586cba
     g_tree_foreach(tree->tree, iova_tree_traverse, iterator);
586cba
 }
586cba
 
586cba
-void iova_tree_remove(IOVATree *tree, const DMAMap *map)
586cba
+void iova_tree_remove(IOVATree *tree, DMAMap map)
586cba
 {
586cba
     const DMAMap *overlap;
586cba
 
586cba
-    while ((overlap = iova_tree_find(tree, map))) {
586cba
+    while ((overlap = iova_tree_find(tree, &map))) {
586cba
         g_tree_remove(tree->tree, overlap);
586cba
     }
586cba
 }
586cba
-- 
586cba
2.31.1
586cba