Handling aliased guest memory maps in vhost-vDPA SVQs
v3 cover Handling aliased guest memory maps in vhost-vDPA SVQs
An issue arises from aliased memory mappings in the guest, where different GPAs map to the same HVA. For example:
- GPA1->HVA1
- GPA2->HVA1
When these mappings exist in the IOVA->HVA tree, a lookup by an HVA backed by guest memory results in ambiguities because the same HVA could correspond to multiple GPAs. This duplication causes issues in managing IOVA trees for SVQs in vDPA, leading to incorrect behavior.
For example, consider these mapped guest memory regions:
HVA GPA IOVA
------------------------------- --------------------------- ----------------------------
[0x7f7903e00000, 0x7f7983e00000) [0x0, 0x80000000) [0x1000, 0x80000000)
[0x7f7983e00000, 0x7f9903e00000) [0x100000000, 0x2080000000) [0x80001000, 0x2000001000)
[0x7f7903ea0000, 0x7f7903ec0000) [0xfeda0000, 0xfedc0000) [0x2000001000, 0x2000021000)
The last HVA range [0x7f7903ea0000, 0x7f7903ec0000) is contained within the first HVA range [0x7f7903e00000, 0x7f7983e00000). Despite this, the GPA ranges for the first and third mappings do not overlap, so the guest sees them as distinct physical memory regions.
Now consider an operation to unmap the mapping associated with HVA 0x7f7903eb0000. This HVA fits into both the first and third HVA ranges.
When searching the HVA->IOVA tree, the search stops at the first matching HVA range [0x7f7903e00000, 0x7f7983e00000) due to the behavior of the red-black tree (GTree). However, the correct mapping to remove is the third mappings, as the HVA translate to GPA 0xfedb0000, which only fits in the third mapping's GPA range.
To address this, we implement a GPA->IOVA tree and use the GPAs of descriptors backed by guest memory when translating to IOVA.
When a descriptor's GPA is used for translation, the GPA->IOVA tree ensures that each GPA maps to exactly one IOVA, regardless of any overlapping HVAs. This guarantees that operations such as unmapping or accessing a descriptor correctly targets the intended memory region in the guest's address space.
For descriptors not backed by guest memory, the existing IOVA->HVA tree is still used.
GPAs are unique and non-overlapping within the guest's address space. By translating GPAs to IOVAs, the ambiguity caused by multiple GPAs mapping to the same HVA is avoided.
--------
This series is a different approach of [1] and is based off of [2], where this issue was originally discovered.
RFC v3:
-------
* Decouple the IOVA allocator to support a SVQ IOVA->HVA tree for host-only mappings.
* Move range check for IOVA allocator to its own patch.
* Implement a GPA->IOVA tree alongside the SVQ IOVA->HVA & IOVA-only trees.
* Add in_xlat_addr & out_xlat_addr VirtQueueElement members to hold the GPAs of an element's input/output descriptors (to be used during translation).
RFC v2:
-------
* Don't decouple IOVA allocator.
* Build a IOVA->GPA tree (instead of GPA->IOVA tree).
* Remove IOVA-only tree and keep the full IOVA->HVA tree.
* Only search through RAMBlocks when we know the HVA is backed by guest memory.
* Slight rewording of function names.
RFC v1:
-------
* Alternative approach of [1].
* First attempt to address this issue found in [2].
[1] https://lore.kernel.org/qemu-devel/[email protected]
[2] https://lore.kernel.org/qemu-devel/[email protected]
Jonah Palmer (5):
vhost-vdpa: Decouple the IOVA allocator
vhost-iova-tree: Remove range check for IOVA allocator
vhost-vdpa: Implement the GPA->IOVA tree
virtio: add in_xlat_addr & out_xlat_addr VirtQueueElement members
svq: Support translations via GPAs in vhost_svq_translate_addr
hw/display/virtio-gpu.c | 5 +- hw/hyperv/vmbus.c | 8 ++- hw/ide/ahci.c | 7 ++- hw/usb/libhw.c | 2 +- hw/virtio/vhost-iova-tree.c | 88 ++++++++++++++++++++++++++++-- hw/virtio/vhost-iova-tree.h | 5 ++ hw/virtio/vhost-shadow-virtqueue.c | 49 +++++++++++++---- hw/virtio/vhost-vdpa.c | 43 ++++++++++----- hw/virtio/virtio.c | 50 ++++++++++++----- include/hw/pci/pci_device.h | 2 +- include/hw/virtio/virtio.h | 2 + include/qemu/iova-tree.h | 22 ++++++++ include/system/dma.h | 25 ++++++++- net/vhost-vdpa.c | 13 ++++- system/dma-helpers.c | 2 +- util/iova-tree.c | 46 ++++++++++++++++ 16 files changed, 310 insertions(+), 59 deletions(-)
Signature · 1 line
2.43.5
RFC v3 1/5 vhost-vdpa: Decouple the IOVA allocator
Decouples the IOVA allocator from the full IOVA->HVA tree to support a SVQ IOVA->HVA tree for host-only memory mappings.
The IOVA allocator still allocates an IOVA range but instead adds this range to an IOVA-only tree (iova_map) that keeps track of allocated IOVA ranges for both guest & host-only memory mappings.
A new API function vhost_iova_tree_insert() is also created for adding IOVA->HVA mappings into the SVQ IOVA->HVA tree, since the allocator is no longer doing that.
--- hw/virtio/vhost-iova-tree.c | 35 +++++++++++++++++++++++++++++++---- hw/virtio/vhost-iova-tree.h | 1 + hw/virtio/vhost-vdpa.c | 21 ++++++++++++++++----- net/vhost-vdpa.c | 13 +++++++++++-- 4 files changed, 59 insertions(+), 11 deletions(-)
hw/virtio/vhost-iova-tree.c +31 -4
@@ -28,12 +28,15 @@ struct VhostIOVATree {/* IOVA address to qemu memory maps. */ IOVATree *iova_taddr_map; + +/* Allocated IOVA addresses */ +IOVATree *iova_map; };/** -* Create a new IOVA tree +* Create a new VhostIOVATree * -* Returns the new IOVA tree +* Returns the new VhostIOVATree */ VhostIOVATree *vhost_iova_tree_new (hwaddr iova_first,hwaddr iova_last) {
@@ -44,15 +47,17 @@ VhostIOVATree *vhost_iova_tree_new(hwaddr iova_first, hwaddr iova_last) tree->iova_last = iova_last; tree->iova_taddr_map =iova_tree_new (); + tree->iova_map =iova_tree_new ();return tree; }/** -* Delete an iova tree +* Delete a VhostIOVATree */ void vhost_iova_tree_delete (VhostIOVATree *iova_tree) {iova_tree_destroy (iova_tree->iova_taddr_map); +iova_tree_destroy (iova_tree->iova_map);g_free (iova_tree); }
@@ -94,7 +99,7 @@ int vhost_iova_tree_map_alloc(VhostIOVATree *tree, DMAMap *map) }/* Allocate a node in IOVA address */ -return iova_tree_alloc_map (tree->iova_taddr_map, map, iova_first, +return iova_tree_alloc_map (tree->iova_map, map, iova_first, tree->iova_last); }
@@ -107,4 +112,26 @@ int vhost_iova_tree_map_alloc(VhostIOVATree *tree, DMAMap *map)void vhost_iova_tree_remove (VhostIOVATree *iova_tree,DMAMap map) {iova_tree_remove (iova_tree->iova_taddr_map, map); +iova_tree_remove (iova_tree->iova_map, map); +} + +/** +* Insert a new mapping to the IOVA->HVA tree +* +* @tree: The VhostIOVATree +* @map: The IOVA->HVA mapping +* +* Returns: +* - IOVA_OK if the map fits in the container +* - IOVA_ERR_INVALID if the map does not make sense (e.g. size overflow) +* - IOVA_ERR_OVERLAP if the IOVA range overlaps with an existing range +*/ +int vhost_iova_tree_insert (VhostIOVATree *iova_tree,DMAMap *map) +{ +if (map->translated_addr + map->size < map->translated_addr || + map->perm ==IOMMU_NONE ) { +return IOVA_ERR_INVALID ; + } + +return iova_tree_insert (iova_tree->iova_taddr_map, map); }
hw/virtio/vhost-iova-tree.h +1 -0
@@ -23,5 +23,6 @@ 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,DMAMap map); +int vhost_iova_tree_insert (VhostIOVATree *iova_tree,DMAMap *map);#endif
hw/virtio/vhost-vdpa.c +16 -5
@@ -1142,18 +1142,29 @@ static void vhost_vdpa_svq_unmap_rings(struct vhost_dev *dev,* * @v: Vhost-vdpa device * @needle: The area to search iova +* @taddr: The translated address (SVQ HVA) * @errorp: Error pointer */ static bool vhost_vdpa_svq_map_ring (struct vhost_vdpa *v,DMAMap *needle, -Error **errp) +hwaddr taddr,Error **errp) {int r; +/* Allocate an IOVA range in the IOVA tree */ r =vhost_iova_tree_map_alloc (v->shared->iova_tree, needle);if (unlikely (r !=IOVA_OK )) {error_setg (errp,"Cannot allocate iova (%d)" , r);return false; } + needle->translated_addr = taddr; + +/* Add IOVA->HVA mapping to the IOVA->HVA tree */ + r =vhost_iova_tree_insert (v->shared->iova_tree, needle); +if (unlikely (r !=IOVA_OK )) { +error_setg (errp,"Cannot add SVQ vring mapping (%d)" , r); +vhost_iova_tree_remove (v->shared->iova_tree, *needle); +return false; + } r = vhost_vdpa_dma_map(v->shared, v->address_space_id, needle->iova, needle->size + 1,
@@ -1192,11 +1203,11 @@ static bool vhost_vdpa_svq_map_rings(struct vhost_dev *dev,vhost_svq_get_vring_addr (svq, &svq_addr); driver_region = (DMAMap ) { - .translated_addr = svq_addr.desc_user_addr, .size = driver_size -1 , .perm =IOMMU_RO , }; - ok =vhost_vdpa_svq_map_ring (v, &driver_region, errp); + ok =vhost_vdpa_svq_map_ring (v, &driver_region, svq_addr.desc_user_addr, + errp);if (unlikely (!ok)) {error_prepend (errp,"Cannot create vq driver region: " );return false;
@@ -1206,11 +1217,11 @@ static bool vhost_vdpa_svq_map_rings(struct vhost_dev *dev, addr->avail_user_addr = driver_region.iova + avail_offset; device_region = (DMAMap ) { - .translated_addr = svq_addr.used_user_addr, .size = device_size -1 , .perm =IOMMU_RW , }; - ok =vhost_vdpa_svq_map_ring (v, &device_region, errp); + ok =vhost_vdpa_svq_map_ring (v, &device_region, svq_addr.used_user_addr, + errp);if (unlikely (!ok)) {error_prepend (errp,"Cannot create vq device region: " );vhost_vdpa_svq_unmap_ring (v, driver_region.translated_addr);
net/vhost-vdpa.c +11 -2
@@ -512,14 +512,23 @@ static int vhost_vdpa_cvq_map_buf(struct vhost_vdpa *v, void *buf, size_t size,DMAMap map = {};int r; - map.translated_addr = (hwaddr )(uintptr_t )buf; map.size = size -1 ; map.perm = write ?IOMMU_RW :IOMMU_RO , + +/* Allocate an IOVA range in the IOVA tree */ r =vhost_iova_tree_map_alloc (v->shared->iova_tree, &map);if (unlikely (r !=IOVA_OK )) { -error_report ("Cannot map injected element" ); +error_report ("Cannot allocate IOVA range for injected element" );return r; } + map.translated_addr = (hwaddr )(uintptr_t )buf; + +/* Add IOVA->HVA mapping to the IOVA->HVA tree */ + r =vhost_iova_tree_insert (v->shared->iova_tree, &map); +if (unlikely (r !=IOVA_OK )) { +error_report ("Cannot map injected element into IOVA->HVA tree" ); + goto dma_map_err; + } r =vhost_vdpa_dma_map (v->shared, v->address_space_id, map.iova,vhost_vdpa_net_cvq_cmd_page_len (), buf, !write);
Signature · 1 line
2.43.5
Re: [RFC v3 1/5] vhost-vdpa: Decouple the IOVA allocator
Quoted 4 lines · Jonah Palmer
Decouples the IOVA allocator from the full
IOVA->HVAtree to support a SVQIOVA->HVAtree for host-only memory mappings.
Quoted 8 lines
The IOVA allocator still allocates an IOVA range but instead adds this range to an IOVA-only tree (
iova_map) that keeps track of allocated IOVA ranges for both guest & host-only memory mappings.A new API function
vhost_iova_tree_insert()is also created for addingIOVA->HVAmappings into the SVQIOVA->HVAtree, since the allocator is no longer doing that.
What is the reason for not adding IOVA -> HVA tree on _alloc automatically? The problematic one is GPA -> HVA, isn't it? Doing this way we force all the allocations to do the two calls (alloc+insert), or the trees will be inconsistent.
Quoted 31 lines
Signed-off-by Jonah Palmer <[email protected]>--- hw/virtio/vhost-iova-tree.c | 35 +++++++++++++++++++++++++++++++---- hw/virtio/vhost-iova-tree.h | 1 + hw/virtio/vhost-vdpa.c | 21 ++++++++++++++++----- net/vhost-vdpa.c | 13 +++++++++++-- 4 files changed, 59 insertions(+), 11 deletions(-)hw/virtio/vhost-iova-tree.c +5
-2@@ -28,12 +28,15 @@ struct VhostIOVATree {/* IOVA address to qemu memory maps. */ IOVATree *iova_taddr_map; + +/* Allocated IOVA addresses */ +IOVATree *iova_map; };/** -* Create a new IOVA tree +* Create a new VhostIOVATree * -* Returns the new IOVA tree +* Returns the new VhostIOVATree */ VhostIOVATree *vhost_iova_tree_new (hwaddr iova_first,hwaddr iova_last) {
hw/virtio/vhost-iova-tree.c VhostIOVATree *vhost_iova_tree_new(hwaddr iova_first, hwaddr iova_last) · line 55
Quoted 10 lines
hw/virtio/vhost-iova-tree.c +2
-1@@ -44,15 +47,17 @@ VhostIOVATree *vhost_iova_tree_new(hwaddr iova_first, hwaddr iova_last) tree->iova_last = iova_last; tree->iova_taddr_map =iova_tree_new (); + tree->iova_map =iova_tree_new ();return tree; }/** -* Delete an iova tree +* Delete a VhostIOVATree
Thanks for fixing the doc of new and delete :) Maybe it is better to put it in an independent patch?
Quoted 151 lines
hw/virtio/vhost-iova-tree.c +24
-1*/ void vhost_iova_tree_delete (VhostIOVATree *iova_tree) {iova_tree_destroy (iova_tree->iova_taddr_map); +iova_tree_destroy (iova_tree->iova_map);g_free (iova_tree); }@@ -94,7 +99,7 @@ int vhost_iova_tree_map_alloc(VhostIOVATree *tree, DMAMap *map) }/* Allocate a node in IOVA address */ -return iova_tree_alloc_map (tree->iova_taddr_map, map, iova_first, +return iova_tree_alloc_map (tree->iova_map, map, iova_first, tree->iova_last); }@@ -107,4 +112,26 @@ int vhost_iova_tree_map_alloc(VhostIOVATree *tree, DMAMap *map)void vhost_iova_tree_remove (VhostIOVATree *iova_tree,DMAMap map) {iova_tree_remove (iova_tree->iova_taddr_map, map); +iova_tree_remove (iova_tree->iova_map, map); +} + +/** +* Insert a new mapping to the IOVA->HVA tree +* +* @tree: The VhostIOVATree +* @map: The IOVA->HVA mapping +* +* Returns: +* - IOVA_OK if the map fits in the container +* - IOVA_ERR_INVALID if the map does not make sense (e.g. size overflow) +* - IOVA_ERR_OVERLAP if the IOVA range overlaps with an existing range +*/ +int vhost_iova_tree_insert (VhostIOVATree *iova_tree,DMAMap *map) +{ +if (map->translated_addr + map->size < map->translated_addr || + map->perm ==IOMMU_NONE ) { +return IOVA_ERR_INVALID ; + } + +return iova_tree_insert (iova_tree->iova_taddr_map, map); }hw/virtio/vhost-iova-tree.h +1
-0@@ -23,5 +23,6 @@ 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,DMAMap map); +int vhost_iova_tree_insert (VhostIOVATree *iova_tree,DMAMap *map);#endif hw/virtio/vhost-vdpa.c +16
-5@@ -1142,18 +1142,29 @@ static void vhost_vdpa_svq_unmap_rings(struct vhost_dev *dev,* * @v: Vhost-vdpa device * @needle: The area to search iova +* @taddr: The translated address (SVQ HVA) * @errorp: Error pointer */ static bool vhost_vdpa_svq_map_ring (struct vhost_vdpa *v,DMAMap *needle, -Error **errp) +hwaddr taddr,Error **errp) {int r; +/* Allocate an IOVA range in the IOVA tree */ r =vhost_iova_tree_map_alloc (v->shared->iova_tree, needle);if (unlikely (r !=IOVA_OK )) {error_setg (errp,"Cannot allocate iova (%d)" , r);return false; } + needle->translated_addr = taddr; + +/* Add IOVA->HVA mapping to the IOVA->HVA tree */ + r =vhost_iova_tree_insert (v->shared->iova_tree, needle); +if (unlikely (r !=IOVA_OK )) { +error_setg (errp,"Cannot add SVQ vring mapping (%d)" , r); +vhost_iova_tree_remove (v->shared->iova_tree, *needle); +return false; + } r = vhost_vdpa_dma_map(v->shared, v->address_space_id, needle->iova, needle->size + 1,@@ -1192,11 +1203,11 @@ static bool vhost_vdpa_svq_map_rings(struct vhost_dev *dev,vhost_svq_get_vring_addr (svq, &svq_addr); driver_region = (DMAMap ) { - .translated_addr = svq_addr.desc_user_addr, .size = driver_size -1 , .perm =IOMMU_RO , }; - ok =vhost_vdpa_svq_map_ring (v, &driver_region, errp); + ok =vhost_vdpa_svq_map_ring (v, &driver_region, svq_addr.desc_user_addr, + errp);if (unlikely (!ok)) {error_prepend (errp,"Cannot create vq driver region: " );return false;@@ -1206,11 +1217,11 @@ static bool vhost_vdpa_svq_map_rings(struct vhost_dev *dev, addr->avail_user_addr = driver_region.iova + avail_offset; device_region = (DMAMap ) { - .translated_addr = svq_addr.used_user_addr, .size = device_size -1 , .perm =IOMMU_RW , }; - ok =vhost_vdpa_svq_map_ring (v, &device_region, errp); + ok =vhost_vdpa_svq_map_ring (v, &device_region, svq_addr.used_user_addr, + errp);if (unlikely (!ok)) {error_prepend (errp,"Cannot create vq device region: " );vhost_vdpa_svq_unmap_ring (v, driver_region.translated_addr);net/vhost-vdpa.c +11
-3@@ -512,14 +512,23 @@ static int vhost_vdpa_cvq_map_buf(struct vhost_vdpa *v, void *buf, size_t size,DMAMap map = {};int r; - map.translated_addr = (hwaddr )(uintptr_t )buf; map.size = size -1 ; map.perm = write ?IOMMU_RW :IOMMU_RO , + +/* Allocate an IOVA range in the IOVA tree */ r =vhost_iova_tree_map_alloc (v->shared->iova_tree, &map);if (unlikely (r !=IOVA_OK )) { -error_report ("Cannot map injected element" ); +error_report ("Cannot allocate IOVA range for injected element" );return r; } + map.translated_addr = (hwaddr )(uintptr_t )buf; + +/* Add IOVA->HVA mapping to the IOVA->HVA tree */ + r =vhost_iova_tree_insert (v->shared->iova_tree, &map); +if (unlikely (r !=IOVA_OK )) { +error_report ("Cannot map injected element into IOVA->HVA tree" ); + goto dma_map_err; + } r =vhost_vdpa_dma_map (v->shared, v->address_space_id, map.iova,vhost_vdpa_net_cvq_cmd_page_len (), buf, !write); --2.43.5
Re: [RFC v3 1/5] vhost-vdpa: Decouple the IOVA allocator
Quoted 14 lines · Eugenio Perez Martin
Quoted 4 lines · Jonah Palmer
Decouples the IOVA allocator from the full
IOVA->HVAtree to support a SVQIOVA->HVAtree for host-only memory mappings.Quoted 8 lines
The IOVA allocator still allocates an IOVA range but instead adds this range to an IOVA-only tree (
iova_map) that keeps track of allocated IOVA ranges for both guest & host-only memory mappings.A new API function
vhost_iova_tree_insert()is also created for addingIOVA->HVAmappings into the SVQIOVA->HVAtree, since the allocator is no longer doing that.
Quoted 5 lines
What is the reason for not adding IOVA -> HVA tree on
_allocautomatically? The problematic one is GPA -> HVA, isn't it? Doing this way we force all the allocations to do the two calls (alloc+insert), or the trees will be inconsistent.
Ah, I believe you also made a similar comment in RFC v1, saying it wasn't intuitive for the user to follow up with a vhost_iova_tree_insert() call afterwards (e.g. in vhost_vdpa_svq_map_ring() or vhost_vdpa_cvq_map_buf()).
I believe what I ended up doing in RFC v2 was creating separate alloc functions for host-only memory mapping (e.g. vhost_vdpa_svq_map_ring() and vhost_vdpa_cvq_map_buf()) and guest-backed memory mapping (e.g. vhost_vdpa_listener_region_add()).
This way, for host-only memory, in the alloc function we allocate an IOVA range (in the IOVA-only tree) and then also inserts the IOVA->HVA mapping into the SVQ IOVA->HVA tree. Similarly, for guest-backed memory, we create its own alloc function (e.g. vhost_iova_tree_map_alloc_gpa()), allocate the IOVA range (in the IOVA-only tree) and then insert the GPA->IOVA mapping into the GPA->IOVA tree.
This was done so that we didn't have to rely on the user to also call the insertion function after calling the allocation function.
Is this kinda what you're thinking of here?
Quoted 38 lines
Quoted 38 lines
Signed-off-by Jonah Palmer <[email protected]>--- hw/virtio/vhost-iova-tree.c | 35 +++++++++++++++++++++++++++++++---- hw/virtio/vhost-iova-tree.h | 1 + hw/virtio/vhost-vdpa.c | 21 ++++++++++++++++----- net/vhost-vdpa.c | 13 +++++++++++-- 4 files changed, 59 insertions(+), 11 deletions(-)hw/virtio/vhost-iova-tree.c +6
-2@@ -28,12 +28,15 @@ struct VhostIOVATree {/* IOVA address to qemu memory maps. */ IOVATree *iova_taddr_map; + +/* Allocated IOVA addresses */ +IOVATree *iova_map; };/** -* Create a new IOVA tree +* Create a new VhostIOVATree * -* Returns the new IOVA tree +* Returns the new VhostIOVATree */ VhostIOVATree *vhost_iova_tree_new (hwaddr iova_first,hwaddr iova_last) {@@ -44,15 +47,17 @@ VhostIOVATree *vhost_iova_tree_new(hwaddr iova_first, hwaddr iova_last) tree->iova_last = iova_last; tree->iova_taddr_map =iova_tree_new (); + tree->iova_map =iova_tree_new ();return tree; }
Quoted 7 lines
hw/virtio/vhost-iova-tree.c VhostIOVATree *vhost_iova_tree_new(hwaddr iova_first, hwaddr iova_last) · line 55
Quoted 3 lines
hw/virtio/vhost-iova-tree.c +1
-1/** -* Delete an iova tree +* Delete a VhostIOVATree Thanks for fixing the doc of new and delete :) Maybe it is better to put it in an independent patch?
Sure can :)
Quoted 152 lines
Quoted 151 lines
hw/virtio/vhost-iova-tree.c +24
-1*/ void vhost_iova_tree_delete (VhostIOVATree *iova_tree) {iova_tree_destroy (iova_tree->iova_taddr_map); +iova_tree_destroy (iova_tree->iova_map);g_free (iova_tree); }@@ -94,7 +99,7 @@ int vhost_iova_tree_map_alloc(VhostIOVATree *tree, DMAMap *map) }/* Allocate a node in IOVA address */ -return iova_tree_alloc_map (tree->iova_taddr_map, map, iova_first, +return iova_tree_alloc_map (tree->iova_map, map, iova_first, tree->iova_last); }@@ -107,4 +112,26 @@ int vhost_iova_tree_map_alloc(VhostIOVATree *tree, DMAMap *map)void vhost_iova_tree_remove (VhostIOVATree *iova_tree,DMAMap map) {iova_tree_remove (iova_tree->iova_taddr_map, map); +iova_tree_remove (iova_tree->iova_map, map); +} + +/** +* Insert a new mapping to the IOVA->HVA tree +* +* @tree: The VhostIOVATree +* @map: The IOVA->HVA mapping +* +* Returns: +* - IOVA_OK if the map fits in the container +* - IOVA_ERR_INVALID if the map does not make sense (e.g. size overflow) +* - IOVA_ERR_OVERLAP if the IOVA range overlaps with an existing range +*/ +int vhost_iova_tree_insert (VhostIOVATree *iova_tree,DMAMap *map) +{ +if (map->translated_addr + map->size < map->translated_addr || + map->perm ==IOMMU_NONE ) { +return IOVA_ERR_INVALID ; + } + +return iova_tree_insert (iova_tree->iova_taddr_map, map); }hw/virtio/vhost-iova-tree.h +1
-0@@ -23,5 +23,6 @@ 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,DMAMap map); +int vhost_iova_tree_insert (VhostIOVATree *iova_tree,DMAMap *map);#endif hw/virtio/vhost-vdpa.c +16
-5@@ -1142,18 +1142,29 @@ static void vhost_vdpa_svq_unmap_rings(struct vhost_dev *dev,* * @v: Vhost-vdpa device * @needle: The area to search iova +* @taddr: The translated address (SVQ HVA) * @errorp: Error pointer */ static bool vhost_vdpa_svq_map_ring (struct vhost_vdpa *v,DMAMap *needle, -Error **errp) +hwaddr taddr,Error **errp) {int r; +/* Allocate an IOVA range in the IOVA tree */ r =vhost_iova_tree_map_alloc (v->shared->iova_tree, needle);if (unlikely (r !=IOVA_OK )) {error_setg (errp,"Cannot allocate iova (%d)" , r);return false; } + needle->translated_addr = taddr; + +/* Add IOVA->HVA mapping to the IOVA->HVA tree */ + r =vhost_iova_tree_insert (v->shared->iova_tree, needle); +if (unlikely (r !=IOVA_OK )) { +error_setg (errp,"Cannot add SVQ vring mapping (%d)" , r); +vhost_iova_tree_remove (v->shared->iova_tree, *needle); +return false; + } r = vhost_vdpa_dma_map(v->shared, v->address_space_id, needle->iova, needle->size + 1,@@ -1192,11 +1203,11 @@ static bool vhost_vdpa_svq_map_rings(struct vhost_dev *dev,vhost_svq_get_vring_addr (svq, &svq_addr); driver_region = (DMAMap ) { - .translated_addr = svq_addr.desc_user_addr, .size = driver_size -1 , .perm =IOMMU_RO , }; - ok =vhost_vdpa_svq_map_ring (v, &driver_region, errp); + ok =vhost_vdpa_svq_map_ring (v, &driver_region, svq_addr.desc_user_addr, + errp);if (unlikely (!ok)) {error_prepend (errp,"Cannot create vq driver region: " );return false;@@ -1206,11 +1217,11 @@ static bool vhost_vdpa_svq_map_rings(struct vhost_dev *dev, addr->avail_user_addr = driver_region.iova + avail_offset; device_region = (DMAMap ) { - .translated_addr = svq_addr.used_user_addr, .size = device_size -1 , .perm =IOMMU_RW , }; - ok =vhost_vdpa_svq_map_ring (v, &device_region, errp); + ok =vhost_vdpa_svq_map_ring (v, &device_region, svq_addr.used_user_addr, + errp);if (unlikely (!ok)) {error_prepend (errp,"Cannot create vq device region: " );vhost_vdpa_svq_unmap_ring (v, driver_region.translated_addr);net/vhost-vdpa.c +11
-3@@ -512,14 +512,23 @@ static int vhost_vdpa_cvq_map_buf(struct vhost_vdpa *v, void *buf, size_t size,DMAMap map = {};int r; - map.translated_addr = (hwaddr )(uintptr_t )buf; map.size = size -1 ; map.perm = write ?IOMMU_RW :IOMMU_RO , + +/* Allocate an IOVA range in the IOVA tree */ r =vhost_iova_tree_map_alloc (v->shared->iova_tree, &map);if (unlikely (r !=IOVA_OK )) { -error_report ("Cannot map injected element" ); +error_report ("Cannot allocate IOVA range for injected element" );return r; } + map.translated_addr = (hwaddr )(uintptr_t )buf; + +/* Add IOVA->HVA mapping to the IOVA->HVA tree */ + r =vhost_iova_tree_insert (v->shared->iova_tree, &map); +if (unlikely (r !=IOVA_OK )) { +error_report ("Cannot map injected element into IOVA->HVA tree" ); + goto dma_map_err; + } r =vhost_vdpa_dma_map (v->shared, v->address_space_id, map.iova,vhost_vdpa_net_cvq_cmd_page_len (), buf, !write); --2.43.5
Re: [RFC v3 1/5] vhost-vdpa: Decouple the IOVA allocator
Quoted 41 lines · Jonah Palmer
Quoted 14 lines · Eugenio Perez Martin
Quoted 4 lines · Jonah Palmer
Decouples the IOVA allocator from the full
IOVA->HVAtree to support a SVQIOVA->HVAtree for host-only memory mappings.Quoted 8 lines
The IOVA allocator still allocates an IOVA range but instead adds this range to an IOVA-only tree (
iova_map) that keeps track of allocated IOVA ranges for both guest & host-only memory mappings.A new API function
vhost_iova_tree_insert()is also created for addingIOVA->HVAmappings into the SVQIOVA->HVAtree, since the allocator is no longer doing that.Quoted 5 lines
What is the reason for not adding IOVA -> HVA tree on
_allocautomatically? The problematic one is GPA -> HVA, isn't it? Doing this way we force all the allocations to do the two calls (alloc+insert), or the trees will be inconsistent.Ah, I believe you also made a similar comment in RFC v1, saying it wasn't intuitive for the user to follow up with a
vhost_iova_tree_insert()call afterwards (e.g. invhost_vdpa_svq_map_ring()orvhost_vdpa_cvq_map_buf()).I believe what I ended up doing in RFC v2 was creating separate alloc functions for host-only memory mapping (e.g.
vhost_vdpa_svq_map_ring()andvhost_vdpa_cvq_map_buf()) and guest-backed memory mapping (e.g.vhost_vdpa_listener_region_add()).This way, for host-only memory, in the alloc function we allocate an IOVA range (in the IOVA-only tree) and then also inserts the
IOVA->HVAmapping into the SVQIOVA->HVAtree. Similarly, for guest-backed memory, we create its own alloc function (e.g.vhost_iova_tree_map_alloc_gpa()), allocate the IOVA range (in the IOVA-only tree) and then insert theGPA->IOVAmapping into theGPA->IOVAtree.
Quoted 5 lines
This was done so that we didn't have to rely on the user to also call the insertion function after calling the allocation function.
Is this kinda what you're thinking of here?
Right, I think it makes more sense. Do you think differently, maybe I missed any drawbacks?
Quoted 201 lines
Quoted 38 lines
Quoted 38 lines
Signed-off-by Jonah Palmer <[email protected]>--- hw/virtio/vhost-iova-tree.c | 35 +++++++++++++++++++++++++++++++---- hw/virtio/vhost-iova-tree.h | 1 + hw/virtio/vhost-vdpa.c | 21 ++++++++++++++++----- net/vhost-vdpa.c | 13 +++++++++++-- 4 files changed, 59 insertions(+), 11 deletions(-)hw/virtio/vhost-iova-tree.c +6
-2@@ -28,12 +28,15 @@ struct VhostIOVATree {/* IOVA address to qemu memory maps. */ IOVATree *iova_taddr_map; + +/* Allocated IOVA addresses */ +IOVATree *iova_map; };/** -* Create a new IOVA tree +* Create a new VhostIOVATree * -* Returns the new IOVA tree +* Returns the new VhostIOVATree */ VhostIOVATree *vhost_iova_tree_new (hwaddr iova_first,hwaddr iova_last) {@@ -44,15 +47,17 @@ VhostIOVATree *vhost_iova_tree_new(hwaddr iova_first, hwaddr iova_last) tree->iova_last = iova_last; tree->iova_taddr_map =iova_tree_new (); + tree->iova_map =iova_tree_new ();return tree; }Quoted 7 lines
hw/virtio/vhost-iova-tree.c VhostIOVATree *vhost_iova_tree_new(hwaddr iova_first, hwaddr iova_last) · line 55
Quoted 3 lines
hw/virtio/vhost-iova-tree.c +1
-1/** -* Delete an iova tree +* Delete a VhostIOVATree Thanks for fixing the doc of new and delete :) Maybe it is better to put it in an independent patch?
Sure can :)
Quoted 152 lines
Quoted 151 lines
hw/virtio/vhost-iova-tree.c +24
-1*/ void vhost_iova_tree_delete (VhostIOVATree *iova_tree) {iova_tree_destroy (iova_tree->iova_taddr_map); +iova_tree_destroy (iova_tree->iova_map);g_free (iova_tree); }@@ -94,7 +99,7 @@ int vhost_iova_tree_map_alloc(VhostIOVATree *tree, DMAMap *map) }/* Allocate a node in IOVA address */ -return iova_tree_alloc_map (tree->iova_taddr_map, map, iova_first, +return iova_tree_alloc_map (tree->iova_map, map, iova_first, tree->iova_last); }@@ -107,4 +112,26 @@ int vhost_iova_tree_map_alloc(VhostIOVATree *tree, DMAMap *map)void vhost_iova_tree_remove (VhostIOVATree *iova_tree,DMAMap map) {iova_tree_remove (iova_tree->iova_taddr_map, map); +iova_tree_remove (iova_tree->iova_map, map); +} + +/** +* Insert a new mapping to the IOVA->HVA tree +* +* @tree: The VhostIOVATree +* @map: The IOVA->HVA mapping +* +* Returns: +* - IOVA_OK if the map fits in the container +* - IOVA_ERR_INVALID if the map does not make sense (e.g. size overflow) +* - IOVA_ERR_OVERLAP if the IOVA range overlaps with an existing range +*/ +int vhost_iova_tree_insert (VhostIOVATree *iova_tree,DMAMap *map) +{ +if (map->translated_addr + map->size < map->translated_addr || + map->perm ==IOMMU_NONE ) { +return IOVA_ERR_INVALID ; + } + +return iova_tree_insert (iova_tree->iova_taddr_map, map); }hw/virtio/vhost-iova-tree.h +1
-0@@ -23,5 +23,6 @@ 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,DMAMap map); +int vhost_iova_tree_insert (VhostIOVATree *iova_tree,DMAMap *map);#endif hw/virtio/vhost-vdpa.c +16
-5@@ -1142,18 +1142,29 @@ static void vhost_vdpa_svq_unmap_rings(struct vhost_dev *dev,* * @v: Vhost-vdpa device * @needle: The area to search iova +* @taddr: The translated address (SVQ HVA) * @errorp: Error pointer */ static bool vhost_vdpa_svq_map_ring (struct vhost_vdpa *v,DMAMap *needle, -Error **errp) +hwaddr taddr,Error **errp) {int r; +/* Allocate an IOVA range in the IOVA tree */ r =vhost_iova_tree_map_alloc (v->shared->iova_tree, needle);if (unlikely (r !=IOVA_OK )) {error_setg (errp,"Cannot allocate iova (%d)" , r);return false; } + needle->translated_addr = taddr; + +/* Add IOVA->HVA mapping to the IOVA->HVA tree */ + r =vhost_iova_tree_insert (v->shared->iova_tree, needle); +if (unlikely (r !=IOVA_OK )) { +error_setg (errp,"Cannot add SVQ vring mapping (%d)" , r); +vhost_iova_tree_remove (v->shared->iova_tree, *needle); +return false; + } r = vhost_vdpa_dma_map(v->shared, v->address_space_id, needle->iova, needle->size + 1,@@ -1192,11 +1203,11 @@ static bool vhost_vdpa_svq_map_rings(struct vhost_dev *dev,vhost_svq_get_vring_addr (svq, &svq_addr); driver_region = (DMAMap ) { - .translated_addr = svq_addr.desc_user_addr, .size = driver_size -1 , .perm =IOMMU_RO , }; - ok =vhost_vdpa_svq_map_ring (v, &driver_region, errp); + ok =vhost_vdpa_svq_map_ring (v, &driver_region, svq_addr.desc_user_addr, + errp);if (unlikely (!ok)) {error_prepend (errp,"Cannot create vq driver region: " );return false;@@ -1206,11 +1217,11 @@ static bool vhost_vdpa_svq_map_rings(struct vhost_dev *dev, addr->avail_user_addr = driver_region.iova + avail_offset; device_region = (DMAMap ) { - .translated_addr = svq_addr.used_user_addr, .size = device_size -1 , .perm =IOMMU_RW , }; - ok =vhost_vdpa_svq_map_ring (v, &device_region, errp); + ok =vhost_vdpa_svq_map_ring (v, &device_region, svq_addr.used_user_addr, + errp);if (unlikely (!ok)) {error_prepend (errp,"Cannot create vq device region: " );vhost_vdpa_svq_unmap_ring (v, driver_region.translated_addr);net/vhost-vdpa.c +11
-3@@ -512,14 +512,23 @@ static int vhost_vdpa_cvq_map_buf(struct vhost_vdpa *v, void *buf, size_t size,DMAMap map = {};int r; - map.translated_addr = (hwaddr )(uintptr_t )buf; map.size = size -1 ; map.perm = write ?IOMMU_RW :IOMMU_RO , + +/* Allocate an IOVA range in the IOVA tree */ r =vhost_iova_tree_map_alloc (v->shared->iova_tree, &map);if (unlikely (r !=IOVA_OK )) { -error_report ("Cannot map injected element" ); +error_report ("Cannot allocate IOVA range for injected element" );return r; } + map.translated_addr = (hwaddr )(uintptr_t )buf; + +/* Add IOVA->HVA mapping to the IOVA->HVA tree */ + r =vhost_iova_tree_insert (v->shared->iova_tree, &map); +if (unlikely (r !=IOVA_OK )) { +error_report ("Cannot map injected element into IOVA->HVA tree" ); + goto dma_map_err; + } r =vhost_vdpa_dma_map (v->shared, v->address_space_id, map.iova,vhost_vdpa_net_cvq_cmd_page_len (), buf, !write); --2.43.5
Re: [RFC v3 1/5] vhost-vdpa: Decouple the IOVA allocator
Quoted 42 lines · Eugenio Perez Martin
Quoted 41 lines · Jonah Palmer
Quoted 14 lines · Eugenio Perez Martin
Quoted 4 lines · Jonah Palmer
Decouples the IOVA allocator from the full
IOVA->HVAtree to support a SVQIOVA->HVAtree for host-only memory mappings.Quoted 8 lines
The IOVA allocator still allocates an IOVA range but instead adds this range to an IOVA-only tree (
iova_map) that keeps track of allocated IOVA ranges for both guest & host-only memory mappings.A new API function
vhost_iova_tree_insert()is also created for addingIOVA->HVAmappings into the SVQIOVA->HVAtree, since the allocator is no longer doing that.Quoted 5 lines
What is the reason for not adding IOVA -> HVA tree on
_allocautomatically? The problematic one is GPA -> HVA, isn't it? Doing this way we force all the allocations to do the two calls (alloc+insert), or the trees will be inconsistent.Ah, I believe you also made a similar comment in RFC v1, saying it wasn't intuitive for the user to follow up with a
vhost_iova_tree_insert()call afterwards (e.g. invhost_vdpa_svq_map_ring()orvhost_vdpa_cvq_map_buf()).I believe what I ended up doing in RFC v2 was creating separate alloc functions for host-only memory mapping (e.g.
vhost_vdpa_svq_map_ring()andvhost_vdpa_cvq_map_buf()) and guest-backed memory mapping (e.g.vhost_vdpa_listener_region_add()).This way, for host-only memory, in the alloc function we allocate an IOVA range (in the IOVA-only tree) and then also inserts the
IOVA->HVAmapping into the SVQIOVA->HVAtree. Similarly, for guest-backed memory, we create its own alloc function (e.g.vhost_iova_tree_map_alloc_gpa()), allocate the IOVA range (in the IOVA-only tree) and then insert theGPA->IOVAmapping into theGPA->IOVAtree.
Quoted 9 lines
Quoted 5 lines
This was done so that we didn't have to rely on the user to also call the insertion function after calling the allocation function.
Is this kinda what you're thinking of here?
Right, I think it makes more sense. Do you think differently, maybe I missed any drawbacks?
No, I totally think this is fine. I can't think of any serious drawbacks.
Will do in the next series!
Quoted 202 lines
Quoted 201 lines
Quoted 38 lines
Quoted 38 lines
Signed-off-by Jonah Palmer <[email protected]>--- hw/virtio/vhost-iova-tree.c | 35 +++++++++++++++++++++++++++++++---- hw/virtio/vhost-iova-tree.h | 1 + hw/virtio/vhost-vdpa.c | 21 ++++++++++++++++----- net/vhost-vdpa.c | 13 +++++++++++-- 4 files changed, 59 insertions(+), 11 deletions(-)hw/virtio/vhost-iova-tree.c +6
-2@@ -28,12 +28,15 @@ struct VhostIOVATree {/* IOVA address to qemu memory maps. */ IOVATree *iova_taddr_map; + +/* Allocated IOVA addresses */ +IOVATree *iova_map; };/** -* Create a new IOVA tree +* Create a new VhostIOVATree * -* Returns the new IOVA tree +* Returns the new VhostIOVATree */ VhostIOVATree *vhost_iova_tree_new (hwaddr iova_first,hwaddr iova_last) {@@ -44,15 +47,17 @@ VhostIOVATree *vhost_iova_tree_new(hwaddr iova_first, hwaddr iova_last) tree->iova_last = iova_last; tree->iova_taddr_map =iova_tree_new (); + tree->iova_map =iova_tree_new ();return tree; }Quoted 7 lines
hw/virtio/vhost-iova-tree.c VhostIOVATree *vhost_iova_tree_new(hwaddr iova_first, hwaddr iova_last) · line 55
Quoted 3 lines
hw/virtio/vhost-iova-tree.c +1
-1/** -* Delete an iova tree +* Delete a VhostIOVATree Thanks for fixing the doc of new and delete :) Maybe it is better to put it in an independent patch?
Sure can :)
Quoted 152 lines
Quoted 151 lines
hw/virtio/vhost-iova-tree.c +24
-1*/ void vhost_iova_tree_delete (VhostIOVATree *iova_tree) {iova_tree_destroy (iova_tree->iova_taddr_map); +iova_tree_destroy (iova_tree->iova_map);g_free (iova_tree); }@@ -94,7 +99,7 @@ int vhost_iova_tree_map_alloc(VhostIOVATree *tree, DMAMap *map) }/* Allocate a node in IOVA address */ -return iova_tree_alloc_map (tree->iova_taddr_map, map, iova_first, +return iova_tree_alloc_map (tree->iova_map, map, iova_first, tree->iova_last); }@@ -107,4 +112,26 @@ int vhost_iova_tree_map_alloc(VhostIOVATree *tree, DMAMap *map)void vhost_iova_tree_remove (VhostIOVATree *iova_tree,DMAMap map) {iova_tree_remove (iova_tree->iova_taddr_map, map); +iova_tree_remove (iova_tree->iova_map, map); +} + +/** +* Insert a new mapping to the IOVA->HVA tree +* +* @tree: The VhostIOVATree +* @map: The IOVA->HVA mapping +* +* Returns: +* - IOVA_OK if the map fits in the container +* - IOVA_ERR_INVALID if the map does not make sense (e.g. size overflow) +* - IOVA_ERR_OVERLAP if the IOVA range overlaps with an existing range +*/ +int vhost_iova_tree_insert (VhostIOVATree *iova_tree,DMAMap *map) +{ +if (map->translated_addr + map->size < map->translated_addr || + map->perm ==IOMMU_NONE ) { +return IOVA_ERR_INVALID ; + } + +return iova_tree_insert (iova_tree->iova_taddr_map, map); }hw/virtio/vhost-iova-tree.h +1
-0@@ -23,5 +23,6 @@ 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,DMAMap map); +int vhost_iova_tree_insert (VhostIOVATree *iova_tree,DMAMap *map);#endif hw/virtio/vhost-vdpa.c +16
-5@@ -1142,18 +1142,29 @@ static void vhost_vdpa_svq_unmap_rings(struct vhost_dev *dev,* * @v: Vhost-vdpa device * @needle: The area to search iova +* @taddr: The translated address (SVQ HVA) * @errorp: Error pointer */ static bool vhost_vdpa_svq_map_ring (struct vhost_vdpa *v,DMAMap *needle, -Error **errp) +hwaddr taddr,Error **errp) {int r; +/* Allocate an IOVA range in the IOVA tree */ r =vhost_iova_tree_map_alloc (v->shared->iova_tree, needle);if (unlikely (r !=IOVA_OK )) {error_setg (errp,"Cannot allocate iova (%d)" , r);return false; } + needle->translated_addr = taddr; + +/* Add IOVA->HVA mapping to the IOVA->HVA tree */ + r =vhost_iova_tree_insert (v->shared->iova_tree, needle); +if (unlikely (r !=IOVA_OK )) { +error_setg (errp,"Cannot add SVQ vring mapping (%d)" , r); +vhost_iova_tree_remove (v->shared->iova_tree, *needle); +return false; + } r = vhost_vdpa_dma_map(v->shared, v->address_space_id, needle->iova, needle->size + 1,@@ -1192,11 +1203,11 @@ static bool vhost_vdpa_svq_map_rings(struct vhost_dev *dev,vhost_svq_get_vring_addr (svq, &svq_addr); driver_region = (DMAMap ) { - .translated_addr = svq_addr.desc_user_addr, .size = driver_size -1 , .perm =IOMMU_RO , }; - ok =vhost_vdpa_svq_map_ring (v, &driver_region, errp); + ok =vhost_vdpa_svq_map_ring (v, &driver_region, svq_addr.desc_user_addr, + errp);if (unlikely (!ok)) {error_prepend (errp,"Cannot create vq driver region: " );return false;@@ -1206,11 +1217,11 @@ static bool vhost_vdpa_svq_map_rings(struct vhost_dev *dev, addr->avail_user_addr = driver_region.iova + avail_offset; device_region = (DMAMap ) { - .translated_addr = svq_addr.used_user_addr, .size = device_size -1 , .perm =IOMMU_RW , }; - ok =vhost_vdpa_svq_map_ring (v, &device_region, errp); + ok =vhost_vdpa_svq_map_ring (v, &device_region, svq_addr.used_user_addr, + errp);if (unlikely (!ok)) {error_prepend (errp,"Cannot create vq device region: " );vhost_vdpa_svq_unmap_ring (v, driver_region.translated_addr);net/vhost-vdpa.c +11
-3@@ -512,14 +512,23 @@ static int vhost_vdpa_cvq_map_buf(struct vhost_vdpa *v, void *buf, size_t size,DMAMap map = {};int r; - map.translated_addr = (hwaddr )(uintptr_t )buf; map.size = size -1 ; map.perm = write ?IOMMU_RW :IOMMU_RO , + +/* Allocate an IOVA range in the IOVA tree */ r =vhost_iova_tree_map_alloc (v->shared->iova_tree, &map);if (unlikely (r !=IOVA_OK )) { -error_report ("Cannot map injected element" ); +error_report ("Cannot allocate IOVA range for injected element" );return r; } + map.translated_addr = (hwaddr )(uintptr_t )buf; + +/* Add IOVA->HVA mapping to the IOVA->HVA tree */ + r =vhost_iova_tree_insert (v->shared->iova_tree, &map); +if (unlikely (r !=IOVA_OK )) { +error_report ("Cannot map injected element into IOVA->HVA tree" ); + goto dma_map_err; + } r =vhost_vdpa_dma_map (v->shared, v->address_space_id, map.iova,vhost_vdpa_net_cvq_cmd_page_len (), buf, !write); --2.43.5
RFC v3 2/5 vhost-iova-tree: Remove range check for IOVA allocator
In reply to Jonah PalmerRemoves the range check portion in vhost_iova_tree_map_alloc.
The previous patch decoupled the IOVA allocator from adding mappings to the IOVA->HVA tree (now a partial SVQ IOVA->HVA tree) and instead adds the allocated IOVA range to an IOVA-only tree. No value exists under translated_addr for the IOVA-only mappings, so this check is no longer needed.
This check was moved to vhost_iova_tree_insert in the previous patch since that function handles adding IOVA->HVA mappings to the SVQ IOVA->HVA tree.
--- hw/virtio/vhost-iova-tree.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
hw/virtio/vhost-iova-tree.c +1 -2
@@ -93,8 +93,7 @@ int vhost_iova_tree_map_alloc(VhostIOVATree *tree, DMAMap *map)/* Some vhost devices do not like addr 0. Skip first page */ hwaddr iova_first = tree->iova_first ?:qemu_real_host_page_size (); -if (map->translated_addr + map->size < map->translated_addr || - map->perm ==IOMMU_NONE ) { +if (map->perm ==IOMMU_NONE ) {return IOVA_ERR_INVALID ; }
Signature · 1 line
2.43.5
Re: [RFC v3 2/5] vhost-iova-tree: Remove range check for IOVA allocator
Quoted 23 lines · Jonah Palmer
Removes the range check portion in
vhost_iova_tree_map_alloc.The previous patch decoupled the IOVA allocator from adding mappings to the
IOVA->HVAtree (now a partial SVQIOVA->HVAtree) and instead adds the allocated IOVA range to an IOVA-only tree. No value exists undertranslated_addrfor the IOVA-only mappings, so this check is no longer needed.This check was moved to
vhost_iova_tree_insertin the previous patch since that function handles addingIOVA->HVAmappings to the SVQIOVA->HVAtree.Signed-off-by Jonah Palmer <[email protected]>--- hw/virtio/vhost-iova-tree.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)hw/virtio/vhost-iova-tree.c +0
-0
hw/virtio/vhost-iova-tree.c int vhost_iova_tree_map_alloc(VhostIOVATree *tree, DMAMap *map) · line 95
Quoted 3 lines
hw/virtio/vhost-iova-tree.c +0
-0@@ -93,8 +93,7 @@ int vhost_iova_tree_map_alloc(VhostIOVATree *tree, DMAMap *map)/* Some vhost devices do not like addr 0. Skip first page */ hwaddr iova_first = tree->iova_first ?:qemu_real_host_page_size ();
This is not a static function, so I guess it is better to duplicate the check if needed? Otherwise a buggy caller can create invalid entries.
Quoted 9 lines
hw/virtio/vhost-iova-tree.c +1
-3-if (map->translated_addr + map->size < map->translated_addr || - map->perm ==IOMMU_NONE ) { +if (map->perm ==IOMMU_NONE ) {return IOVA_ERR_INVALID ; } --2.43.5
Re: [RFC v3 2/5] vhost-iova-tree: Remove range check for IOVA allocator
Quoted 19 lines · Eugenio Perez Martin
Quoted 18 lines · Jonah Palmer
Removes the range check portion in
vhost_iova_tree_map_alloc.The previous patch decoupled the IOVA allocator from adding mappings to the
IOVA->HVAtree (now a partial SVQIOVA->HVAtree) and instead adds the allocated IOVA range to an IOVA-only tree. No value exists undertranslated_addrfor the IOVA-only mappings, so this check is no longer needed.This check was moved to
vhost_iova_tree_insertin the previous patch since that function handles addingIOVA->HVAmappings to the SVQIOVA->HVAtree.Signed-off-by Jonah Palmer <[email protected]>--- hw/virtio/vhost-iova-tree.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
Quoted 13 lines
hw/virtio/vhost-iova-tree.c int vhost_iova_tree_map_alloc(VhostIOVATree *tree, DMAMap *map) · line 95
Quoted 8 lines
hw/virtio/vhost-iova-tree.c +0
-0@@ -93,8 +93,7 @@ int vhost_iova_tree_map_alloc(VhostIOVATree *tree, DMAMap *map)/* Some vhost devices do not like addr 0. Skip first page */ hwaddr iova_first = tree->iova_first ?:qemu_real_host_page_size ();This is not a static function, so I guess it is better to duplicate the check if needed? Otherwise a buggy caller can create invalid entries.
Gotcha. I can drop this patch then.
Quoted 10 lines
Quoted 9 lines
hw/virtio/vhost-iova-tree.c +1
-3-if (map->translated_addr + map->size < map->translated_addr || - map->perm ==IOMMU_NONE ) { +if (map->perm ==IOMMU_NONE ) {return IOVA_ERR_INVALID ; } --2.43.5
RFC v3 3/5 vhost-vdpa: Implement the GPA->IOVA tree
In reply to Jonah PalmerImplements the GPA->IOVA tree for handling mapping and unmapping for guest memory. This, alongside the SVQ IOVA->HVA tree & IOVA-only tree implemented in the previous patches, allows us to handle guest and host-only memory mapping operations separately via their own respective trees.
The next patches will implement a method to determine if an incomming address for translation is backed by guest or host-only memory.
--- hw/virtio/vhost-iova-tree.c | 50 +++++++++++++++++++++++++++++++++++++ hw/virtio/vhost-iova-tree.h | 4 +++ hw/virtio/vhost-vdpa.c | 22 ++++++++++------ include/qemu/iova-tree.h | 22 ++++++++++++++++ util/iova-tree.c | 46 ++++++++++++++++++++++++++++++++++ 5 files changed, 136 insertions(+), 8 deletions(-)
hw/virtio/vhost-iova-tree.c +50 -0
@@ -31,6 +31,9 @@ struct VhostIOVATree {/* Allocated IOVA addresses */ IOVATree *iova_map; + +/* GPA to IOVA address memory maps */ +IOVATree *gpa_iova_map; };/**
@@ -48,6 +51,7 @@ VhostIOVATree *vhost_iova_tree_new(hwaddr iova_first, hwaddr iova_last) tree->iova_taddr_map =iova_tree_new (); tree->iova_map =iova_tree_new (); + tree->gpa_iova_map =gpa_tree_new ();return tree; }
@@ -58,6 +62,7 @@ void vhost_iova_tree_delete(VhostIOVATree *iova_tree) {iova_tree_destroy (iova_tree->iova_taddr_map);iova_tree_destroy (iova_tree->iova_map); +iova_tree_destroy (iova_tree->gpa_iova_map);g_free (iova_tree); }
@@ -134,3 +139,48 @@ int vhost_iova_tree_insert(VhostIOVATree *iova_tree, DMAMap *map)return iova_tree_insert (iova_tree->iova_taddr_map, map); } + +/** Insert a new GPA->IOVA mapping to the GPA->IOVA tree +* +* @iova_tree: The VhostIOVATree +* @map: The GPA->IOVA mapping +* +* Returns: +* - IOVA_OK if the map fits in the container +* - IOVA_ERR_INVALID if the map does not make sense (e.g. size overflow) +* - IOVA_ERR_OVERLAP if the GPA range overlaps with an existing range +*/ +int vhost_iova_tree_insert_gpa (VhostIOVATree *iova_tree,DMAMap *map) +{ +if (map->iova + map->size < map->iova || map->perm ==IOMMU_NONE ) { +return IOVA_ERR_INVALID ; + } + +return gpa_tree_insert (iova_tree->gpa_iova_map, map); +} + +/** +* Find the IOVA address stored from a guest memory address (GPA) +* +* @tree: The VhostIOVATree +* @map: The map with the guest memory address +* +* Returns the stored GPA->IOVA mapping, or NULL if not found. +*/ +const DMAMap *vhost_iova_tree_find_gpa (const VhostIOVATree *tree, +const DMAMap *map) +{ +return iova_tree_find_iova (tree->gpa_iova_map, map); +} + +/** +* Remove existing mappings from the GPA->IOVA & IOVA trees +* +* @iova_tree: The VhostIOVATree +* @map: The guest memory address map to remove +*/ +void vhost_iova_tree_remove_gpa (VhostIOVATree *iova_tree,DMAMap map) +{ +iova_tree_remove (iova_tree->gpa_iova_map, map); +iova_tree_remove (iova_tree->iova_map, map); +}
hw/virtio/vhost-iova-tree.h +4 -0
@@ -24,5 +24,9 @@ const DMAMap *vhost_iova_tree_find_iova(const VhostIOVATree *iova_tree,int vhost_iova_tree_map_alloc (VhostIOVATree *iova_tree,DMAMap *map);void vhost_iova_tree_remove (VhostIOVATree *iova_tree,DMAMap map);int vhost_iova_tree_insert (VhostIOVATree *iova_tree,DMAMap *map); +int vhost_iova_tree_insert_gpa (VhostIOVATree *iova_tree,DMAMap *map); +const DMAMap *vhost_iova_tree_find_gpa (const VhostIOVATree *iova_tree, +const DMAMap *map); +void vhost_iova_tree_remove_gpa (VhostIOVATree *iova_tree,DMAMap map);#endif
hw/virtio/vhost-vdpa.c +14 -8
@@ -361,10 +361,10 @@ static void vhost_vdpa_listener_region_add(MemoryListener *listener,if (s->shadow_data) {int r; - mem_region.translated_addr = (hwaddr )(uintptr_t )vaddr, mem_region.size =int128_get64 (llsize) -1 , mem_region.perm =IOMMU_ACCESS_FLAG (true, section->readonly), +/* Allocate an IOVA range in the IOVA tree */ r =vhost_iova_tree_map_alloc (s->iova_tree, &mem_region);if (unlikely (r !=IOVA_OK )) {error_report ("Can't allocate a mapping (%d)" , r);
@@ -372,6 +372,14 @@ static void vhost_vdpa_listener_region_add(MemoryListener *listener, } iova = mem_region.iova; + mem_region.translated_addr = section->offset_within_address_space; + +/* Add GPA->IOVA mapping to the GPA->IOVA tree */ + r =vhost_iova_tree_insert_gpa (s->iova_tree, &mem_region); +if (unlikely (r !=IOVA_OK )) { +error_report ("Can't add listener region mapping (%d)" , r); + goto fail_map; + } }vhost_vdpa_iotlb_batch_begin_once (s);
@@ -386,7 +394,7 @@ static void vhost_vdpa_listener_region_add(MemoryListener *listener, fail_map:if (s->shadow_data) { -vhost_iova_tree_remove (s->iova_tree, mem_region); +vhost_iova_tree_remove_gpa (s->iova_tree, mem_region); } fail:
@@ -440,21 +448,19 @@ static void vhost_vdpa_listener_region_del(MemoryListener *listener,if (s->shadow_data) {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 )(uintptr_t )vaddr, + .translated_addr = section->offset_within_address_space, .size =int128_get64 (llsize) -1 , }; - result =vhost_iova_tree_find_iova (s->iova_tree, &mem_region); +/* Search the GPA->IOVA tree */ + result =vhost_iova_tree_find_gpa (s->iova_tree, &mem_region);if (!result) {/* The memory listener map wasn't mapped */ return ; } iova = result->iova; -vhost_iova_tree_remove (s->iova_tree, *result); +vhost_iova_tree_remove_gpa (s->iova_tree, *result); }vhost_vdpa_iotlb_batch_begin_once (s);/*
include/qemu/iova-tree.h +22 -0
@@ -40,6 +40,15 @@ typedef struct DMAMap { }QEMU_PACKED DMAMap;typedef gboolean (*iova_tree_iterator )(DMAMap *map); +/** +* gpa_tree_new: +* +* Create a new GPA->IOVA tree. +* +* Returns: the tree pointer on success, or NULL otherwise. +*/ +IOVATree *gpa_tree_new (void ); +/** * iova_tree_new: *
@@ -49,6 +58,19 @@ typedef gboolean (*iova_tree_iterator)(DMAMap *map);*/ IOVATree *iova_tree_new (void ); +/** +* gpa_tree_insert: +* +* @tree: The GPA->IOVA tree we're inserting the mapping to +* @map: The GPA->IOVA mapping to insert +* +* Inserts a GPA range to the GPA->IOVA tree. If there are overlapped +* ranges, IOVA_ERR_OVERLAP will be returned. +* +* Return: 0 if successful, < 0 otherwise. +*/ +int gpa_tree_insert (IOVATree *tree,const DMAMap *map); +/** * iova_tree_insert: *
util/iova-tree.c +46 -0
64 lines not on this page · raw message
Signature · 1 line
2.43.5
Re: [RFC v3 3/5] vhost-vdpa: Implement the GPA->IOVA tree
Quoted 8 lines · Jonah Palmer
Implements the
GPA->IOVAtree for handling mapping and unmapping for guest memory. This, alongside the SVQIOVA->HVAtree & IOVA-only tree implemented in the previous patches, allows us to handle guest and host-only memory mapping operations separately via their own respective trees.The next patches will implement a method to determine if an incomming
s/incomming/incoming/ (credits to google syntax highlight actually :) )
Quoted 122 lines
address for translation is backed by guest or host-only memory.
Signed-off-by Jonah Palmer <[email protected]>--- hw/virtio/vhost-iova-tree.c | 50 +++++++++++++++++++++++++++++++++++++ hw/virtio/vhost-iova-tree.h | 4 +++ hw/virtio/vhost-vdpa.c | 22 ++++++++++------ include/qemu/iova-tree.h | 22 ++++++++++++++++ util/iova-tree.c | 46 ++++++++++++++++++++++++++++++++++ 5 files changed, 136 insertions(+), 8 deletions(-)hw/virtio/vhost-iova-tree.c +50
-0@@ -31,6 +31,9 @@ struct VhostIOVATree {/* Allocated IOVA addresses */ IOVATree *iova_map; + +/* GPA to IOVA address memory maps */ +IOVATree *gpa_iova_map; };/** @@ -48,6 +51,7 @@ VhostIOVATree *vhost_iova_tree_new(hwaddr iova_first, hwaddr iova_last) tree->iova_taddr_map =iova_tree_new (); tree->iova_map =iova_tree_new (); + tree->gpa_iova_map =gpa_tree_new ();return tree; }@@ -58,6 +62,7 @@ void vhost_iova_tree_delete(VhostIOVATree *iova_tree) {iova_tree_destroy (iova_tree->iova_taddr_map);iova_tree_destroy (iova_tree->iova_map); +iova_tree_destroy (iova_tree->gpa_iova_map);g_free (iova_tree); }@@ -134,3 +139,48 @@ int vhost_iova_tree_insert(VhostIOVATree *iova_tree, DMAMap *map)return iova_tree_insert (iova_tree->iova_taddr_map, map); } + +/** Insert a new GPA->IOVA mapping to the GPA->IOVA tree +* +* @iova_tree: The VhostIOVATree +* @map: The GPA->IOVA mapping +* +* Returns: +* - IOVA_OK if the map fits in the container +* - IOVA_ERR_INVALID if the map does not make sense (e.g. size overflow) +* - IOVA_ERR_OVERLAP if the GPA range overlaps with an existing range +*/ +int vhost_iova_tree_insert_gpa (VhostIOVATree *iova_tree,DMAMap *map) +{ +if (map->iova + map->size < map->iova || map->perm ==IOMMU_NONE ) { +return IOVA_ERR_INVALID ; + } + +return gpa_tree_insert (iova_tree->gpa_iova_map, map); +} + +/** +* Find the IOVA address stored from a guest memory address (GPA) +* +* @tree: The VhostIOVATree +* @map: The map with the guest memory address +* +* Returns the stored GPA->IOVA mapping, or NULL if not found. +*/ +const DMAMap *vhost_iova_tree_find_gpa (const VhostIOVATree *tree, +const DMAMap *map) +{ +return iova_tree_find_iova (tree->gpa_iova_map, map); +} + +/** +* Remove existing mappings from the GPA->IOVA & IOVA trees +* +* @iova_tree: The VhostIOVATree +* @map: The guest memory address map to remove +*/ +void vhost_iova_tree_remove_gpa (VhostIOVATree *iova_tree,DMAMap map) +{ +iova_tree_remove (iova_tree->gpa_iova_map, map); +iova_tree_remove (iova_tree->iova_map, map); +}hw/virtio/vhost-iova-tree.h +4
-0@@ -24,5 +24,9 @@ const DMAMap *vhost_iova_tree_find_iova(const VhostIOVATree *iova_tree,int vhost_iova_tree_map_alloc (VhostIOVATree *iova_tree,DMAMap *map);void vhost_iova_tree_remove (VhostIOVATree *iova_tree,DMAMap map);int vhost_iova_tree_insert (VhostIOVATree *iova_tree,DMAMap *map); +int vhost_iova_tree_insert_gpa (VhostIOVATree *iova_tree,DMAMap *map); +const DMAMap *vhost_iova_tree_find_gpa (const VhostIOVATree *iova_tree, +const DMAMap *map); +void vhost_iova_tree_remove_gpa (VhostIOVATree *iova_tree,DMAMap map);#endif hw/virtio/vhost-vdpa.c +1
-1@@ -361,10 +361,10 @@ static void vhost_vdpa_listener_region_add(MemoryListener *listener,if (s->shadow_data) {int r; - mem_region.translated_addr = (hwaddr )(uintptr_t )vaddr, mem_region.size =int128_get64 (llsize) -1 , mem_region.perm =IOMMU_ACCESS_FLAG (true, section->readonly), +/* Allocate an IOVA range in the IOVA tree */ r =vhost_iova_tree_map_alloc (s->iova_tree, &mem_region);if (unlikely (r !=IOVA_OK )) {error_report ("Can't allocate a mapping (%d)" , r);@@ -372,6 +372,14 @@ static void vhost_vdpa_listener_region_add(MemoryListener *listener, }
hw/virtio/vhost-vdpa.c static void vhost_vdpa_listener_region_add(MemoryListener *listener, · line 382
Quoted 10 lines
hw/virtio/vhost-vdpa.c +8
-0iova = mem_region.iova; + mem_region.translated_addr = section->offset_within_address_space; + +/* Add GPA->IOVA mapping to the GPA->IOVA tree */ + r =vhost_iova_tree_insert_gpa (s->iova_tree, &mem_region); +if (unlikely (r !=IOVA_OK )) { +error_report ("Can't add listener region mapping (%d)" , r); + goto fail_map; + }
If we want to make the two disjoint trees, we need to make the previous commits working. I mean, either insert hva and then switch to gpa here, or merge patches, or something similar. Otherwise, bisection breaks.
Quoted 136 lines
hw/virtio/vhost-vdpa.c +5
-7}vhost_vdpa_iotlb_batch_begin_once (s);@@ -386,7 +394,7 @@ static void vhost_vdpa_listener_region_add(MemoryListener *listener, fail_map:if (s->shadow_data) { -vhost_iova_tree_remove (s->iova_tree, mem_region); +vhost_iova_tree_remove_gpa (s->iova_tree, mem_region); } fail:@@ -440,21 +448,19 @@ static void vhost_vdpa_listener_region_del(MemoryListener *listener,if (s->shadow_data) {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 )(uintptr_t )vaddr, + .translated_addr = section->offset_within_address_space, .size =int128_get64 (llsize) -1 , }; - result =vhost_iova_tree_find_iova (s->iova_tree, &mem_region); +/* Search the GPA->IOVA tree */ + result =vhost_iova_tree_find_gpa (s->iova_tree, &mem_region);if (!result) {/* The memory listener map wasn't mapped */ return ; } iova = result->iova; -vhost_iova_tree_remove (s->iova_tree, *result); +vhost_iova_tree_remove_gpa (s->iova_tree, *result); }vhost_vdpa_iotlb_batch_begin_once (s);/* include/qemu/iova-tree.h +22
-0@@ -40,6 +40,15 @@ typedef struct DMAMap { }QEMU_PACKED DMAMap;typedef gboolean (*iova_tree_iterator )(DMAMap *map); +/** +* gpa_tree_new: +* +* Create a new GPA->IOVA tree. +* +* Returns: the tree pointer on success, or NULL otherwise. +*/ +IOVATree *gpa_tree_new (void ); +/** * iova_tree_new: * @@ -49,6 +58,19 @@ typedef gboolean (*iova_tree_iterator)(DMAMap *map);*/ IOVATree *iova_tree_new (void ); +/** +* gpa_tree_insert: +* +* @tree: The GPA->IOVA tree we're inserting the mapping to +* @map: The GPA->IOVA mapping to insert +* +* Inserts a GPA range to the GPA->IOVA tree. If there are overlapped +* ranges, IOVA_ERR_OVERLAP will be returned. +* +* Return: 0 if successful, < 0 otherwise. +*/ +int gpa_tree_insert (IOVATree *tree,const DMAMap *map); +/** * iova_tree_insert: * util/iova-tree.c +36
-0@@ -55,6 +55,22 @@ static void iova_tree_alloc_args_iterate(struct IOVATreeAllocArgs *args, args->this = next; } +static int gpa_tree_compare (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 ; +} +static int iova_tree_compare (gconstpointer a,gconstpointer b,gpointer data) {const DMAMap *m1 = a, *m2 = b;@@ -71,6 +87,15 @@ static int iova_tree_compare(gconstpointer a, gconstpointer b, gpointer data)return 0 ; } +IOVATree *gpa_tree_new (void ) +{ +IOVATree *gpa_tree =g_new0 (IOVATree,1 ); + + gpa_tree->tree =g_tree_new_full (gpa_tree_compare,NULL , g_free,NULL ); + +return gpa_tree; +} +IOVATree *iova_tree_new (void ) {IOVATree *iova_tree =g_new0 (IOVATree,1 );@@ -121,6 +146,27 @@ static inline void iova_tree_insert_internal(GTree *gtree, DMAMap *range)g_tree_insert (gtree, range, range); } +int gpa_tree_insert (IOVATree *tree,const DMAMap *map) +{ +DMAMap *new; + +if (map->translated_addr + map->size < map->translated_addr || + map->perm ==IOMMU_NONE ) { +return IOVA_ERR_INVALID ; + } + +/* We don't allow inserting ranges that overlap with existing ones */ +if (iova_tree_find (tree,map)) {
util/iova-tree.c static inline void iova_tree_insert_internal(GTree *gtree, DMAMap *range) · line 169
Quoted 10 lines
util/iova-tree.c +10
-0+return IOVA_ERR_OVERLAP ; + } + + new =g_new0 (DMAMap,1 ); +memcpy (new, map,sizeof (*new)); +iova_tree_insert_internal (tree->tree, new); + +return IOVA_OK ; +} +
I'm missing the advantage of using all of these functions, why not use another iova_tree_new and iova_tree_insert? gpa_tree_compare seems like a 1:1 copy of iova_tree_compare to me. Same with _insert.
Quoted 6 lines
util/iova-tree.c +0
-1int iova_tree_insert (IOVATree *tree,const DMAMap *map) {DMAMap *new; --2.43.5
Re: [RFC v3 3/5] vhost-vdpa: Implement the GPA->IOVA tree
Quoted 8 lines · Eugenio Perez Martin
Quoted 7 lines · Jonah Palmer
Implements the
GPA->IOVAtree for handling mapping and unmapping for guest memory. This, alongside the SVQIOVA->HVAtree & IOVA-only tree implemented in the previous patches, allows us to handle guest and host-only memory mapping operations separately via their own respective trees.
Quoted 4 lines
Quoted 1 line
The next patches will implement a method to determine if an incomming
s/incomming/incoming/ (credits to google syntax highlight actually :) )
Whoops! Good catch. Maybe I should add a spellchecker plugin to my Vim configuration haha.
Quoted 123 lines
Quoted 123 lines
address for translation is backed by guest or host-only memory.
Signed-off-by Jonah Palmer <[email protected]>--- hw/virtio/vhost-iova-tree.c | 50 +++++++++++++++++++++++++++++++++++++ hw/virtio/vhost-iova-tree.h | 4 +++ hw/virtio/vhost-vdpa.c | 22 ++++++++++------ include/qemu/iova-tree.h | 22 ++++++++++++++++ util/iova-tree.c | 46 ++++++++++++++++++++++++++++++++++ 5 files changed, 136 insertions(+), 8 deletions(-)hw/virtio/vhost-iova-tree.c +50
-0@@ -31,6 +31,9 @@ struct VhostIOVATree {/* Allocated IOVA addresses */ IOVATree *iova_map; + +/* GPA to IOVA address memory maps */ +IOVATree *gpa_iova_map; };/** @@ -48,6 +51,7 @@ VhostIOVATree *vhost_iova_tree_new(hwaddr iova_first, hwaddr iova_last) tree->iova_taddr_map =iova_tree_new (); tree->iova_map =iova_tree_new (); + tree->gpa_iova_map =gpa_tree_new ();return tree; }@@ -58,6 +62,7 @@ void vhost_iova_tree_delete(VhostIOVATree *iova_tree) {iova_tree_destroy (iova_tree->iova_taddr_map);iova_tree_destroy (iova_tree->iova_map); +iova_tree_destroy (iova_tree->gpa_iova_map);g_free (iova_tree); }@@ -134,3 +139,48 @@ int vhost_iova_tree_insert(VhostIOVATree *iova_tree, DMAMap *map)return iova_tree_insert (iova_tree->iova_taddr_map, map); } + +/** Insert a new GPA->IOVA mapping to the GPA->IOVA tree +* +* @iova_tree: The VhostIOVATree +* @map: The GPA->IOVA mapping +* +* Returns: +* - IOVA_OK if the map fits in the container +* - IOVA_ERR_INVALID if the map does not make sense (e.g. size overflow) +* - IOVA_ERR_OVERLAP if the GPA range overlaps with an existing range +*/ +int vhost_iova_tree_insert_gpa (VhostIOVATree *iova_tree,DMAMap *map) +{ +if (map->iova + map->size < map->iova || map->perm ==IOMMU_NONE ) { +return IOVA_ERR_INVALID ; + } + +return gpa_tree_insert (iova_tree->gpa_iova_map, map); +} + +/** +* Find the IOVA address stored from a guest memory address (GPA) +* +* @tree: The VhostIOVATree +* @map: The map with the guest memory address +* +* Returns the stored GPA->IOVA mapping, or NULL if not found. +*/ +const DMAMap *vhost_iova_tree_find_gpa (const VhostIOVATree *tree, +const DMAMap *map) +{ +return iova_tree_find_iova (tree->gpa_iova_map, map); +} + +/** +* Remove existing mappings from the GPA->IOVA & IOVA trees +* +* @iova_tree: The VhostIOVATree +* @map: The guest memory address map to remove +*/ +void vhost_iova_tree_remove_gpa (VhostIOVATree *iova_tree,DMAMap map) +{ +iova_tree_remove (iova_tree->gpa_iova_map, map); +iova_tree_remove (iova_tree->iova_map, map); +}hw/virtio/vhost-iova-tree.h +4
-0@@ -24,5 +24,9 @@ const DMAMap *vhost_iova_tree_find_iova(const VhostIOVATree *iova_tree,int vhost_iova_tree_map_alloc (VhostIOVATree *iova_tree,DMAMap *map);void vhost_iova_tree_remove (VhostIOVATree *iova_tree,DMAMap map);int vhost_iova_tree_insert (VhostIOVATree *iova_tree,DMAMap *map); +int vhost_iova_tree_insert_gpa (VhostIOVATree *iova_tree,DMAMap *map); +const DMAMap *vhost_iova_tree_find_gpa (const VhostIOVATree *iova_tree, +const DMAMap *map); +void vhost_iova_tree_remove_gpa (VhostIOVATree *iova_tree,DMAMap map);#endif hw/virtio/vhost-vdpa.c +1
-1@@ -361,10 +361,10 @@ static void vhost_vdpa_listener_region_add(MemoryListener *listener,if (s->shadow_data) {int r; - mem_region.translated_addr = (hwaddr )(uintptr_t )vaddr, mem_region.size =int128_get64 (llsize) -1 , mem_region.perm =IOMMU_ACCESS_FLAG (true, section->readonly), +/* Allocate an IOVA range in the IOVA tree */ r =vhost_iova_tree_map_alloc (s->iova_tree, &mem_region);if (unlikely (r !=IOVA_OK )) {error_report ("Can't allocate a mapping (%d)" , r);@@ -372,6 +372,14 @@ static void vhost_vdpa_listener_region_add(MemoryListener *listener, }
Quoted 16 lines
hw/virtio/vhost-vdpa.c static void vhost_vdpa_listener_region_add(MemoryListener *listener, · line 382
Quoted 9 lines
hw/virtio/vhost-vdpa.c +8
-0iova = mem_region.iova; + mem_region.translated_addr = section->offset_within_address_space; + +/* Add GPA->IOVA mapping to the GPA->IOVA tree */ + r =vhost_iova_tree_insert_gpa (s->iova_tree, &mem_region); +if (unlikely (r !=IOVA_OK )) { +error_report ("Can't add listener region mapping (%d)" , r); + goto fail_map; + }If we want to make the two disjoint trees, we need to make the previous commits working. I mean, either insert hva and then switch to gpa here, or merge patches, or something similar. Otherwise, bisection breaks.
Gotcha. In the future I'll be more careful and make sure individual patches in a series actually work. My apologies.
Will fix this in the next series.
Quoted 142 lines
Quoted 142 lines
hw/virtio/vhost-vdpa.c +5
-7}vhost_vdpa_iotlb_batch_begin_once (s);@@ -386,7 +394,7 @@ static void vhost_vdpa_listener_region_add(MemoryListener *listener, fail_map:if (s->shadow_data) { -vhost_iova_tree_remove (s->iova_tree, mem_region); +vhost_iova_tree_remove_gpa (s->iova_tree, mem_region); } fail:@@ -440,21 +448,19 @@ static void vhost_vdpa_listener_region_del(MemoryListener *listener,if (s->shadow_data) {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 )(uintptr_t )vaddr, + .translated_addr = section->offset_within_address_space, .size =int128_get64 (llsize) -1 , }; - result =vhost_iova_tree_find_iova (s->iova_tree, &mem_region); +/* Search the GPA->IOVA tree */ + result =vhost_iova_tree_find_gpa (s->iova_tree, &mem_region);if (!result) {/* The memory listener map wasn't mapped */ return ; } iova = result->iova; -vhost_iova_tree_remove (s->iova_tree, *result); +vhost_iova_tree_remove_gpa (s->iova_tree, *result); }vhost_vdpa_iotlb_batch_begin_once (s);/* include/qemu/iova-tree.h +22
-0@@ -40,6 +40,15 @@ typedef struct DMAMap { }QEMU_PACKED DMAMap;typedef gboolean (*iova_tree_iterator )(DMAMap *map); +/** +* gpa_tree_new: +* +* Create a new GPA->IOVA tree. +* +* Returns: the tree pointer on success, or NULL otherwise. +*/ +IOVATree *gpa_tree_new (void ); +/** * iova_tree_new: * @@ -49,6 +58,19 @@ typedef gboolean (*iova_tree_iterator)(DMAMap *map);*/ IOVATree *iova_tree_new (void ); +/** +* gpa_tree_insert: +* +* @tree: The GPA->IOVA tree we're inserting the mapping to +* @map: The GPA->IOVA mapping to insert +* +* Inserts a GPA range to the GPA->IOVA tree. If there are overlapped +* ranges, IOVA_ERR_OVERLAP will be returned. +* +* Return: 0 if successful, < 0 otherwise. +*/ +int gpa_tree_insert (IOVATree *tree,const DMAMap *map); +/** * iova_tree_insert: * util/iova-tree.c +42
-0@@ -55,6 +55,22 @@ static void iova_tree_alloc_args_iterate(struct IOVATreeAllocArgs *args, args->this = next; } +static int gpa_tree_compare (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 ; +} +static int iova_tree_compare (gconstpointer a,gconstpointer b,gpointer data) {const DMAMap *m1 = a, *m2 = b;@@ -71,6 +87,15 @@ static int iova_tree_compare(gconstpointer a, gconstpointer b, gpointer data)return 0 ; } +IOVATree *gpa_tree_new (void ) +{ +IOVATree *gpa_tree =g_new0 (IOVATree,1 ); + + gpa_tree->tree =g_tree_new_full (gpa_tree_compare,NULL , g_free,NULL ); + +return gpa_tree; +} +IOVATree *iova_tree_new (void ) {IOVATree *iova_tree =g_new0 (IOVATree,1 );@@ -121,6 +146,27 @@ static inline void iova_tree_insert_internal(GTree *gtree, DMAMap *range)g_tree_insert (gtree, range, range); } +int gpa_tree_insert (IOVATree *tree,const DMAMap *map) +{ +DMAMap *new; + +if (map->translated_addr + map->size < map->translated_addr || + map->perm ==IOMMU_NONE ) { +return IOVA_ERR_INVALID ; + } + +/* We don't allow inserting ranges that overlap with existing ones */ +if (iova_tree_find (tree,map)) { +return IOVA_ERR_OVERLAP ; + } + + new =g_new0 (DMAMap,1 ); +memcpy (new, map,sizeof (*new)); +iova_tree_insert_internal (tree->tree, new);
Quoted 9 lines
util/iova-tree.c static inline void iova_tree_insert_internal(GTree *gtree, DMAMap *range) · line 169
Quoted 4 lines
util/iova-tree.c +4
-0+ +return IOVA_OK ; +} +I'm missing the advantage of using all of these functions, why not use another
iova_tree_newandiova_tree_insert?gpa_tree_compareseems like a 1:1 copy ofiova_tree_compareto me. Same with_insert.
It's mainly due to how the GPA tree is configured. That is, GPA->IOVA (or translated_addr->iova).
The existing functions assume the configuration of the tree to be iova->translated_addr (e.g. IOVA->HVA).
I think I could still use the existing functions but then it'd have to be a IOVA->GPA tree, else a GPA->IOVA tree would mean DMAMap map->iova == GPA and map->translated_addr == IOVA, which obviously would cause confusion for the API users.
Quoted 7 lines
Quoted 6 lines
util/iova-tree.c +0
-1int iova_tree_insert (IOVATree *tree,const DMAMap *map) {DMAMap *new; --2.43.5
Re: [RFC v3 3/5] vhost-vdpa: Implement the GPA->IOVA tree
Quoted 160 lines · Jonah Palmer
Quoted 8 lines · Eugenio Perez Martin
Quoted 7 lines · Jonah Palmer
Implements the
GPA->IOVAtree for handling mapping and unmapping for guest memory. This, alongside the SVQIOVA->HVAtree & IOVA-only tree implemented in the previous patches, allows us to handle guest and host-only memory mapping operations separately via their own respective trees.Quoted 4 lines
Quoted 1 line
The next patches will implement a method to determine if an incomming
s/incomming/incoming/ (credits to google syntax highlight actually :) )
Whoops! Good catch. Maybe I should add a spellchecker plugin to my Vim configuration haha.
Quoted 123 lines
Quoted 123 lines
address for translation is backed by guest or host-only memory.
Signed-off-by Jonah Palmer <[email protected]>--- hw/virtio/vhost-iova-tree.c | 50 +++++++++++++++++++++++++++++++++++++ hw/virtio/vhost-iova-tree.h | 4 +++ hw/virtio/vhost-vdpa.c | 22 ++++++++++------ include/qemu/iova-tree.h | 22 ++++++++++++++++ util/iova-tree.c | 46 ++++++++++++++++++++++++++++++++++ 5 files changed, 136 insertions(+), 8 deletions(-)hw/virtio/vhost-iova-tree.c +50
-0@@ -31,6 +31,9 @@ struct VhostIOVATree {/* Allocated IOVA addresses */ IOVATree *iova_map; + +/* GPA to IOVA address memory maps */ +IOVATree *gpa_iova_map; };/** @@ -48,6 +51,7 @@ VhostIOVATree *vhost_iova_tree_new(hwaddr iova_first, hwaddr iova_last) tree->iova_taddr_map =iova_tree_new (); tree->iova_map =iova_tree_new (); + tree->gpa_iova_map =gpa_tree_new ();return tree; }@@ -58,6 +62,7 @@ void vhost_iova_tree_delete(VhostIOVATree *iova_tree) {iova_tree_destroy (iova_tree->iova_taddr_map);iova_tree_destroy (iova_tree->iova_map); +iova_tree_destroy (iova_tree->gpa_iova_map);g_free (iova_tree); }@@ -134,3 +139,48 @@ int vhost_iova_tree_insert(VhostIOVATree *iova_tree, DMAMap *map)return iova_tree_insert (iova_tree->iova_taddr_map, map); } + +/** Insert a new GPA->IOVA mapping to the GPA->IOVA tree +* +* @iova_tree: The VhostIOVATree +* @map: The GPA->IOVA mapping +* +* Returns: +* - IOVA_OK if the map fits in the container +* - IOVA_ERR_INVALID if the map does not make sense (e.g. size overflow) +* - IOVA_ERR_OVERLAP if the GPA range overlaps with an existing range +*/ +int vhost_iova_tree_insert_gpa (VhostIOVATree *iova_tree,DMAMap *map) +{ +if (map->iova + map->size < map->iova || map->perm ==IOMMU_NONE ) { +return IOVA_ERR_INVALID ; + } + +return gpa_tree_insert (iova_tree->gpa_iova_map, map); +} + +/** +* Find the IOVA address stored from a guest memory address (GPA) +* +* @tree: The VhostIOVATree +* @map: The map with the guest memory address +* +* Returns the stored GPA->IOVA mapping, or NULL if not found. +*/ +const DMAMap *vhost_iova_tree_find_gpa (const VhostIOVATree *tree, +const DMAMap *map) +{ +return iova_tree_find_iova (tree->gpa_iova_map, map); +} + +/** +* Remove existing mappings from the GPA->IOVA & IOVA trees +* +* @iova_tree: The VhostIOVATree +* @map: The guest memory address map to remove +*/ +void vhost_iova_tree_remove_gpa (VhostIOVATree *iova_tree,DMAMap map) +{ +iova_tree_remove (iova_tree->gpa_iova_map, map); +iova_tree_remove (iova_tree->iova_map, map); +}hw/virtio/vhost-iova-tree.h +4
-0@@ -24,5 +24,9 @@ const DMAMap *vhost_iova_tree_find_iova(const VhostIOVATree *iova_tree,int vhost_iova_tree_map_alloc (VhostIOVATree *iova_tree,DMAMap *map);void vhost_iova_tree_remove (VhostIOVATree *iova_tree,DMAMap map);int vhost_iova_tree_insert (VhostIOVATree *iova_tree,DMAMap *map); +int vhost_iova_tree_insert_gpa (VhostIOVATree *iova_tree,DMAMap *map); +const DMAMap *vhost_iova_tree_find_gpa (const VhostIOVATree *iova_tree, +const DMAMap *map); +void vhost_iova_tree_remove_gpa (VhostIOVATree *iova_tree,DMAMap map);#endif hw/virtio/vhost-vdpa.c +1
-1@@ -361,10 +361,10 @@ static void vhost_vdpa_listener_region_add(MemoryListener *listener,if (s->shadow_data) {int r; - mem_region.translated_addr = (hwaddr )(uintptr_t )vaddr, mem_region.size =int128_get64 (llsize) -1 , mem_region.perm =IOMMU_ACCESS_FLAG (true, section->readonly), +/* Allocate an IOVA range in the IOVA tree */ r =vhost_iova_tree_map_alloc (s->iova_tree, &mem_region);if (unlikely (r !=IOVA_OK )) {error_report ("Can't allocate a mapping (%d)" , r);@@ -372,6 +372,14 @@ static void vhost_vdpa_listener_region_add(MemoryListener *listener, }Quoted 16 lines
hw/virtio/vhost-vdpa.c static void vhost_vdpa_listener_region_add(MemoryListener *listener, · line 382
Quoted 9 lines
hw/virtio/vhost-vdpa.c +8
-0iova = mem_region.iova; + mem_region.translated_addr = section->offset_within_address_space; + +/* Add GPA->IOVA mapping to the GPA->IOVA tree */ + r =vhost_iova_tree_insert_gpa (s->iova_tree, &mem_region); +if (unlikely (r !=IOVA_OK )) { +error_report ("Can't add listener region mapping (%d)" , r); + goto fail_map; + }If we want to make the two disjoint trees, we need to make the previous commits working. I mean, either insert hva and then switch to gpa here, or merge patches, or something similar. Otherwise, bisection breaks.
Quoted 5 lines
Gotcha. In the future I'll be more careful and make sure individual patches in a series actually work. My apologies.
Will fix this in the next series.
Great!
Quoted 155 lines
Quoted 142 lines
Quoted 142 lines
hw/virtio/vhost-vdpa.c +5
-7}vhost_vdpa_iotlb_batch_begin_once (s);@@ -386,7 +394,7 @@ static void vhost_vdpa_listener_region_add(MemoryListener *listener, fail_map:if (s->shadow_data) { -vhost_iova_tree_remove (s->iova_tree, mem_region); +vhost_iova_tree_remove_gpa (s->iova_tree, mem_region); } fail:@@ -440,21 +448,19 @@ static void vhost_vdpa_listener_region_del(MemoryListener *listener,if (s->shadow_data) {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 )(uintptr_t )vaddr, + .translated_addr = section->offset_within_address_space, .size =int128_get64 (llsize) -1 , }; - result =vhost_iova_tree_find_iova (s->iova_tree, &mem_region); +/* Search the GPA->IOVA tree */ + result =vhost_iova_tree_find_gpa (s->iova_tree, &mem_region);if (!result) {/* The memory listener map wasn't mapped */ return ; } iova = result->iova; -vhost_iova_tree_remove (s->iova_tree, *result); +vhost_iova_tree_remove_gpa (s->iova_tree, *result); }vhost_vdpa_iotlb_batch_begin_once (s);/* include/qemu/iova-tree.h +22
-0@@ -40,6 +40,15 @@ typedef struct DMAMap { }QEMU_PACKED DMAMap;typedef gboolean (*iova_tree_iterator )(DMAMap *map); +/** +* gpa_tree_new: +* +* Create a new GPA->IOVA tree. +* +* Returns: the tree pointer on success, or NULL otherwise. +*/ +IOVATree *gpa_tree_new (void ); +/** * iova_tree_new: * @@ -49,6 +58,19 @@ typedef gboolean (*iova_tree_iterator)(DMAMap *map);*/ IOVATree *iova_tree_new (void ); +/** +* gpa_tree_insert: +* +* @tree: The GPA->IOVA tree we're inserting the mapping to +* @map: The GPA->IOVA mapping to insert +* +* Inserts a GPA range to the GPA->IOVA tree. If there are overlapped +* ranges, IOVA_ERR_OVERLAP will be returned. +* +* Return: 0 if successful, < 0 otherwise. +*/ +int gpa_tree_insert (IOVATree *tree,const DMAMap *map); +/** * iova_tree_insert: * util/iova-tree.c +42
-0@@ -55,6 +55,22 @@ static void iova_tree_alloc_args_iterate(struct IOVATreeAllocArgs *args, args->this = next; } +static int gpa_tree_compare (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 ; +} +static int iova_tree_compare (gconstpointer a,gconstpointer b,gpointer data) {const DMAMap *m1 = a, *m2 = b;@@ -71,6 +87,15 @@ static int iova_tree_compare(gconstpointer a, gconstpointer b, gpointer data)return 0 ; } +IOVATree *gpa_tree_new (void ) +{ +IOVATree *gpa_tree =g_new0 (IOVATree,1 ); + + gpa_tree->tree =g_tree_new_full (gpa_tree_compare,NULL , g_free,NULL ); + +return gpa_tree; +} +IOVATree *iova_tree_new (void ) {IOVATree *iova_tree =g_new0 (IOVATree,1 );@@ -121,6 +146,27 @@ static inline void iova_tree_insert_internal(GTree *gtree, DMAMap *range)g_tree_insert (gtree, range, range); } +int gpa_tree_insert (IOVATree *tree,const DMAMap *map) +{ +DMAMap *new; + +if (map->translated_addr + map->size < map->translated_addr || + map->perm ==IOMMU_NONE ) { +return IOVA_ERR_INVALID ; + } + +/* We don't allow inserting ranges that overlap with existing ones */ +if (iova_tree_find (tree,map)) { +return IOVA_ERR_OVERLAP ; + } + + new =g_new0 (DMAMap,1 ); +memcpy (new, map,sizeof (*new)); +iova_tree_insert_internal (tree->tree, new);Quoted 9 lines
util/iova-tree.c static inline void iova_tree_insert_internal(GTree *gtree, DMAMap *range) · line 169
Quoted 4 lines
util/iova-tree.c +4
-0+ +return IOVA_OK ; +} +I'm missing the advantage of using all of these functions, why not use another
iova_tree_newandiova_tree_insert?gpa_tree_compareseems like a 1:1 copy ofiova_tree_compareto me. Same with_insert.It's mainly due to how the GPA tree is configured. That is,
GPA->IOVA(ortranslated_addr->iova).
Quoted 8 lines
The existing functions assume the configuration of the tree to be
iova->translated_addr(e.g.IOVA->HVA).I think I could still use the existing functions but then it'd have to be a
IOVA->GPAtree, else aGPA->IOVAtree would mean DMAMapmap->iova== GPA andmap->translated_addr== IOVA, which obviously would cause confusion for the API users.
Ook now I remember having this same doubt, a very good point :).
In my opinion it is enough with a comment in the VhostIOVAtree struct definition as long as the usage keeps internal to it and well wrapped in the non static functions. MST, Jason, any thoughts on this?
If we ever want to reduce the memory consumption of the tree we can also reuse the same node for both trees and reuse these functions.
RFC v3 4/5 virtio: add in_xlat_addr & out_xlat_addr VirtQueueElement members
In reply to Jonah PalmerAdds the in_xlat_addr & out_xlat_addr hwaddr arrays to the VirtQueueElement struct and introduces an optional GPA output parameter to dma_memory_map().
These arrays will store a VirtQueueElement's input/output descriptors' GPA of the mapped memory region, if it's backed by guest memory, via dma_memory_map().
The GPA will always correspond 1:1 to the iovec entry when translating addresses between Qemu VAs and SVQ IOVAs in vhost_svq_translate_addr(). This helps to avoid extra complicated code in SVQ's vhost_svq_vring_write_descs() function (e.g. splitting up iovec into multiple buffers, not breaking devices using aliased mapping, etc.).
Since the translation is only done once inside the DMA API alongside virtqueue_pop(), the cost should be minimal.
--- hw/display/virtio-gpu.c | 5 ++-- hw/hyperv/vmbus.c | 8 +++--- hw/ide/ahci.c | 7 +++--- hw/usb/libhw.c | 2 +- hw/virtio/virtio.c | 50 ++++++++++++++++++++++++++----------- include/hw/pci/pci_device.h | 2 +- include/hw/virtio/virtio.h | 2 ++ include/system/dma.h | 25 ++++++++++++++++++- system/dma-helpers.c | 2 +- 9 files changed, 77 insertions(+), 26 deletions(-)
hw/display/virtio-gpu.c +3 -2
@@ -839,7 +839,7 @@ int virtio_gpu_create_mapping_iov(VirtIOGPU *g, len = l; map =dma_memory_map (VIRTIO_DEVICE (g)->dma_as, a, &len,DMA_DIRECTION_TO_DEVICE , -MEMTXATTRS_UNSPECIFIED ); +MEMTXATTRS_UNSPECIFIED ,NULL );if (!map) {qemu_log_mask (LOG_GUEST_ERROR ,"%s: failed to map MMIO memory for" " element %d\n" , __func__, e);
@@ -1258,7 +1258,8 @@ static bool virtio_gpu_load_restore_mapping(VirtIOGPU *g,hwaddr len = res->iov[i].iov_len; res->iov[i].iov_base =dma_memory_map (VIRTIO_DEVICE (g)->dma_as, res->addrs[i], &len, -DMA_DIRECTION_TO_DEVICE ,MEMTXATTRS_UNSPECIFIED ); +DMA_DIRECTION_TO_DEVICE ,MEMTXATTRS_UNSPECIFIED , +NULL );if (!res->iov[i].iov_base || len != res->iov[i].iov_len) {/* Clean up the half-a-mapping we just created... */
hw/hyperv/vmbus.c +5 -3
@@ -374,7 +374,7 @@ static ssize_t gpadl_iter_io(GpadlIter *iter, void *buf, uint32_t len) maddr = (iter->gpadl->gfns[idx] <<TARGET_PAGE_BITS ) | off_in_page; iter->map =dma_memory_map (iter->as, maddr, &mlen, iter->dir, -MEMTXATTRS_UNSPECIFIED ); +MEMTXATTRS_UNSPECIFIED ,NULL );if (mlen != pgleft) {dma_memory_unmap (iter->as, iter->map, mlen, iter->dir,0 ); iter->map =NULL ;
@@ -492,7 +492,8 @@ int vmbus_map_sgl(VMBusChanReq *req, DMADirection dir, struct iovec *iov, } iov[ret_cnt].iov_base =dma_memory_map (sgl->as, a, &l, dir, -MEMTXATTRS_UNSPECIFIED ); +MEMTXATTRS_UNSPECIFIED , +NULL );if (!l) { ret = -EFAULT ; goto err;
@@ -568,7 +569,8 @@ static vmbus_ring_buffer *ringbuf_map_hdr(VMBusRingBufCommon *ringbuf)dma_addr_t mlen =sizeof (*rb); rb =dma_memory_map (ringbuf->as, ringbuf->rb_addr, &mlen, -DMA_DIRECTION_FROM_DEVICE ,MEMTXATTRS_UNSPECIFIED ); +DMA_DIRECTION_FROM_DEVICE ,MEMTXATTRS_UNSPECIFIED , +NULL );if (mlen !=sizeof (*rb)) {dma_memory_unmap (ringbuf->as, rb, mlen,DMA_DIRECTION_FROM_DEVICE ,0 );
hw/ide/ahci.c +4 -3
@@ -221,7 +221,7 @@ static void map_page(AddressSpace *as, uint8_t **ptr, uint64_t addr, } *ptr =dma_memory_map (as, addr, &len,DMA_DIRECTION_FROM_DEVICE , -MEMTXATTRS_UNSPECIFIED ); +MEMTXATTRS_UNSPECIFIED ,NULL );if (len < wanted && *ptr) {dma_memory_unmap (as, *ptr, len,DMA_DIRECTION_FROM_DEVICE , len); *ptr =NULL ;
@@ -928,7 +928,7 @@ static int ahci_populate_sglist(AHCIDevice *ad, QEMUSGList *sglist,/* map PRDT */ if (!(prdt =dma_memory_map (ad->hba->as, prdt_addr, &prdt_len,DMA_DIRECTION_TO_DEVICE , -MEMTXATTRS_UNSPECIFIED ))){ +MEMTXATTRS_UNSPECIFIED ,NULL ))) {trace_ahci_populate_sglist_no_map (ad->hba, ad->port_no);return -1 ; }
@@ -1338,7 +1338,8 @@ static void handle_cmd(AHCIState *s, int port, uint8_t slot) tbl_addr =le64_to_cpu (cmd->tbl_addr); cmd_len =0x80 ; cmd_fis =dma_memory_map (s->as, tbl_addr, &cmd_len, -DMA_DIRECTION_TO_DEVICE ,MEMTXATTRS_UNSPECIFIED ); +DMA_DIRECTION_TO_DEVICE ,MEMTXATTRS_UNSPECIFIED , +NULL );if (!cmd_fis) {trace_handle_cmd_badfis (s, port);return ;
hw/usb/libhw.c +1 -1
@@ -37,7 +37,7 @@ int usb_packet_map(USBPacket *p, QEMUSGList *sgl)while (len) {dma_addr_t xlen = len; mem =dma_memory_map (sgl->as, base, &xlen, dir, -MEMTXATTRS_UNSPECIFIED ); +MEMTXATTRS_UNSPECIFIED ,NULL );if (!mem) { goto err; }
hw/virtio/virtio.c +36 -14
159 lines not on this page · raw message
include/hw/pci/pci_device.h +1 -1
@@ -328,7 +328,7 @@ static inline void *pci_dma_map(PCIDevice *dev, dma_addr_t addr, dma_addr_t *plen, DMADirection dir) {return dma_memory_map (pci_get_address_space (dev), addr, plen, dir, -MEMTXATTRS_UNSPECIFIED ); +MEMTXATTRS_UNSPECIFIED ,NULL ); } static inline void pci_dma_unmap(PCIDevice *dev, void *buffer, dma_addr_t len,
include/hw/virtio/virtio.h +2 -0
@@ -75,6 +75,8 @@ typedef struct VirtQueueElementhwaddr *out_addr;struct iovec *in_sg;struct iovec *out_sg; +hwaddr *in_xlat_addr; +hwaddr *out_xlat_addr; } VirtQueueElement;#define VIRTIO_QUEUE_MAX 1024
include/system/dma.h +24 -1
@@ -12,6 +12,7 @@#include "exec/memory.h" #include "exec/address-spaces.h" +#include "exec/ramblock.h" #include "block/block.h" #include "block/accounting.h"
@@ -201,10 +202,12 @@ MemTxResult dma_memory_set(AddressSpace *as, dma_addr_t addr,* @len: pointer to length of buffer; updated on return * @dir: indicates the transfer direction * @attrs: memory attributes +* @guest_addr: optional output for GPA */ static inline void *dma_memory_map (AddressSpace *as,dma_addr_t addr,dma_addr_t *len, -DMADirection dir,MemTxAttrs attrs) +DMADirection dir,MemTxAttrs attrs, +hwaddr *guest_addr) {hwaddr xlen = *len;void *p;
@@ -212,6 +215,26 @@ static inline void *dma_memory_map(AddressSpace *as, p =address_space_map (as, addr, &xlen, dir ==DMA_DIRECTION_FROM_DEVICE , attrs); *len = xlen; + +/* Attempt to find a backing GPA for this HVA */ +if (guest_addr) { +if (p) { +RAMBlock *rb; +ram_addr_t offset; + + rb =qemu_ram_block_from_host (p, false, &offset); +if (rb) { +/* HVA corresponds to guest memory */ + *guest_addr = rb->offset + offset; + }else { +/* HVA doesn't correspond to guest memory */ + *guest_addr =0 ; + } + }else { +/* Mapping failed */ + *guest_addr =0 ; + } + }return p; }
system/dma-helpers.c +1 -1
@@ -135,7 +135,7 @@ static void dma_blk_cb(void *opaque, int ret) cur_addr = dbs->sg->sg[dbs->sg_cur_index].base + dbs->sg_cur_byte; cur_len = dbs->sg->sg[dbs->sg_cur_index].len - dbs->sg_cur_byte; mem =dma_memory_map (dbs->sg->as, cur_addr, &cur_len, dbs->dir, -MEMTXATTRS_UNSPECIFIED ); +MEMTXATTRS_UNSPECIFIED ,NULL );/* * Make reads deterministic in icount mode. Windows sometimes issues * disk read requests with overlapping SGs. It leads
Signature · 1 line
2.43.5
Re: [RFC v3 4/5] virtio: add in_xlat_addr & out_xlat_addr VirtQueueElement members
Quoted 9 lines · Jonah Palmer
Adds the
in_xlat_addr&out_xlat_addrhwaddr arrays to the VirtQueueElement struct and introduces an optional GPA output parameter todma_memory_map().These arrays will store a VirtQueueElement's input/output descriptors' GPA of the mapped memory region, if it's backed by guest memory, via
dma_memory_map().
Quoted 9 lines
The GPA will always correspond 1:1 to the iovec entry when translating addresses between Qemu VAs and SVQ IOVAs in
vhost_svq_translate_addr(). This helps to avoid extra complicated code in SVQ'svhost_svq_vring_write_descs()function (e.g. splitting up iovec into multiple buffers, not breaking devices using aliased mapping, etc.).Since the translation is only done once inside the DMA API alongside
virtqueue_pop(), the cost should be minimal.
I think this is a very strong change as it touches the dma subsystem. Let me try to avoid it on 5/5 :).
Quoted 388 lines
Signed-off-by Jonah Palmer <[email protected]>--- hw/display/virtio-gpu.c | 5 ++-- hw/hyperv/vmbus.c | 8 +++--- hw/ide/ahci.c | 7 +++--- hw/usb/libhw.c | 2 +- hw/virtio/virtio.c | 50 ++++++++++++++++++++++++++----------- include/hw/pci/pci_device.h | 2 +- include/hw/virtio/virtio.h | 2 ++ include/system/dma.h | 25 ++++++++++++++++++- system/dma-helpers.c | 2 +- 9 files changed, 77 insertions(+), 26 deletions(-)hw/display/virtio-gpu.c +3
-2@@ -839,7 +839,7 @@ int virtio_gpu_create_mapping_iov(VirtIOGPU *g, len = l; map =dma_memory_map (VIRTIO_DEVICE (g)->dma_as, a, &len,DMA_DIRECTION_TO_DEVICE , -MEMTXATTRS_UNSPECIFIED ); +MEMTXATTRS_UNSPECIFIED ,NULL );if (!map) {qemu_log_mask (LOG_GUEST_ERROR ,"%s: failed to map MMIO memory for" " element %d\n" , __func__, e);@@ -1258,7 +1258,8 @@ static bool virtio_gpu_load_restore_mapping(VirtIOGPU *g,hwaddr len = res->iov[i].iov_len; res->iov[i].iov_base =dma_memory_map (VIRTIO_DEVICE (g)->dma_as, res->addrs[i], &len, -DMA_DIRECTION_TO_DEVICE ,MEMTXATTRS_UNSPECIFIED ); +DMA_DIRECTION_TO_DEVICE ,MEMTXATTRS_UNSPECIFIED , +NULL );if (!res->iov[i].iov_base || len != res->iov[i].iov_len) {/* Clean up the half-a-mapping we just created... */ hw/hyperv/vmbus.c +5
-3@@ -374,7 +374,7 @@ static ssize_t gpadl_iter_io(GpadlIter *iter, void *buf, uint32_t len) maddr = (iter->gpadl->gfns[idx] <<TARGET_PAGE_BITS ) | off_in_page; iter->map =dma_memory_map (iter->as, maddr, &mlen, iter->dir, -MEMTXATTRS_UNSPECIFIED ); +MEMTXATTRS_UNSPECIFIED ,NULL );if (mlen != pgleft) {dma_memory_unmap (iter->as, iter->map, mlen, iter->dir,0 ); iter->map =NULL ;@@ -492,7 +492,8 @@ int vmbus_map_sgl(VMBusChanReq *req, DMADirection dir, struct iovec *iov, } iov[ret_cnt].iov_base =dma_memory_map (sgl->as, a, &l, dir, -MEMTXATTRS_UNSPECIFIED ); +MEMTXATTRS_UNSPECIFIED , +NULL );if (!l) { ret = -EFAULT ; goto err;@@ -568,7 +569,8 @@ static vmbus_ring_buffer *ringbuf_map_hdr(VMBusRingBufCommon *ringbuf)dma_addr_t mlen =sizeof (*rb); rb =dma_memory_map (ringbuf->as, ringbuf->rb_addr, &mlen, -DMA_DIRECTION_FROM_DEVICE ,MEMTXATTRS_UNSPECIFIED ); +DMA_DIRECTION_FROM_DEVICE ,MEMTXATTRS_UNSPECIFIED , +NULL );if (mlen !=sizeof (*rb)) {dma_memory_unmap (ringbuf->as, rb, mlen,DMA_DIRECTION_FROM_DEVICE ,0 );hw/ide/ahci.c +4
-3@@ -221,7 +221,7 @@ static void map_page(AddressSpace *as, uint8_t **ptr, uint64_t addr, } *ptr =dma_memory_map (as, addr, &len,DMA_DIRECTION_FROM_DEVICE , -MEMTXATTRS_UNSPECIFIED ); +MEMTXATTRS_UNSPECIFIED ,NULL );if (len < wanted && *ptr) {dma_memory_unmap (as, *ptr, len,DMA_DIRECTION_FROM_DEVICE , len); *ptr =NULL ;@@ -928,7 +928,7 @@ static int ahci_populate_sglist(AHCIDevice *ad, QEMUSGList *sglist,/* map PRDT */ if (!(prdt =dma_memory_map (ad->hba->as, prdt_addr, &prdt_len,DMA_DIRECTION_TO_DEVICE , -MEMTXATTRS_UNSPECIFIED ))){ +MEMTXATTRS_UNSPECIFIED ,NULL ))) {trace_ahci_populate_sglist_no_map (ad->hba, ad->port_no);return -1 ; }@@ -1338,7 +1338,8 @@ static void handle_cmd(AHCIState *s, int port, uint8_t slot) tbl_addr =le64_to_cpu (cmd->tbl_addr); cmd_len =0x80 ; cmd_fis =dma_memory_map (s->as, tbl_addr, &cmd_len, -DMA_DIRECTION_TO_DEVICE ,MEMTXATTRS_UNSPECIFIED ); +DMA_DIRECTION_TO_DEVICE ,MEMTXATTRS_UNSPECIFIED , +NULL );if (!cmd_fis) {trace_handle_cmd_badfis (s, port);return ;hw/usb/libhw.c +1
-1@@ -37,7 +37,7 @@ int usb_packet_map(USBPacket *p, QEMUSGList *sgl)while (len) {dma_addr_t xlen = len; mem =dma_memory_map (sgl->as, base, &xlen, dir, -MEMTXATTRS_UNSPECIFIED ); +MEMTXATTRS_UNSPECIFIED ,NULL );if (!mem) { goto err; }hw/virtio/virtio.c +36
-14@@ -1553,9 +1553,9 @@ int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes, }static bool virtqueue_map_desc (VirtIODevice *vdev,unsigned int *p_num_sg, -hwaddr *addr,struct iovec *iov, -unsigned int max_num_sg,bool is_write, -hwaddr pa,size_t sz) +hwaddr *addr,hwaddr *xlat_addr, +struct iovec *iov,unsigned int max_num_sg, +bool is_write,hwaddr pa,size_t sz) {bool ok = false;unsigned num_sg = *p_num_sg;@@ -1579,7 +1579,8 @@ static bool virtqueue_map_desc(VirtIODevice *vdev, unsigned int *p_num_sg, is_write ?DMA_DIRECTION_FROM_DEVICE :DMA_DIRECTION_TO_DEVICE , - MEMTXATTRS_UNSPECIFIED); +MEMTXATTRS_UNSPECIFIED , + &xlat_addr[num_sg]);if (!iov[num_sg].iov_base) {virtio_error (vdev,"virtio: bogus descriptor or out of resources" ); goto out;@@ -1618,7 +1619,7 @@ static void virtqueue_undo_map_desc(unsigned int out_num, unsigned int in_num,static void virtqueue_map_iovec (VirtIODevice *vdev,struct iovec *sg,hwaddr *addr,unsigned int num_sg, -bool is_write) +hwaddr *xlat_addr,bool is_write) {unsigned int i;hwaddr len;@@ -1629,7 +1630,8 @@ static void virtqueue_map_iovec(VirtIODevice *vdev, struct iovec *sg, addr[i], &len, is_write ?DMA_DIRECTION_FROM_DEVICE :DMA_DIRECTION_TO_DEVICE , - MEMTXATTRS_UNSPECIFIED); +MEMTXATTRS_UNSPECIFIED , + &xlat_addr[i]);if (!sg[i].iov_base) {error_report ("virtio: error trying to map MMIO memory" );exit (1 );@@ -1643,9 +1645,10 @@ static void virtqueue_map_iovec(VirtIODevice *vdev, struct iovec *sg,void virtqueue_map (VirtIODevice *vdev,VirtQueueElement *elem) { -virtqueue_map_iovec (vdev, elem->in_sg, elem->in_addr, elem->in_num, true); +virtqueue_map_iovec (vdev, elem->in_sg, elem->in_addr, elem->in_num, + elem->in_xlat_addr, true);virtqueue_map_iovec (vdev, elem->out_sg, elem->out_addr, elem->out_num, - false); + elem->out_xlat_addr, false); }static void *virtqueue_alloc_element (size_t sz,unsigned out_num,unsigned in_num)@@ -1654,7 +1657,14 @@ static void *virtqueue_alloc_element(size_t sz, unsigned out_num, unsigned in_nu size_t in_addr_ofs = QEMU_ALIGN_UP(sz, __alignof__(elem->in_addr[0]));size_t out_addr_ofs = in_addr_ofs + in_num *sizeof (elem->in_addr[0 ]);size_t out_addr_end = out_addr_ofs + out_num *sizeof (elem->out_addr[0 ]); - size_t in_sg_ofs = QEMU_ALIGN_UP(out_addr_end, __alignof__(elem->in_sg[0])); +size_t in_xlat_addr_ofs = + QEMU_ALIGN_UP(out_addr_end, __alignof__(elem->in_xlat_addr[0])); +size_t out_xlat_addr_ofs = in_xlat_addr_ofs + in_num * +sizeof (elem->in_xlat_addr[0 ]); +size_t out_xlat_addr_end = out_xlat_addr_ofs + out_num * +sizeof (elem->out_xlat_addr[0 ]); +size_t in_sg_ofs = + QEMU_ALIGN_UP(out_xlat_addr_end, __alignof__(elem->in_sg[0]));size_t out_sg_ofs = in_sg_ofs + in_num *sizeof (elem->in_sg[0 ]);size_t out_sg_end = out_sg_ofs + out_num *sizeof (elem->out_sg[0 ]);@@ -1665,6 +1675,8 @@ static void *virtqueue_alloc_element(size_t sz, unsigned out_num, unsigned in_nu elem->in_num = in_num; elem->in_addr = (void *)elem + in_addr_ofs; elem->out_addr = (void *)elem + out_addr_ofs; + elem->in_xlat_addr = (void *)elem + in_xlat_addr_ofs; + elem->out_xlat_addr = (void *)elem + out_xlat_addr_ofs; elem->in_sg = (void *)elem + in_sg_ofs; elem->out_sg = (void *)elem + out_sg_ofs;return elem;@@ -1681,6 +1693,7 @@ static void *virtqueue_split_pop(VirtQueue *vq, size_t sz)VirtQueueElement *elem =NULL ;unsigned out_num, in_num, elem_entries;hwaddr addr[VIRTQUEUE_MAX_SIZE ]; +hwaddr xlat_addr[VIRTQUEUE_MAX_SIZE ];struct iovec iov[VIRTQUEUE_MAX_SIZE ];VRingDesc desc;int rc;@@ -1754,7 +1767,7 @@ static void *virtqueue_split_pop(VirtQueue *vq, size_t sz)if (desc.flags &VRING_DESC_F_WRITE ) { map_ok =virtqueue_map_desc (vdev, &in_num, addr + out_num, - iov + out_num, + xlat_addr + out_num, iov + out_num,VIRTQUEUE_MAX_SIZE - out_num, true, desc.addr, desc.len); }else {@@ -1762,8 +1775,8 @@ static void *virtqueue_split_pop(VirtQueue *vq, size_t sz)virtio_error (vdev,"Incorrect order for descriptors" ); goto err_undo_map; } - map_ok =virtqueue_map_desc (vdev, &out_num, addr, iov, -VIRTQUEUE_MAX_SIZE , false, + map_ok =virtqueue_map_desc (vdev, &out_num, addr, xlat_addr, + iov,VIRTQUEUE_MAX_SIZE , false, desc.addr, desc.len); }if (!map_ok) {@@ -1790,10 +1803,12 @@ static void *virtqueue_split_pop(VirtQueue *vq, size_t sz)for (i =0 ; i < out_num; i++) { elem->out_addr[i] = addr[i]; elem->out_sg[i] = iov[i]; + elem->out_xlat_addr[i] = xlat_addr[i]; }for (i =0 ; i < in_num; i++) { elem->in_addr[i] = addr[out_num + i]; elem->in_sg[i] = iov[out_num + i]; + elem->in_xlat_addr[i] = xlat_addr[out_num + i]; }if (virtio_vdev_has_feature (vdev,VIRTIO_F_IN_ORDER )) {@@ -1827,6 +1842,7 @@ static void *virtqueue_packed_pop(VirtQueue *vq, size_t sz)VirtQueueElement *elem =NULL ;unsigned out_num, in_num, elem_entries;hwaddr addr[VIRTQUEUE_MAX_SIZE ]; +hwaddr xlat_addr[VIRTQUEUE_MAX_SIZE ];struct iovec iov[VIRTQUEUE_MAX_SIZE ];VRingPackedDesc desc;uint16_t id;@@ -1891,7 +1907,7 @@ static void *virtqueue_packed_pop(VirtQueue *vq, size_t sz)if (desc.flags &VRING_DESC_F_WRITE ) { map_ok =virtqueue_map_desc (vdev, &in_num, addr + out_num, - iov + out_num, + xlat_addr + out_num, iov + out_num,VIRTQUEUE_MAX_SIZE - out_num, true, desc.addr, desc.len); }else {@@ -1899,7 +1915,7 @@ static void *virtqueue_packed_pop(VirtQueue *vq, size_t sz)virtio_error (vdev,"Incorrect order for descriptors" ); goto err_undo_map; } - map_ok =virtqueue_map_desc (vdev, &out_num, addr, iov, + map_ok =virtqueue_map_desc (vdev, &out_num, addr, xlat_addr, iov,VIRTQUEUE_MAX_SIZE , false, desc.addr, desc.len); }@@ -1928,10 +1944,12 @@ static void *virtqueue_packed_pop(VirtQueue *vq, size_t sz)for (i =0 ; i < out_num; i++) { elem->out_addr[i] = addr[i]; elem->out_sg[i] = iov[i]; + elem->out_xlat_addr[i] = xlat_addr[i]; }for (i =0 ; i < in_num; i++) { elem->in_addr[i] = addr[out_num + i]; elem->in_sg[i] = iov[out_num + i]; + elem->in_xlat_addr[i] = xlat_addr[out_num + i]; } elem->index = id;@@ -2117,10 +2135,14 @@ void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz) elem->index = data.index;for (i =0 ; i < elem->in_num; i++) { +/* xlat_addr is overwritten by virtqueue_map */ + elem->in_xlat_addr[i] =0 ; elem->in_addr[i] = data.in_addr[i]; }for (i =0 ; i < elem->out_num; i++) { +/* xlat_addr is overwritten by virtqueue_map */ + elem->out_xlat_addr[i] =0 ; elem->out_addr[i] = data.out_addr[i]; }include/hw/pci/pci_device.h +1
-1@@ -328,7 +328,7 @@ static inline void *pci_dma_map(PCIDevice *dev, dma_addr_t addr, dma_addr_t *plen, DMADirection dir) {return dma_memory_map (pci_get_address_space (dev), addr, plen, dir, -MEMTXATTRS_UNSPECIFIED ); +MEMTXATTRS_UNSPECIFIED ,NULL ); } static inline void pci_dma_unmap(PCIDevice *dev, void *buffer, dma_addr_t len,include/hw/virtio/virtio.h +2
-0@@ -75,6 +75,8 @@ typedef struct VirtQueueElementhwaddr *out_addr;struct iovec *in_sg;struct iovec *out_sg; +hwaddr *in_xlat_addr; +hwaddr *out_xlat_addr; } VirtQueueElement;#define VIRTIO_QUEUE_MAX 1024include/system/dma.h +24
-1@@ -12,6 +12,7 @@#include "exec/memory.h" #include "exec/address-spaces.h" +#include "exec/ramblock.h" #include "block/block.h" #include "block/accounting.h" @@ -201,10 +202,12 @@ MemTxResult dma_memory_set(AddressSpace *as, dma_addr_t addr,* @len: pointer to length of buffer; updated on return * @dir: indicates the transfer direction * @attrs: memory attributes +* @guest_addr: optional output for GPA */ static inline void *dma_memory_map (AddressSpace *as,dma_addr_t addr,dma_addr_t *len, -DMADirection dir,MemTxAttrs attrs) +DMADirection dir,MemTxAttrs attrs, +hwaddr *guest_addr) {hwaddr xlen = *len;void *p;@@ -212,6 +215,26 @@ static inline void *dma_memory_map(AddressSpace *as, p =address_space_map (as, addr, &xlen, dir ==DMA_DIRECTION_FROM_DEVICE , attrs); *len = xlen; + +/* Attempt to find a backing GPA for this HVA */ +if (guest_addr) { +if (p) { +RAMBlock *rb; +ram_addr_t offset; + + rb =qemu_ram_block_from_host (p, false, &offset); +if (rb) { +/* HVA corresponds to guest memory */ + *guest_addr = rb->offset + offset; + }else { +/* HVA doesn't correspond to guest memory */ + *guest_addr =0 ; + } + }else { +/* Mapping failed */ + *guest_addr =0 ; + } + }return p; }system/dma-helpers.c +1
-2@@ -135,7 +135,7 @@ static void dma_blk_cb(void *opaque, int ret) cur_addr = dbs->sg->sg[dbs->sg_cur_index].base + dbs->sg_cur_byte; cur_len = dbs->sg->sg[dbs->sg_cur_index].len - dbs->sg_cur_byte; mem =dma_memory_map (dbs->sg->as, cur_addr, &cur_len, dbs->dir, -MEMTXATTRS_UNSPECIFIED ); +MEMTXATTRS_UNSPECIFIED ,NULL );/* * Make reads deterministic in icount mode. Windows sometimes issues * disk read requests with overlapping SGs. It leads -- 2.43.5
Re: [RFC v3 4/5] virtio: add in_xlat_addr & out_xlat_addr VirtQueueElement members
Quoted 15 lines · Eugenio Perez Martin
Quoted 14 lines · Jonah Palmer
Adds the
in_xlat_addr&out_xlat_addrhwaddr arrays to the VirtQueueElement struct and introduces an optional GPA output parameter todma_memory_map().These arrays will store a VirtQueueElement's input/output descriptors' GPA of the mapped memory region, if it's backed by guest memory, via
dma_memory_map().The GPA will always correspond 1:1 to the iovec entry when translating addresses between Qemu VAs and SVQ IOVAs in
vhost_svq_translate_addr(). This helps to avoid extra complicated code in SVQ'svhost_svq_vring_write_descs()function (e.g. splitting up iovec into multiple buffers, not breaking devices using aliased mapping, etc.).
Quoted 5 lines
Quoted 3 lines
Since the translation is only done once inside the DMA API alongside
virtqueue_pop(), the cost should be minimal.I think this is a very strong change as it touches the dma subsystem. Let me try to avoid it on 5/5 :).
This change was what I suggested to Jonah actually, the intent for the DMA API change was to lessen the friction of vIOMMU support for Shadow VQ (for which case elem->in_addr and elem->out_addr is GIOVA rather than GPA). This will be the most efficient and performant way to get the needed translation done only once without having to maintain separate GPA mapping & translation out of the memory subsystem. I wonder would it work to create an variant of dma_memory_map() API while leaving the current one as is: the new API variant would return a cookie that is used to represent the address translation, such that SVQ subsystem can use it later on whenever needed in order to fetch the GIOVA to GPA translation in place (the translated GPA address may be cached in the opaque cookie) from the memory system?
Regards,
-Siwei
Quoted 389 lines
Quoted 389 lines
Signed-off-by Jonah Palmer <[email protected]>--- hw/display/virtio-gpu.c | 5 ++-- hw/hyperv/vmbus.c | 8 +++--- hw/ide/ahci.c | 7 +++--- hw/usb/libhw.c | 2 +- hw/virtio/virtio.c | 50 ++++++++++++++++++++++++++----------- include/hw/pci/pci_device.h | 2 +- include/hw/virtio/virtio.h | 2 ++ include/system/dma.h | 25 ++++++++++++++++++- system/dma-helpers.c | 2 +- 9 files changed, 77 insertions(+), 26 deletions(-)hw/display/virtio-gpu.c +3
-2@@ -839,7 +839,7 @@ int virtio_gpu_create_mapping_iov(VirtIOGPU *g, len = l; map =dma_memory_map (VIRTIO_DEVICE (g)->dma_as, a, &len,DMA_DIRECTION_TO_DEVICE , -MEMTXATTRS_UNSPECIFIED ); +MEMTXATTRS_UNSPECIFIED ,NULL );if (!map) {qemu_log_mask (LOG_GUEST_ERROR ,"%s: failed to map MMIO memory for" " element %d\n" , __func__, e);@@ -1258,7 +1258,8 @@ static bool virtio_gpu_load_restore_mapping(VirtIOGPU *g,hwaddr len = res->iov[i].iov_len; res->iov[i].iov_base =dma_memory_map (VIRTIO_DEVICE (g)->dma_as, res->addrs[i], &len, -DMA_DIRECTION_TO_DEVICE ,MEMTXATTRS_UNSPECIFIED ); +DMA_DIRECTION_TO_DEVICE ,MEMTXATTRS_UNSPECIFIED , +NULL );if (!res->iov[i].iov_base || len != res->iov[i].iov_len) {/* Clean up the half-a-mapping we just created... */ hw/hyperv/vmbus.c +5
-3@@ -374,7 +374,7 @@ static ssize_t gpadl_iter_io(GpadlIter *iter, void *buf, uint32_t len) maddr = (iter->gpadl->gfns[idx] <<TARGET_PAGE_BITS ) | off_in_page; iter->map =dma_memory_map (iter->as, maddr, &mlen, iter->dir, -MEMTXATTRS_UNSPECIFIED ); +MEMTXATTRS_UNSPECIFIED ,NULL );if (mlen != pgleft) {dma_memory_unmap (iter->as, iter->map, mlen, iter->dir,0 ); iter->map =NULL ;@@ -492,7 +492,8 @@ int vmbus_map_sgl(VMBusChanReq *req, DMADirection dir, struct iovec *iov, } iov[ret_cnt].iov_base =dma_memory_map (sgl->as, a, &l, dir, -MEMTXATTRS_UNSPECIFIED ); +MEMTXATTRS_UNSPECIFIED , +NULL );if (!l) { ret = -EFAULT ; goto err;@@ -568,7 +569,8 @@ static vmbus_ring_buffer *ringbuf_map_hdr(VMBusRingBufCommon *ringbuf)dma_addr_t mlen =sizeof (*rb); rb =dma_memory_map (ringbuf->as, ringbuf->rb_addr, &mlen, -DMA_DIRECTION_FROM_DEVICE ,MEMTXATTRS_UNSPECIFIED ); +DMA_DIRECTION_FROM_DEVICE ,MEMTXATTRS_UNSPECIFIED , +NULL );if (mlen !=sizeof (*rb)) {dma_memory_unmap (ringbuf->as, rb, mlen,DMA_DIRECTION_FROM_DEVICE ,0 );hw/ide/ahci.c +4
-3@@ -221,7 +221,7 @@ static void map_page(AddressSpace *as, uint8_t **ptr, uint64_t addr, } *ptr =dma_memory_map (as, addr, &len,DMA_DIRECTION_FROM_DEVICE , -MEMTXATTRS_UNSPECIFIED ); +MEMTXATTRS_UNSPECIFIED ,NULL );if (len < wanted && *ptr) {dma_memory_unmap (as, *ptr, len,DMA_DIRECTION_FROM_DEVICE , len); *ptr =NULL ;@@ -928,7 +928,7 @@ static int ahci_populate_sglist(AHCIDevice *ad, QEMUSGList *sglist,/* map PRDT */ if (!(prdt =dma_memory_map (ad->hba->as, prdt_addr, &prdt_len,DMA_DIRECTION_TO_DEVICE , -MEMTXATTRS_UNSPECIFIED ))){ +MEMTXATTRS_UNSPECIFIED ,NULL ))) {trace_ahci_populate_sglist_no_map (ad->hba, ad->port_no);return -1 ; }@@ -1338,7 +1338,8 @@ static void handle_cmd(AHCIState *s, int port, uint8_t slot) tbl_addr =le64_to_cpu (cmd->tbl_addr); cmd_len =0x80 ; cmd_fis =dma_memory_map (s->as, tbl_addr, &cmd_len, -DMA_DIRECTION_TO_DEVICE ,MEMTXATTRS_UNSPECIFIED ); +DMA_DIRECTION_TO_DEVICE ,MEMTXATTRS_UNSPECIFIED , +NULL );if (!cmd_fis) {trace_handle_cmd_badfis (s, port);return ;hw/usb/libhw.c +1
-1@@ -37,7 +37,7 @@ int usb_packet_map(USBPacket *p, QEMUSGList *sgl)while (len) {dma_addr_t xlen = len; mem =dma_memory_map (sgl->as, base, &xlen, dir, -MEMTXATTRS_UNSPECIFIED ); +MEMTXATTRS_UNSPECIFIED ,NULL );if (!mem) { goto err; }hw/virtio/virtio.c +36
-14@@ -1553,9 +1553,9 @@ int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes, }static bool virtqueue_map_desc (VirtIODevice *vdev,unsigned int *p_num_sg, -hwaddr *addr,struct iovec *iov, -unsigned int max_num_sg,bool is_write, -hwaddr pa,size_t sz) +hwaddr *addr,hwaddr *xlat_addr, +struct iovec *iov,unsigned int max_num_sg, +bool is_write,hwaddr pa,size_t sz) {bool ok = false;unsigned num_sg = *p_num_sg;@@ -1579,7 +1579,8 @@ static bool virtqueue_map_desc(VirtIODevice *vdev, unsigned int *p_num_sg, is_write ?DMA_DIRECTION_FROM_DEVICE :DMA_DIRECTION_TO_DEVICE , - MEMTXATTRS_UNSPECIFIED); +MEMTXATTRS_UNSPECIFIED , + &xlat_addr[num_sg]);if (!iov[num_sg].iov_base) {virtio_error (vdev,"virtio: bogus descriptor or out of resources" ); goto out;@@ -1618,7 +1619,7 @@ static void virtqueue_undo_map_desc(unsigned int out_num, unsigned int in_num,static void virtqueue_map_iovec (VirtIODevice *vdev,struct iovec *sg,hwaddr *addr,unsigned int num_sg, -bool is_write) +hwaddr *xlat_addr,bool is_write) {unsigned int i;hwaddr len;@@ -1629,7 +1630,8 @@ static void virtqueue_map_iovec(VirtIODevice *vdev, struct iovec *sg, addr[i], &len, is_write ?DMA_DIRECTION_FROM_DEVICE :DMA_DIRECTION_TO_DEVICE , - MEMTXATTRS_UNSPECIFIED); +MEMTXATTRS_UNSPECIFIED , + &xlat_addr[i]);if (!sg[i].iov_base) {error_report ("virtio: error trying to map MMIO memory" );exit (1 );@@ -1643,9 +1645,10 @@ static void virtqueue_map_iovec(VirtIODevice *vdev, struct iovec *sg,void virtqueue_map (VirtIODevice *vdev,VirtQueueElement *elem) { -virtqueue_map_iovec (vdev, elem->in_sg, elem->in_addr, elem->in_num, true); +virtqueue_map_iovec (vdev, elem->in_sg, elem->in_addr, elem->in_num, + elem->in_xlat_addr, true);virtqueue_map_iovec (vdev, elem->out_sg, elem->out_addr, elem->out_num, - false); + elem->out_xlat_addr, false); }static void *virtqueue_alloc_element (size_t sz,unsigned out_num,unsigned in_num)@@ -1654,7 +1657,14 @@ static void *virtqueue_alloc_element(size_t sz, unsigned out_num, unsigned in_nu size_t in_addr_ofs = QEMU_ALIGN_UP(sz, __alignof__(elem->in_addr[0]));size_t out_addr_ofs = in_addr_ofs + in_num *sizeof (elem->in_addr[0 ]);size_t out_addr_end = out_addr_ofs + out_num *sizeof (elem->out_addr[0 ]); - size_t in_sg_ofs = QEMU_ALIGN_UP(out_addr_end, __alignof__(elem->in_sg[0])); +size_t in_xlat_addr_ofs = + QEMU_ALIGN_UP(out_addr_end, __alignof__(elem->in_xlat_addr[0])); +size_t out_xlat_addr_ofs = in_xlat_addr_ofs + in_num * +sizeof (elem->in_xlat_addr[0 ]); +size_t out_xlat_addr_end = out_xlat_addr_ofs + out_num * +sizeof (elem->out_xlat_addr[0 ]); +size_t in_sg_ofs = + QEMU_ALIGN_UP(out_xlat_addr_end, __alignof__(elem->in_sg[0]));size_t out_sg_ofs = in_sg_ofs + in_num *sizeof (elem->in_sg[0 ]);size_t out_sg_end = out_sg_ofs + out_num *sizeof (elem->out_sg[0 ]);@@ -1665,6 +1675,8 @@ static void *virtqueue_alloc_element(size_t sz, unsigned out_num, unsigned in_nu elem->in_num = in_num; elem->in_addr = (void *)elem + in_addr_ofs; elem->out_addr = (void *)elem + out_addr_ofs; + elem->in_xlat_addr = (void *)elem + in_xlat_addr_ofs; + elem->out_xlat_addr = (void *)elem + out_xlat_addr_ofs; elem->in_sg = (void *)elem + in_sg_ofs; elem->out_sg = (void *)elem + out_sg_ofs;return elem;@@ -1681,6 +1693,7 @@ static void *virtqueue_split_pop(VirtQueue *vq, size_t sz)VirtQueueElement *elem =NULL ;unsigned out_num, in_num, elem_entries;hwaddr addr[VIRTQUEUE_MAX_SIZE ]; +hwaddr xlat_addr[VIRTQUEUE_MAX_SIZE ];struct iovec iov[VIRTQUEUE_MAX_SIZE ];VRingDesc desc;int rc;@@ -1754,7 +1767,7 @@ static void *virtqueue_split_pop(VirtQueue *vq, size_t sz)if (desc.flags &VRING_DESC_F_WRITE ) { map_ok =virtqueue_map_desc (vdev, &in_num, addr + out_num, - iov + out_num, + xlat_addr + out_num, iov + out_num,VIRTQUEUE_MAX_SIZE - out_num, true, desc.addr, desc.len); }else {@@ -1762,8 +1775,8 @@ static void *virtqueue_split_pop(VirtQueue *vq, size_t sz)virtio_error (vdev,"Incorrect order for descriptors" ); goto err_undo_map; } - map_ok =virtqueue_map_desc (vdev, &out_num, addr, iov, -VIRTQUEUE_MAX_SIZE , false, + map_ok =virtqueue_map_desc (vdev, &out_num, addr, xlat_addr, + iov,VIRTQUEUE_MAX_SIZE , false, desc.addr, desc.len); }if (!map_ok) {@@ -1790,10 +1803,12 @@ static void *virtqueue_split_pop(VirtQueue *vq, size_t sz)for (i =0 ; i < out_num; i++) { elem->out_addr[i] = addr[i]; elem->out_sg[i] = iov[i]; + elem->out_xlat_addr[i] = xlat_addr[i]; }for (i =0 ; i < in_num; i++) { elem->in_addr[i] = addr[out_num + i]; elem->in_sg[i] = iov[out_num + i]; + elem->in_xlat_addr[i] = xlat_addr[out_num + i]; }if (virtio_vdev_has_feature (vdev,VIRTIO_F_IN_ORDER )) {@@ -1827,6 +1842,7 @@ static void *virtqueue_packed_pop(VirtQueue *vq, size_t sz)VirtQueueElement *elem =NULL ;unsigned out_num, in_num, elem_entries;hwaddr addr[VIRTQUEUE_MAX_SIZE ]; +hwaddr xlat_addr[VIRTQUEUE_MAX_SIZE ];struct iovec iov[VIRTQUEUE_MAX_SIZE ];VRingPackedDesc desc;uint16_t id;@@ -1891,7 +1907,7 @@ static void *virtqueue_packed_pop(VirtQueue *vq, size_t sz)if (desc.flags &VRING_DESC_F_WRITE ) { map_ok =virtqueue_map_desc (vdev, &in_num, addr + out_num, - iov + out_num, + xlat_addr + out_num, iov + out_num,VIRTQUEUE_MAX_SIZE - out_num, true, desc.addr, desc.len); }else {@@ -1899,7 +1915,7 @@ static void *virtqueue_packed_pop(VirtQueue *vq, size_t sz)virtio_error (vdev,"Incorrect order for descriptors" ); goto err_undo_map; } - map_ok =virtqueue_map_desc (vdev, &out_num, addr, iov, + map_ok =virtqueue_map_desc (vdev, &out_num, addr, xlat_addr, iov,VIRTQUEUE_MAX_SIZE , false, desc.addr, desc.len); }@@ -1928,10 +1944,12 @@ static void *virtqueue_packed_pop(VirtQueue *vq, size_t sz)for (i =0 ; i < out_num; i++) { elem->out_addr[i] = addr[i]; elem->out_sg[i] = iov[i]; + elem->out_xlat_addr[i] = xlat_addr[i]; }for (i =0 ; i < in_num; i++) { elem->in_addr[i] = addr[out_num + i]; elem->in_sg[i] = iov[out_num + i]; + elem->in_xlat_addr[i] = xlat_addr[out_num + i]; } elem->index = id;@@ -2117,10 +2135,14 @@ void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz) elem->index = data.index;for (i =0 ; i < elem->in_num; i++) { +/* xlat_addr is overwritten by virtqueue_map */ + elem->in_xlat_addr[i] =0 ; elem->in_addr[i] = data.in_addr[i]; }for (i =0 ; i < elem->out_num; i++) { +/* xlat_addr is overwritten by virtqueue_map */ + elem->out_xlat_addr[i] =0 ; elem->out_addr[i] = data.out_addr[i]; }include/hw/pci/pci_device.h +1
-1@@ -328,7 +328,7 @@ static inline void *pci_dma_map(PCIDevice *dev, dma_addr_t addr, dma_addr_t *plen, DMADirection dir) {return dma_memory_map (pci_get_address_space (dev), addr, plen, dir, -MEMTXATTRS_UNSPECIFIED ); +MEMTXATTRS_UNSPECIFIED ,NULL ); } static inline void pci_dma_unmap(PCIDevice *dev, void *buffer, dma_addr_t len,include/hw/virtio/virtio.h +2
-0@@ -75,6 +75,8 @@ typedef struct VirtQueueElementhwaddr *out_addr;struct iovec *in_sg;struct iovec *out_sg; +hwaddr *in_xlat_addr; +hwaddr *out_xlat_addr; } VirtQueueElement;#define VIRTIO_QUEUE_MAX 1024include/system/dma.h +24
-1@@ -12,6 +12,7 @@#include "exec/memory.h" #include "exec/address-spaces.h" +#include "exec/ramblock.h" #include "block/block.h" #include "block/accounting.h" @@ -201,10 +202,12 @@ MemTxResult dma_memory_set(AddressSpace *as, dma_addr_t addr,* @len: pointer to length of buffer; updated on return * @dir: indicates the transfer direction * @attrs: memory attributes +* @guest_addr: optional output for GPA */ static inline void *dma_memory_map (AddressSpace *as,dma_addr_t addr,dma_addr_t *len, -DMADirection dir,MemTxAttrs attrs) +DMADirection dir,MemTxAttrs attrs, +hwaddr *guest_addr) {hwaddr xlen = *len;void *p;@@ -212,6 +215,26 @@ static inline void *dma_memory_map(AddressSpace *as, p =address_space_map (as, addr, &xlen, dir ==DMA_DIRECTION_FROM_DEVICE , attrs); *len = xlen; + +/* Attempt to find a backing GPA for this HVA */ +if (guest_addr) { +if (p) { +RAMBlock *rb; +ram_addr_t offset; + + rb =qemu_ram_block_from_host (p, false, &offset); +if (rb) { +/* HVA corresponds to guest memory */ + *guest_addr = rb->offset + offset; + }else { +/* HVA doesn't correspond to guest memory */ + *guest_addr =0 ; + } + }else { +/* Mapping failed */ + *guest_addr =0 ; + } + }return p; }system/dma-helpers.c +1
-2@@ -135,7 +135,7 @@ static void dma_blk_cb(void *opaque, int ret) cur_addr = dbs->sg->sg[dbs->sg_cur_index].base + dbs->sg_cur_byte; cur_len = dbs->sg->sg[dbs->sg_cur_index].len - dbs->sg_cur_byte; mem =dma_memory_map (dbs->sg->as, cur_addr, &cur_len, dbs->dir, -MEMTXATTRS_UNSPECIFIED ); +MEMTXATTRS_UNSPECIFIED ,NULL );/* * Make reads deterministic in icount mode. Windows sometimes issues * disk read requests with overlapping SGs. It leads -- 2.43.5
RFC v3 5/5 svq: Support translations via GPAs in vhost_svq_translate_addr
In reply to Jonah PalmerPropagates the GPAs (in_xlat_addr/out_xlat_addr) of a VirtQueueElement to vhost_svq_translate_addr() to translate to IOVAs via GPA->IOVA tree when descriptors are backed by guest memory.
For descriptors backed by guest memory, the translation is performed using GPAs via the GPA->IOVA tree. GPAs are unique in the guest's address space, so this ensures unambiguous IOVA translations.
For descriptors not backed by guest memory, the existing IOVA->HVA tree is used.
This avoids the issue where different GPAs map to the same HVA, causing the HVA->IOVA translation to potentially return an IOVA associated with the wrong intended GPA.
--- hw/virtio/vhost-shadow-virtqueue.c | 49 ++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 12 deletions(-)
hw/virtio/vhost-shadow-virtqueue.c +37 -12
@@ -78,24 +78,37 @@ uint16_t vhost_svq_available_slots(const VhostShadowVirtqueue *svq)* @vaddr: Translated IOVA addresses * @iovec: Source qemu's VA addresses * @num: Length of iovec and minimum length of vaddr +* @gpas: Descriptors' GPAs, if backed by guest memory */ static bool vhost_svq_translate_addr (const VhostShadowVirtqueue *svq,hwaddr *addrs,const struct iovec *iovec, -size_t num) +size_t num,const hwaddr *gpas) {if (num ==0 ) {return true; }for (size_t i =0 ; i < num; ++i) { -DMAMap needle = { - .translated_addr = (hwaddr )(uintptr_t )iovec[i].iov_base, - .size = iovec[i].iov_len, - }; +const DMAMap *map; +DMAMap needle;Int128 needle_last, map_last;size_t off; -const DMAMap *map =vhost_iova_tree_find_iova (svq->iova_tree, &needle); +if (gpas) { +/* Search the GPA->IOVA tree */ + needle = (DMAMap ) { + .translated_addr = gpas[i], + .size = iovec[i].iov_len, + }; + map =vhost_iova_tree_find_gpa (svq->iova_tree, &needle); + }else { +/* Searh the IOVA->HVA tree */ + needle = (DMAMap ) { + .translated_addr = (hwaddr )(uintptr_t )iovec[i].iov_base, + .size = iovec[i].iov_len, + }; + 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
@@ -132,12 +145,14 @@ static bool vhost_svq_translate_addr(const VhostShadowVirtqueue *svq,* @num: iovec length * @more_descs: True if more descriptors come in the chain * @write: True if they are writeable descriptors +* @gpas: Descriptors' GPAs, if backed by guest memory * * Return true if success, false otherwise and print error. */ static bool vhost_svq_vring_write_descs (VhostShadowVirtqueue *svq,hwaddr *sg,const struct iovec *iovec,size_t num, -bool more_descs,bool write) +bool more_descs,bool write, +const hwaddr *gpas) {uint16_t i = svq->free_head, last = svq->free_head;unsigned n;
@@ -149,7 +164,7 @@ static bool vhost_svq_vring_write_descs(VhostShadowVirtqueue *svq, hwaddr *sg,return true; } - ok =vhost_svq_translate_addr (svq, sg, iovec, num); + ok =vhost_svq_translate_addr (svq, sg, iovec, num, gpas);if (unlikely (!ok)) {return false; }
@@ -175,7 +190,8 @@ static bool vhost_svq_vring_write_descs(VhostShadowVirtqueue *svq, hwaddr *sg,static bool vhost_svq_add_split (VhostShadowVirtqueue *svq,const struct iovec *out_sg,size_t out_num,const struct iovec *in_sg,size_t in_num, -unsigned *head) +unsigned *head,const hwaddr *in_gpas, +const hwaddr *out_gpas) {unsigned avail_idx;vring_avail_t *avail = svq->vring.avail;
@@ -192,12 +208,13 @@ static bool vhost_svq_add_split(VhostShadowVirtqueue *svq, } ok =vhost_svq_vring_write_descs (svq, sgs, out_sg, out_num, in_num >0 , - false); + false, out_gpas);if (unlikely (!ok)) {return false; } - ok =vhost_svq_vring_write_descs (svq, sgs, in_sg, in_num, false, true); + ok =vhost_svq_vring_write_descs (svq, sgs, in_sg, in_num, false, true, + in_gpas);if (unlikely (!ok)) {return false; }
@@ -253,12 +270,20 @@ int vhost_svq_add(VhostShadowVirtqueue *svq, const struct iovec *out_sg,unsigned qemu_head;unsigned ndescs = in_num + out_num;bool ok; +hwaddr *in_gpas =NULL ; +hwaddr *out_gpas =NULL ;if (unlikely (ndescs >vhost_svq_available_slots (svq))) {return -ENOSPC ; } - ok =vhost_svq_add_split (svq, out_sg, out_num, in_sg, in_num, &qemu_head); +if (elem) { + in_gpas = elem->in_xlat_addr; + out_gpas = elem->out_xlat_addr; + } + + ok =vhost_svq_add_split (svq, out_sg, out_num, in_sg, in_num, &qemu_head, + in_gpas, out_gpas);if (unlikely (!ok)) {return -EINVAL ; }
Signature · 1 line
2.43.5
Re: [RFC v3 5/5] svq: Support translations via GPAs in vhost_svq_translate_addr
Quoted 9 lines · Jonah Palmer
Propagates the GPAs (in_xlat_addr/out_xlat_addr) of a VirtQueueElement to
vhost_svq_translate_addr()to translate to IOVAs viaGPA->IOVAtree when descriptors are backed by guest memory.For descriptors backed by guest memory, the translation is performed using GPAs via the
GPA->IOVAtree. GPAs are unique in the guest's address space, so this ensures unambiguous IOVA translations.
Quoted 7 lines
For descriptors not backed by guest memory, the existing
IOVA->HVAtree is used.This avoids the issue where different GPAs map to the same HVA, causing the
HVA->IOVAtranslation to potentially return an IOVA associated with the wrong intended GPA.
If SVQ is the only one needing xlat_addrs we can create a new SVQElement, following the code of VirtIOBlockReq or VirtIOSCSIReq for example.
But why do we need it? As long as svq->desc_state[qemu_head].elem != NULL the GPA is elem->in_addr or elem->out_addr, and we can use that to look into the GPA tree, isn't it? If we don't have any elem, then we need to go with "old" HVA -> IOVA lookup.
Quoted 129 lines
Signed-off-by Jonah Palmer <[email protected]>--- hw/virtio/vhost-shadow-virtqueue.c | 49 ++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 12 deletions(-)hw/virtio/vhost-shadow-virtqueue.c +37
-13@@ -78,24 +78,37 @@ uint16_t vhost_svq_available_slots(const VhostShadowVirtqueue *svq)* @vaddr: Translated IOVA addresses * @iovec: Source qemu's VA addresses * @num: Length of iovec and minimum length of vaddr +* @gpas: Descriptors' GPAs, if backed by guest memory */ static bool vhost_svq_translate_addr (const VhostShadowVirtqueue *svq,hwaddr *addrs,const struct iovec *iovec, -size_t num) +size_t num,const hwaddr *gpas) {if (num ==0 ) {return true; }for (size_t i =0 ; i < num; ++i) { -DMAMap needle = { - .translated_addr = (hwaddr )(uintptr_t )iovec[i].iov_base, - .size = iovec[i].iov_len, - }; +const DMAMap *map; +DMAMap needle;Int128 needle_last, map_last;size_t off; -const DMAMap *map =vhost_iova_tree_find_iova (svq->iova_tree, &needle); +if (gpas) { +/* Search the GPA->IOVA tree */ + needle = (DMAMap ) { + .translated_addr = gpas[i], + .size = iovec[i].iov_len, + }; + map =vhost_iova_tree_find_gpa (svq->iova_tree, &needle); + }else { +/* Searh the IOVA->HVA tree */ + needle = (DMAMap ) { + .translated_addr = (hwaddr )(uintptr_t )iovec[i].iov_base, + .size = iovec[i].iov_len, + }; + 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 @@ -132,12 +145,14 @@ static bool vhost_svq_translate_addr(const VhostShadowVirtqueue *svq,* @num: iovec length * @more_descs: True if more descriptors come in the chain * @write: True if they are writeable descriptors +* @gpas: Descriptors' GPAs, if backed by guest memory * * Return true if success, false otherwise and print error. */ static bool vhost_svq_vring_write_descs (VhostShadowVirtqueue *svq,hwaddr *sg,const struct iovec *iovec,size_t num, -bool more_descs,bool write) +bool more_descs,bool write, +const hwaddr *gpas) {uint16_t i = svq->free_head, last = svq->free_head;unsigned n;@@ -149,7 +164,7 @@ static bool vhost_svq_vring_write_descs(VhostShadowVirtqueue *svq, hwaddr *sg,return true; } - ok =vhost_svq_translate_addr (svq, sg, iovec, num); + ok =vhost_svq_translate_addr (svq, sg, iovec, num, gpas);if (unlikely (!ok)) {return false; }@@ -175,7 +190,8 @@ static bool vhost_svq_vring_write_descs(VhostShadowVirtqueue *svq, hwaddr *sg,static bool vhost_svq_add_split (VhostShadowVirtqueue *svq,const struct iovec *out_sg,size_t out_num,const struct iovec *in_sg,size_t in_num, -unsigned *head) +unsigned *head,const hwaddr *in_gpas, +const hwaddr *out_gpas) {unsigned avail_idx;vring_avail_t *avail = svq->vring.avail;@@ -192,12 +208,13 @@ static bool vhost_svq_add_split(VhostShadowVirtqueue *svq, } ok =vhost_svq_vring_write_descs (svq, sgs, out_sg, out_num, in_num >0 , - false); + false, out_gpas);if (unlikely (!ok)) {return false; } - ok =vhost_svq_vring_write_descs (svq, sgs, in_sg, in_num, false, true); + ok =vhost_svq_vring_write_descs (svq, sgs, in_sg, in_num, false, true, + in_gpas);if (unlikely (!ok)) {return false; }@@ -253,12 +270,20 @@ int vhost_svq_add(VhostShadowVirtqueue *svq, const struct iovec *out_sg,unsigned qemu_head;unsigned ndescs = in_num + out_num;bool ok; +hwaddr *in_gpas =NULL ; +hwaddr *out_gpas =NULL ;if (unlikely (ndescs >vhost_svq_available_slots (svq))) {return -ENOSPC ; } - ok =vhost_svq_add_split (svq, out_sg, out_num, in_sg, in_num, &qemu_head); +if (elem) { + in_gpas = elem->in_xlat_addr; + out_gpas = elem->out_xlat_addr; + } + + ok =vhost_svq_add_split (svq, out_sg, out_num, in_sg, in_num, &qemu_head, + in_gpas, out_gpas);if (unlikely (!ok)) {return -EINVAL ; } --2.43.5
Re: [RFC v3 5/5] svq: Support translations via GPAs in vhost_svq_translate_addr
Quoted 18 lines · Eugenio Perez Martin
Quoted 9 lines · Jonah Palmer
Propagates the GPAs (in_xlat_addr/out_xlat_addr) of a VirtQueueElement to
vhost_svq_translate_addr()to translate to IOVAs viaGPA->IOVAtree when descriptors are backed by guest memory.For descriptors backed by guest memory, the translation is performed using GPAs via the
GPA->IOVAtree. GPAs are unique in the guest's address space, so this ensures unambiguous IOVA translations.Quoted 7 lines
For descriptors not backed by guest memory, the existing
IOVA->HVAtree is used.This avoids the issue where different GPAs map to the same HVA, causing the
HVA->IOVAtranslation to potentially return an IOVA associated with the wrong intended GPA.
Quoted 9 lines
If SVQ is the only one needing
xlat_addrswe can create a new SVQElement, following the code of VirtIOBlockReq or VirtIOSCSIReq for example.But why do we need it? As long as svq->desc_state[qemu_head].elem != NULL the GPA is
elem->in_addrorelem->out_addr, and we can use that to look into the GPA tree, isn't it? If we don't have any elem, then we need to go with "old" HVA -> IOVA lookup.
Oh okay, interesting. I hadn't realized that I was actually duplicating information already available in elem->in_addr and elem->out_addr with in_xlat_addr and out_xlat_addr.
If this is indeed the case, then yea there's no need for these in_xlat_addr and out_xlat_addr members.
I'll look into this! Thanks for your comments Eugenio =]
Quoted 130 lines
Quoted 129 lines
Signed-off-by Jonah Palmer <[email protected]>--- hw/virtio/vhost-shadow-virtqueue.c | 49 ++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 12 deletions(-)hw/virtio/vhost-shadow-virtqueue.c +37
-13@@ -78,24 +78,37 @@ uint16_t vhost_svq_available_slots(const VhostShadowVirtqueue *svq)* @vaddr: Translated IOVA addresses * @iovec: Source qemu's VA addresses * @num: Length of iovec and minimum length of vaddr +* @gpas: Descriptors' GPAs, if backed by guest memory */ static bool vhost_svq_translate_addr (const VhostShadowVirtqueue *svq,hwaddr *addrs,const struct iovec *iovec, -size_t num) +size_t num,const hwaddr *gpas) {if (num ==0 ) {return true; }for (size_t i =0 ; i < num; ++i) { -DMAMap needle = { - .translated_addr = (hwaddr )(uintptr_t )iovec[i].iov_base, - .size = iovec[i].iov_len, - }; +const DMAMap *map; +DMAMap needle;Int128 needle_last, map_last;size_t off; -const DMAMap *map =vhost_iova_tree_find_iova (svq->iova_tree, &needle); +if (gpas) { +/* Search the GPA->IOVA tree */ + needle = (DMAMap ) { + .translated_addr = gpas[i], + .size = iovec[i].iov_len, + }; + map =vhost_iova_tree_find_gpa (svq->iova_tree, &needle); + }else { +/* Searh the IOVA->HVA tree */ + needle = (DMAMap ) { + .translated_addr = (hwaddr )(uintptr_t )iovec[i].iov_base, + .size = iovec[i].iov_len, + }; + 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 @@ -132,12 +145,14 @@ static bool vhost_svq_translate_addr(const VhostShadowVirtqueue *svq,* @num: iovec length * @more_descs: True if more descriptors come in the chain * @write: True if they are writeable descriptors +* @gpas: Descriptors' GPAs, if backed by guest memory * * Return true if success, false otherwise and print error. */ static bool vhost_svq_vring_write_descs (VhostShadowVirtqueue *svq,hwaddr *sg,const struct iovec *iovec,size_t num, -bool more_descs,bool write) +bool more_descs,bool write, +const hwaddr *gpas) {uint16_t i = svq->free_head, last = svq->free_head;unsigned n;@@ -149,7 +164,7 @@ static bool vhost_svq_vring_write_descs(VhostShadowVirtqueue *svq, hwaddr *sg,return true; } - ok =vhost_svq_translate_addr (svq, sg, iovec, num); + ok =vhost_svq_translate_addr (svq, sg, iovec, num, gpas);if (unlikely (!ok)) {return false; }@@ -175,7 +190,8 @@ static bool vhost_svq_vring_write_descs(VhostShadowVirtqueue *svq, hwaddr *sg,static bool vhost_svq_add_split (VhostShadowVirtqueue *svq,const struct iovec *out_sg,size_t out_num,const struct iovec *in_sg,size_t in_num, -unsigned *head) +unsigned *head,const hwaddr *in_gpas, +const hwaddr *out_gpas) {unsigned avail_idx;vring_avail_t *avail = svq->vring.avail;@@ -192,12 +208,13 @@ static bool vhost_svq_add_split(VhostShadowVirtqueue *svq, } ok =vhost_svq_vring_write_descs (svq, sgs, out_sg, out_num, in_num >0 , - false); + false, out_gpas);if (unlikely (!ok)) {return false; } - ok =vhost_svq_vring_write_descs (svq, sgs, in_sg, in_num, false, true); + ok =vhost_svq_vring_write_descs (svq, sgs, in_sg, in_num, false, true, + in_gpas);if (unlikely (!ok)) {return false; }@@ -253,12 +270,20 @@ int vhost_svq_add(VhostShadowVirtqueue *svq, const struct iovec *out_sg,unsigned qemu_head;unsigned ndescs = in_num + out_num;bool ok; +hwaddr *in_gpas =NULL ; +hwaddr *out_gpas =NULL ;if (unlikely (ndescs >vhost_svq_available_slots (svq))) {return -ENOSPC ; } - ok =vhost_svq_add_split (svq, out_sg, out_num, in_sg, in_num, &qemu_head); +if (elem) { + in_gpas = elem->in_xlat_addr; + out_gpas = elem->out_xlat_addr; + } + + ok =vhost_svq_add_split (svq, out_sg, out_num, in_sg, in_num, &qemu_head, + in_gpas, out_gpas);if (unlikely (!ok)) {return -EINVAL ; } --2.43.5