Skip to content

Commit

Permalink
vfio/type1: unmap cleanup
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Steve Sistare authored and Alex Williamson committed Feb 1, 2021
1 parent c98fe7c commit 0f53afa
Showing 1 changed file with 15 additions and 23 deletions.
38 changes: 15 additions & 23 deletions drivers/vfio/vfio_iommu_type1.c
Original file line number Diff line number Diff line change
Expand Up @@ -1074,34 +1074,28 @@ static int vfio_dma_do_unmap(struct vfio_iommu *iommu,
{
struct vfio_dma *dma, *dma_last = NULL;
size_t unmapped = 0, pgsize;
int ret = 0, retries = 0;
int ret = -EINVAL, retries = 0;
unsigned long pgshift;
dma_addr_t iova = unmap->iova;
unsigned long size = unmap->size;

mutex_lock(&iommu->lock);

pgshift = __ffs(iommu->pgsize_bitmap);
pgsize = (size_t)1 << pgshift;

if (unmap->iova & (pgsize - 1)) {
ret = -EINVAL;
if (iova & (pgsize - 1))
goto unlock;
}

if (!unmap->size || unmap->size & (pgsize - 1)) {
ret = -EINVAL;
if (!size || size & (pgsize - 1))
goto unlock;
}

if (unmap->iova + unmap->size - 1 < unmap->iova ||
unmap->size > SIZE_MAX) {
ret = -EINVAL;
if (iova + size - 1 < iova || size > SIZE_MAX)
goto unlock;
}

/* When dirty tracking is enabled, allow only min supported pgsize */
if ((unmap->flags & VFIO_DMA_UNMAP_FLAG_GET_DIRTY_BITMAP) &&
(!iommu->dirty_page_tracking || (bitmap->pgsize != pgsize))) {
ret = -EINVAL;
goto unlock;
}

Expand Down Expand Up @@ -1139,20 +1133,18 @@ static int vfio_dma_do_unmap(struct vfio_iommu *iommu,
* mappings within the range.
*/
if (iommu->v2) {
dma = vfio_find_dma(iommu, unmap->iova, 1);
if (dma && dma->iova != unmap->iova) {
ret = -EINVAL;
dma = vfio_find_dma(iommu, iova, 1);
if (dma && dma->iova != iova)
goto unlock;
}
dma = vfio_find_dma(iommu, unmap->iova + unmap->size - 1, 0);
if (dma && dma->iova + dma->size != unmap->iova + unmap->size) {
ret = -EINVAL;

dma = vfio_find_dma(iommu, iova + size - 1, 0);
if (dma && dma->iova + dma->size != iova + size)
goto unlock;
}
}

while ((dma = vfio_find_dma(iommu, unmap->iova, unmap->size))) {
if (!iommu->v2 && unmap->iova > dma->iova)
ret = 0;
while ((dma = vfio_find_dma(iommu, iova, size))) {
if (!iommu->v2 && iova > dma->iova)
break;
/*
* Task with same address space who mapped this iova range is
Expand Down Expand Up @@ -1190,7 +1182,7 @@ static int vfio_dma_do_unmap(struct vfio_iommu *iommu,

if (unmap->flags & VFIO_DMA_UNMAP_FLAG_GET_DIRTY_BITMAP) {
ret = update_user_bitmap(bitmap->data, iommu, dma,
unmap->iova, pgsize);
iova, pgsize);
if (ret)
break;
}
Expand Down

0 comments on commit 0f53afa

Please sign in to comment.