Skip to contentSkip to conversation

vdpa: do not save failed dma maps in SVQ iova tree

PATCH Superseded Eugenio Pérez 2 participants 7 messages Aug 2, 2022 – Aug 4, 2022

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vdpa: do not save failed dma maps in SVQ iova tree

If 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.

Fixes 34e3c94eda ("vdpa: Add custom IOTLB translations to SVQ")
---
 hw/virtio/vhost-vdpa.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
hw/virtio/vhost-vdpa.c +13 -7
index 3ff9ce3501..e44c23dce5 100644
@@ -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
#1 lore raw
Quoted 4 lines · Eugenio Pérez

If 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.

I forgot to add:

Quoted 60 lines
Fixes 34e3c94eda ("vdpa: Add custom IOTLB translations to SVQ")
---
 hw/virtio/vhost-vdpa.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
hw/virtio/vhost-vdpa.c +13 -8
index 3ff9ce3501..e44c23dce5 100644
@@ -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
--

2.31.1

#2 lore raw
In reply to Eugenio Pérez
Quoted 23 lines · Eugenio Pérez

If 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.

Fixes 34e3c94eda ("vdpa: Add custom IOTLB translations to SVQ")
---
 hw/virtio/vhost-vdpa.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
hw/virtio/vhost-vdpa.c +1 -0
index 3ff9ce3501..e44c23dce5 100644
@@ -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;

hw/virtio/vhost-vdpa.c static void vhost_vdpa_listener_region_add(MemoryListener *listener, · line 219

Quoted 8 lines
hw/virtio/vhost-vdpa.c +0 -5
@@ -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),
-        };

Nit: can we keep this part unchanged?

Thanks

Quoted 32 lines
hw/virtio/vhost-vdpa.c +12 -3
+        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
--

2.31.1

#3 lore raw
Quoted 26 lines · Jason Wang
Quoted 24 lines · Eugenio Pérez

If 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.

Fixes 34e3c94eda ("vdpa: Add custom IOTLB translations to SVQ")
---
 hw/virtio/vhost-vdpa.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
hw/virtio/vhost-vdpa.c +1 -0
index 3ff9ce3501..e44c23dce5 100644
@@ -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,
 
Quoted 10 lines

hw/virtio/vhost-vdpa.c static void vhost_vdpa_listener_region_add(MemoryListener *listener, · line 219

Quoted 7 lines
hw/virtio/vhost-vdpa.c +0 -5
     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),
-        };

Nit: can we keep this part unchanged?

We can, but that implies we should look for iova again at fail_map tag. If you are ok with that I'm fine to perform the search again.

Quoted 35 lines

Thanks

Quoted 32 lines
hw/virtio/vhost-vdpa.c +12 -3
+        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
--

2.31.1

#4 lore raw
Quoted 34 lines · Eugenio Perez Martin
Quoted 24 lines · Jason Wang
Quoted 23 lines · Eugenio Pérez

If 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.

Fixes 34e3c94eda ("vdpa: Add custom IOTLB translations to SVQ")
---
  hw/virtio/vhost-vdpa.c | 20 +++++++++++++-------
  1 file changed, 13 insertions(+), 7 deletions(-)
hw/virtio/vhost-vdpa.c +1 -0
index 3ff9ce3501..e44c23dce5 100644
@@ -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,
 
Quoted 9 lines

hw/virtio/vhost-vdpa.c static void vhost_vdpa_listener_region_add(MemoryListener *listener, · line 219

Quoted 7 lines
hw/virtio/vhost-vdpa.c +0 -5
      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),
-        };

Nit: can we keep this part unchanged?

Quoted 2 lines

We can, but that implies we should look for iova again at fail_map tag. If you are ok with that I'm fine to perform the search again.

I meant something like:

hw/virtio/vhost-vdpa.c +0 -0
index 9a2daef7e3..edf40868e3 100644
@@ -232,11 +232,15 @@ static void vhost_vdpa_listener_region_add(MemoryListener *listener,

                              vaddr, section->readonly); if (ret) {
         error_report("vhost vdpa map fail!"); -        goto fail;
+        goto fail_unmap;
     }

     return;

+fail_unmap:
+    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

Thanks

Quoted 35 lines
Quoted 35 lines

Thanks

