vhost-iova-tree: Implement an IOVA-only tree

Creates and supports an IOVA-only tree to support a SVQ IOVA->HVA and
GPA->IOVA tree for host-only and guest-backed memory, respectively, in
the next patch.

The IOVA allocator still allocates an IOVA range but now adds this range
to the IOVA-only tree as well as to the full IOVA->HVA tree.

In the next patch, the full IOVA->HVA tree will be split into a partial
SVQ IOVA->HVA tree and a GPA->IOVA tree. The motivation behind having an
IOVA-only tree was to have a single tree that would keep track of all
allocated IOVA ranges between the partial SVQ IOVA->HVA and GPA->IOVA
trees.

Signed-off-by: Jonah Palmer <jonah.palmer@oracle.com>
Acked-by: Eugenio Pérez <eperezma@redhat.com>
Tested-by: Lei Yang <leiyang@redhat.com>
Message-Id: <20250217144936.3589907-2-jonah.palmer@oracle.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
This commit is contained in:
Jonah Palmer 2025-02-17 09:49:32 -05:00 committed by Michael S. Tsirkin
parent 3684717b74
commit 92cf61e708
4 changed files with 51 additions and 17 deletions

View file

@ -28,6 +28,9 @@ struct VhostIOVATree {
/* IOVA address to qemu memory maps. */ /* IOVA address to qemu memory maps. */
IOVATree *iova_taddr_map; IOVATree *iova_taddr_map;
/* Allocated IOVA addresses */
IOVATree *iova_map;
}; };
/** /**
@ -44,6 +47,7 @@ VhostIOVATree *vhost_iova_tree_new(hwaddr iova_first, hwaddr iova_last)
tree->iova_last = iova_last; tree->iova_last = iova_last;
tree->iova_taddr_map = iova_tree_new(); tree->iova_taddr_map = iova_tree_new();
tree->iova_map = iova_tree_new();
return tree; return tree;
} }
@ -53,6 +57,7 @@ VhostIOVATree *vhost_iova_tree_new(hwaddr iova_first, hwaddr iova_last)
void vhost_iova_tree_delete(VhostIOVATree *iova_tree) void vhost_iova_tree_delete(VhostIOVATree *iova_tree)
{ {
iova_tree_destroy(iova_tree->iova_taddr_map); iova_tree_destroy(iova_tree->iova_taddr_map);
iova_tree_destroy(iova_tree->iova_map);
g_free(iova_tree); g_free(iova_tree);
} }
@ -75,6 +80,7 @@ const DMAMap *vhost_iova_tree_find_iova(const VhostIOVATree *tree,
* *
* @tree: The iova tree * @tree: The iova tree
* @map: The iova map * @map: The iova map
* @taddr: The translated address (HVA)
* *
* Returns: * Returns:
* - IOVA_OK if the map fits in the container * - IOVA_OK if the map fits in the container
@ -83,19 +89,26 @@ const DMAMap *vhost_iova_tree_find_iova(const VhostIOVATree *tree,
* *
* It returns assignated iova in map->iova if return value is VHOST_DMA_MAP_OK. * It returns assignated iova in map->iova if return value is VHOST_DMA_MAP_OK.
*/ */
int vhost_iova_tree_map_alloc(VhostIOVATree *tree, DMAMap *map) int vhost_iova_tree_map_alloc(VhostIOVATree *tree, DMAMap *map, hwaddr taddr)
{ {
int ret;
/* Some vhost devices do not like addr 0. Skip first page */ /* Some vhost devices do not like addr 0. Skip first page */
hwaddr iova_first = tree->iova_first ?: qemu_real_host_page_size(); hwaddr iova_first = tree->iova_first ?: qemu_real_host_page_size();
if (map->translated_addr + map->size < map->translated_addr || if (taddr + map->size < taddr || map->perm == IOMMU_NONE) {
map->perm == IOMMU_NONE) {
return IOVA_ERR_INVALID; return IOVA_ERR_INVALID;
} }
/* Allocate a node in IOVA address */ /* Allocate a node in the IOVA-only tree */
return iova_tree_alloc_map(tree->iova_taddr_map, map, iova_first, ret = iova_tree_alloc_map(tree->iova_map, map, iova_first, tree->iova_last);
tree->iova_last); if (unlikely(ret != IOVA_OK)) {
return ret;
}
/* Insert a node in the IOVA->HVA tree */
map->translated_addr = taddr;
return iova_tree_insert(tree->iova_taddr_map, map);
} }
/** /**
@ -107,4 +120,5 @@ int vhost_iova_tree_map_alloc(VhostIOVATree *tree, DMAMap *map)
void vhost_iova_tree_remove(VhostIOVATree *iova_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_taddr_map, map);
iova_tree_remove(iova_tree->iova_map, map);
} }

View file

@ -21,7 +21,8 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(VhostIOVATree, vhost_iova_tree_delete);
const DMAMap *vhost_iova_tree_find_iova(const VhostIOVATree *iova_tree, const DMAMap *vhost_iova_tree_find_iova(const VhostIOVATree *iova_tree,
const DMAMap *map); const DMAMap *map);
int vhost_iova_tree_map_alloc(VhostIOVATree *iova_tree, DMAMap *map); int vhost_iova_tree_map_alloc(VhostIOVATree *iova_tree, DMAMap *map,
hwaddr taddr);
void vhost_iova_tree_remove(VhostIOVATree *iova_tree, DMAMap map); void vhost_iova_tree_remove(VhostIOVATree *iova_tree, DMAMap map);
#endif #endif

View file

@ -360,14 +360,20 @@ static void vhost_vdpa_listener_region_add(MemoryListener *listener,
llsize = int128_sub(llend, int128_make64(iova)); llsize = int128_sub(llend, int128_make64(iova));
if (s->shadow_data) { if (s->shadow_data) {
int r; int r;
hwaddr hw_vaddr = (hwaddr)(uintptr_t)vaddr;
mem_region.translated_addr = (hwaddr)(uintptr_t)vaddr,
mem_region.size = int128_get64(llsize) - 1, mem_region.size = int128_get64(llsize) - 1,
mem_region.perm = IOMMU_ACCESS_FLAG(true, section->readonly), mem_region.perm = IOMMU_ACCESS_FLAG(true, section->readonly),
r = vhost_iova_tree_map_alloc(s->iova_tree, &mem_region); r = vhost_iova_tree_map_alloc(s->iova_tree, &mem_region, hw_vaddr);
if (unlikely(r != IOVA_OK)) { if (unlikely(r != IOVA_OK)) {
error_report("Can't allocate a mapping (%d)", r); error_report("Can't allocate a mapping (%d)", r);
if (mem_region.translated_addr == hw_vaddr) {
error_report("Insertion to IOVA->HVA tree failed");
/* Remove the mapping from the IOVA-only tree */
goto fail_map;
}
goto fail; goto fail;
} }
@ -1142,16 +1148,23 @@ static void vhost_vdpa_svq_unmap_rings(struct vhost_dev *dev,
* *
* @v: Vhost-vdpa device * @v: Vhost-vdpa device
* @needle: The area to search iova * @needle: The area to search iova
* @taddr: The translated address (HVA)
* @errorp: Error pointer * @errorp: Error pointer
*/ */
static bool vhost_vdpa_svq_map_ring(struct vhost_vdpa *v, DMAMap *needle, static bool vhost_vdpa_svq_map_ring(struct vhost_vdpa *v, DMAMap *needle,
Error **errp) hwaddr taddr, Error **errp)
{ {
int r; int r;
r = vhost_iova_tree_map_alloc(v->shared->iova_tree, needle); r = vhost_iova_tree_map_alloc(v->shared->iova_tree, needle, taddr);
if (unlikely(r != IOVA_OK)) { if (unlikely(r != IOVA_OK)) {
error_setg(errp, "Cannot allocate iova (%d)", r); error_setg(errp, "Cannot allocate iova (%d)", r);
if (needle->translated_addr == taddr) {
error_append_hint(errp, "Insertion to IOVA->HVA tree failed");
/* Remove the mapping from the IOVA-only tree */
vhost_iova_tree_remove(v->shared->iova_tree, *needle);
}
return false; return false;
} }
@ -1192,11 +1205,11 @@ static bool vhost_vdpa_svq_map_rings(struct vhost_dev *dev,
vhost_svq_get_vring_addr(svq, &svq_addr); vhost_svq_get_vring_addr(svq, &svq_addr);
driver_region = (DMAMap) { driver_region = (DMAMap) {
.translated_addr = svq_addr.desc_user_addr,
.size = driver_size - 1, .size = driver_size - 1,
.perm = IOMMU_RO, .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)) { if (unlikely(!ok)) {
error_prepend(errp, "Cannot create vq driver region: "); error_prepend(errp, "Cannot create vq driver region: ");
return false; return false;
@ -1206,11 +1219,11 @@ static bool vhost_vdpa_svq_map_rings(struct vhost_dev *dev,
addr->avail_user_addr = driver_region.iova + avail_offset; addr->avail_user_addr = driver_region.iova + avail_offset;
device_region = (DMAMap) { device_region = (DMAMap) {
.translated_addr = svq_addr.used_user_addr,
.size = device_size - 1, .size = device_size - 1,
.perm = IOMMU_RW, .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)) { if (unlikely(!ok)) {
error_prepend(errp, "Cannot create vq device region: "); error_prepend(errp, "Cannot create vq device region: ");
vhost_vdpa_svq_unmap_ring(v, driver_region.translated_addr); vhost_vdpa_svq_unmap_ring(v, driver_region.translated_addr);

View file

@ -510,14 +510,20 @@ static int vhost_vdpa_cvq_map_buf(struct vhost_vdpa *v, void *buf, size_t size,
bool write) bool write)
{ {
DMAMap map = {}; DMAMap map = {};
hwaddr taddr = (hwaddr)(uintptr_t)buf;
int r; int r;
map.translated_addr = (hwaddr)(uintptr_t)buf;
map.size = size - 1; map.size = size - 1;
map.perm = write ? IOMMU_RW : IOMMU_RO, map.perm = write ? IOMMU_RW : IOMMU_RO,
r = vhost_iova_tree_map_alloc(v->shared->iova_tree, &map); r = vhost_iova_tree_map_alloc(v->shared->iova_tree, &map, taddr);
if (unlikely(r != IOVA_OK)) { if (unlikely(r != IOVA_OK)) {
error_report("Cannot map injected element"); error_report("Cannot map injected element");
if (map.translated_addr == taddr) {
error_report("Insertion to IOVA->HVA tree failed");
/* Remove the mapping from the IOVA-only tree */
goto dma_map_err;
}
return r; return r;
} }