vfio/type1: unmap cleanup

Minor changes in vfio_dma_do_unmap to improve readability, which also
simplify the subsequent unmap-all patch.  No functional change.

Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
Reviewed-by: Cornelia Huck <cohuck@redhat.com>
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
This commit is contained in:
Steve Sistare 2021-01-29 08:54:05 -08:00 committed by Alex Williamson
parent c98fe7c2a2
commit 0f53afa12b
1 changed files with 15 additions and 23 deletions

View File

@ -1074,34 +1074,28 @@ static int vfio_dma_do_unmap(struct vfio_iommu *iommu,
{ {
struct vfio_dma *dma, *dma_last = NULL; struct vfio_dma *dma, *dma_last = NULL;
size_t unmapped = 0, pgsize; size_t unmapped = 0, pgsize;
int ret = 0, retries = 0; int ret = -EINVAL, retries = 0;
unsigned long pgshift; unsigned long pgshift;
dma_addr_t iova = unmap->iova;
unsigned long size = unmap->size;
mutex_lock(&iommu->lock); mutex_lock(&iommu->lock);
pgshift = __ffs(iommu->pgsize_bitmap); pgshift = __ffs(iommu->pgsize_bitmap);
pgsize = (size_t)1 << pgshift; pgsize = (size_t)1 << pgshift;
if (unmap->iova & (pgsize - 1)) { if (iova & (pgsize - 1))
ret = -EINVAL;
goto unlock; goto unlock;
}
if (!unmap->size || unmap->size & (pgsize - 1)) { if (!size || size & (pgsize - 1))
ret = -EINVAL;
goto unlock; goto unlock;
}
if (unmap->iova + unmap->size - 1 < unmap->iova || if (iova + size - 1 < iova || size > SIZE_MAX)
unmap->size > SIZE_MAX) {
ret = -EINVAL;
goto unlock; goto unlock;
}
/* When dirty tracking is enabled, allow only min supported pgsize */ /* When dirty tracking is enabled, allow only min supported pgsize */
if ((unmap->flags & VFIO_DMA_UNMAP_FLAG_GET_DIRTY_BITMAP) && if ((unmap->flags & VFIO_DMA_UNMAP_FLAG_GET_DIRTY_BITMAP) &&
(!iommu->dirty_page_tracking || (bitmap->pgsize != pgsize))) { (!iommu->dirty_page_tracking || (bitmap->pgsize != pgsize))) {
ret = -EINVAL;
goto unlock; goto unlock;
} }
@ -1139,20 +1133,18 @@ static int vfio_dma_do_unmap(struct vfio_iommu *iommu,
* mappings within the range. * mappings within the range.
*/ */
if (iommu->v2) { if (iommu->v2) {
dma = vfio_find_dma(iommu, unmap->iova, 1); dma = vfio_find_dma(iommu, iova, 1);
if (dma && dma->iova != unmap->iova) { if (dma && dma->iova != iova)
ret = -EINVAL;
goto unlock; goto unlock;
}
dma = vfio_find_dma(iommu, unmap->iova + unmap->size - 1, 0); dma = vfio_find_dma(iommu, iova + size - 1, 0);
if (dma && dma->iova + dma->size != unmap->iova + unmap->size) { if (dma && dma->iova + dma->size != iova + size)
ret = -EINVAL;
goto unlock; goto unlock;
}
} }
while ((dma = vfio_find_dma(iommu, unmap->iova, unmap->size))) { ret = 0;
if (!iommu->v2 && unmap->iova > dma->iova) while ((dma = vfio_find_dma(iommu, iova, size))) {
if (!iommu->v2 && iova > dma->iova)
break; break;
/* /*
* Task with same address space who mapped this iova range is * Task with same address space who mapped this iova range is
@ -1190,7 +1182,7 @@ static int vfio_dma_do_unmap(struct vfio_iommu *iommu,
if (unmap->flags & VFIO_DMA_UNMAP_FLAG_GET_DIRTY_BITMAP) { if (unmap->flags & VFIO_DMA_UNMAP_FLAG_GET_DIRTY_BITMAP) {
ret = update_user_bitmap(bitmap->data, iommu, dma, ret = update_user_bitmap(bitmap->data, iommu, dma,
unmap->iova, pgsize); iova, pgsize);
if (ret) if (ret)
break; break;
} }