Quoted 32 lines
hw/virtio/vhost-vdpa.c +12 -3
+        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
--

2.31.1

#5 lore raw
Quoted 64 lines · Jason Wang
Quoted 34 lines · Eugenio Perez Martin
Quoted 24 lines · Jason Wang
Quoted 23 lines · Eugenio Pérez

If 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.

Fixes 34e3c94eda ("vdpa: Add custom IOTLB translations to SVQ")
---
  hw/virtio/vhost-vdpa.c | 20 +++++++++++++-------
  1 file changed, 13 insertions(+), 7 deletions(-)
hw/virtio/vhost-vdpa.c +1 -0
index 3ff9ce3501..e44c23dce5 100644
@@ -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,
 
Quoted 9 lines

hw/virtio/vhost-vdpa.c static void vhost_vdpa_listener_region_add(MemoryListener *listener, · line 219

Quoted 7 lines
hw/virtio/vhost-vdpa.c +0 -5
      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),
-        };

Nit: can we keep this part unchanged?

Quoted 2 lines

We can, but that implies we should look for iova again at fail_map tag. If you are ok with that I'm fine to perform the search again.

I meant something like:

hw/virtio/vhost-vdpa.c +5 -1
index 9a2daef7e3..edf40868e3 100644
@@ -232,11 +232,15 @@ static void
 vhost_vdpa_listener_region_add(MemoryListener *listener,
                               vaddr, section->readonly);
      if (ret) {
          error_report("vhost vdpa map fail!");
-        goto fail;
+        goto fail_unmap;
      }
 
      return;
 
+fail_unmap:
+    if (v->shadow_vqs_enabled) {
+        vhost_iova_tree_remove(v->iova_tree, &mem_region);
+    }
  fail:
      /*
Quoted 2 lines

* On the initfn path, store the first error in the container so we

Sorry, still not following.

mem_region does not exist in the error path, since it's declared in the if (v->shadow_vqs_enabled){} block. We can left first part unchanged if we do a lookup for the mem region, based on the translated addr.

Thanks!

Quoted 39 lines

Thanks

Quoted 35 lines
Quoted 35 lines

Thanks

Quoted 32 lines
hw/virtio/vhost-vdpa.c +12 -3
+        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
--

2.31.1

#6 lore raw
Quoted 71 lines · Eugenio Perez Martin
Quoted 64 lines · Jason Wang
Quoted 34 lines · Eugenio Perez Martin
Quoted 24 lines · Jason Wang
Quoted 23 lines · Eugenio Pérez

If 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.

Fixes 34e3c94eda ("vdpa: Add custom IOTLB translations to SVQ")
---
  hw/virtio/vhost-vdpa.c | 20 +++++++++++++-------
  1 file changed, 13 insertions(+), 7 deletions(-)
hw/virtio/vhost-vdpa.c +1 -0
index 3ff9ce3501..e44c23dce5 100644
@@ -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,
 
Quoted 9 lines

hw/virtio/vhost-vdpa.c static void vhost_vdpa_listener_region_add(MemoryListener *listener, · line 219

Quoted 7 lines
hw/virtio/vhost-vdpa.c +0 -5
      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),
-        };

Nit: can we keep this part unchanged?

Quoted 2 lines

We can, but that implies we should look for iova again at fail_map tag. If you are ok with that I'm fine to perform the search again.

I meant something like:

hw/virtio/vhost-vdpa.c +5 -1
index 9a2daef7e3..edf40868e3 100644
@@ -232,11 +232,15 @@ static void
 vhost_vdpa_listener_region_add(MemoryListener *listener,
                               vaddr, section->readonly);
      if (ret) {
          error_report("vhost vdpa map fail!");
-        goto fail;
+        goto fail_unmap;
      }
 
      return;
 
+fail_unmap:
+    if (v->shadow_vqs_enabled) {
+        vhost_iova_tree_remove(v->iova_tree, &mem_region);
+    }
  fail:
      /*
Quoted 2 lines

* On the initfn path, store the first error in the container so we

Sorry, still not following.

Quoted 5 lines

mem_region does not exist in the error path, since it's declared in the if (v->shadow_vqs_enabled){} block. We can left first part unchanged if we do a lookup for the mem region, based on the translated addr.

Sending v2 expanding the fix without this comment, please let me know ifI misunderstood something.

Thanks!