[PATCH V1 1/5] vfio: maintain dma_list order

Steve Sistare
2021-01-05T16:05:07Z

Keep entries properly sorted in the dma_list rb_tree so that iterating
over multiple entries across a range with gaps works, without requiring
one to delete each visited entry as in vfio_dma_do_unmap.

Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
---
drivers/vfio/vfio_iommu_type1.c | 18 +++++++++++-------
1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
index 5fbf0c1..02228d0 100644
--- a/drivers/vfio/vfio_iommu_type1.c
+++ b/drivers/vfio/vfio_iommu_type1.c
@@ -157,20 +157,24 @@ static struct vfio_group *vfio_iommu_find_iommu_group(struct vfio_iommu *iommu,
static struct vfio_dma *vfio_find_dma(struct vfio_iommu *iommu,
dma_addr_t start, size_t size)
{
+ struct vfio_dma *res = 0;
struct rb_node *node = iommu->dma_list.rb_node;

while (node) {
struct vfio_dma *dma = rb_entry(node, struct vfio_dma, node);

- if (start + size <= dma->iova)
+ if (start < dma->iova + dma->size) {
+ res = dma;
+ if (start >= dma->iova)
+ break;
node = node->rb_left;
- else if (start >= dma->iova + dma->size)
+ } else {
node = node->rb_right;
- else
- return dma;
+ }
}
-
- return NULL;
+ if (res && size && res->iova >= start + size)
+ res = 0;
+ return res;
}

static void vfio_link_dma(struct vfio_iommu *iommu, struct vfio_dma *new)
@@ -182,7 +186,7 @@ static void vfio_link_dma(struct vfio_iommu *iommu, struct vfio_dma *new)
parent = *link;
dma = rb_entry(parent, struct vfio_dma, node);

- if (new->iova + new->size <= dma->iova)
+ if (new->iova < dma->iova)
link = &(*link)->rb_left;
else
link = &(*link)->rb_right;
--
1.8.3.1