Skip to contentSkip to conversation

iommufd: Make vfio_compat's unmap succeed if the range is already empty

PATCH In review Jason Gunthorpe 5 participants 6 messages Nov 4, 2025 – Nov 17, 2025

Subsystems (3)
lore
Share & tools
lore

Jason Gunthorpe

author maintainer
#0 lore raw

PATCH 1/1 iommufd: Make vfio_compat's unmap succeed if the range is already empty

iommufd returns ENOENT when attempting to unmap a range that is already empty, while vfio type1 returns success. Fix vfio_compat to match.

Fixes d624d6652a65 ("iommufd: vfio container FD ioctl compatibility")
---
 drivers/iommu/iommufd/io_pagetable.c    | 12 +++---------
 drivers/iommu/iommufd/ioas.c            |  4 ++++
 tools/testing/selftests/iommu/iommufd.c |  2 ++
 3 files changed, 9 insertions(+), 9 deletions(-)
drivers/iommu/iommufd/io_pagetable.c +3 -9
index c0360c450880b8..75d60f2ad90082 100644
@@ -707,7 +707,8 @@ static int iopt_unmap_iova_range(struct io_pagetable *iopt, unsigned long start,
 	struct iopt_area *area;
 	unsigned long unmapped_bytes = 0;
 	unsigned int tries = 0;
-	int rc = -ENOENT;
+	/* If there are no mapped entries then success */
+	int rc = 0;
 
 	/*
 	 * The domains_rwsem must be held in read mode any time any area->pages
@@ -777,8 +778,6 @@ static int iopt_unmap_iova_range(struct io_pagetable *iopt, unsigned long start,
 
 		down_write(&iopt->iova_rwsem);
 	}
-	if (unmapped_bytes)
-		rc = 0;
 
 out_unlock_iova:
 	up_write(&iopt->iova_rwsem);
@@ -815,13 +814,8 @@ int iopt_unmap_iova(struct io_pagetable *iopt, unsigned long iova,
 
 int iopt_unmap_all(struct io_pagetable *iopt, unsigned long *unmapped)
 {
-	int rc;
-
-	rc = iopt_unmap_iova_range(iopt, 0, ULONG_MAX, unmapped);
 	/* If the IOVAs are empty then unmap all succeeds */
-	if (rc == -ENOENT)
-		return 0;
-	return rc;
+	return iopt_unmap_iova_range(iopt, 0, ULONG_MAX, unmapped);
 }
 
 /* The caller must always free all the nodes in the allowed_iova rb_root. */
drivers/iommu/iommufd/ioas.c +4 -0
index 1542c5fd10a85c..459a7c5169154b 100644
@@ -367,6 +367,10 @@ int iommufd_ioas_unmap(struct iommufd_ucmd *ucmd)
 				     &unmapped);
 		if (rc)
 			goto out_put;
+		if (!unmapped) {
+			rc = -ENOENT;
+			goto out_put;
+		}
 	}
 
 	cmd->length = unmapped;
tools/testing/selftests/iommu/iommufd.c +2 -0
index 3eebf5e3b974f4..bb4d33dde3c899 100644
@@ -2638,6 +2638,8 @@ TEST_F(vfio_compat_mock_domain, map)
 	ASSERT_EQ(0, ioctl(self->fd, VFIO_IOMMU_MAP_DMA, &map_cmd));
 	ASSERT_EQ(0, ioctl(self->fd, VFIO_IOMMU_UNMAP_DMA, &unmap_cmd));
 	ASSERT_EQ(BUFFER_SIZE, unmap_cmd.size);
+	/* Unmap of empty is success */
+	ASSERT_EQ(0, ioctl(self->fd, VFIO_IOMMU_UNMAP_DMA, &unmap_cmd));
 
 	/* UNMAP_FLAG_ALL requires 0 iova/size */
 	ASSERT_EQ(0, ioctl(self->fd, VFIO_IOMMU_MAP_DMA, &map_cmd));

base-commit: b09ed52db1e688eb8205b1939ca1345179ecd515

Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Jason Gunthorpe

iommufd returns ENOENT when attempting to unmap a range that is already empty, while vfio type1 returns success. Fix vfio_compat to match.

Fixes d624d6652a65 ("iommufd: vfio container FD ioctl compatibility")

Thanks Jason.

Alex

Reviewed-by on v1 1/1
#2 lore raw
In reply to Jason Gunthorpe
Quoted 7 lines · Jason Gunthorpe

iommufd returns ENOENT when attempting to unmap a range that is already empty, while vfio type1 returns success. Fix vfio_compat to match.

Fixes d624d6652a65 ("iommufd: vfio container FD ioctl compatibility")
Reviewed-by on v1 1/1
#3 lore raw
In reply to Jason Gunthorpe
Quoted 7 lines · Jason Gunthorpe

iommufd returns ENOENT when attempting to unmap a range that is already empty, while vfio type1 returns success. Fix vfio_compat to match.

Fixes d624d6652a65 ("iommufd: vfio container FD ioctl compatibility")
Quoted 5 lines
---
  drivers/iommu/iommufd/io_pagetable.c    | 12 +++---------
  drivers/iommu/iommufd/ioas.c            |  4 ++++
  tools/testing/selftests/iommu/iommufd.c |  2 ++
  3 files changed, 9 insertions(+), 9 deletions(-)
Quoted 68 lines
drivers/iommu/iommufd/io_pagetable.c +3 -9
index c0360c450880b8..75d60f2ad90082 100644
@@ -707,7 +707,8 @@ static int iopt_unmap_iova_range(struct io_pagetable *iopt, unsigned long start,
  	struct iopt_area *area;
  	unsigned long unmapped_bytes = 0;
  	unsigned int tries = 0;
-	int rc = -ENOENT;
+	/* If there are no mapped entries then success */
+	int rc = 0;
 
  	/*
  	 * The domains_rwsem must be held in read mode any time any area->pages
@@ -777,8 +778,6 @@ static int iopt_unmap_iova_range(struct io_pagetable *iopt, unsigned long start,
 
  		down_write(&iopt->iova_rwsem);
  	}
-	if (unmapped_bytes)
-		rc = 0;
 
  out_unlock_iova:
  	up_write(&iopt->iova_rwsem);
@@ -815,13 +814,8 @@ int iopt_unmap_iova(struct io_pagetable *iopt, unsigned long iova,
 
  int iopt_unmap_all(struct io_pagetable *iopt, unsigned long *unmapped)
  {
-	int rc;
-
-	rc = iopt_unmap_iova_range(iopt, 0, ULONG_MAX, unmapped);
  	/* If the IOVAs are empty then unmap all succeeds */
-	if (rc == -ENOENT)
-		return 0;
-	return rc;
+	return iopt_unmap_iova_range(iopt, 0, ULONG_MAX, unmapped);
  }
 
  /* The caller must always free all the nodes in the allowed_iova rb_root. */
drivers/iommu/iommufd/ioas.c +4 -0
index 1542c5fd10a85c..459a7c5169154b 100644
@@ -367,6 +367,10 @@ int iommufd_ioas_unmap(struct iommufd_ucmd *ucmd)
  				     &unmapped);
  		if (rc)
  			goto out_put;
+		if (!unmapped) {
+			rc = -ENOENT;
+			goto out_put;
+		}
  	}
 
  	cmd->length = unmapped;
tools/testing/selftests/iommu/iommufd.c +2 -0
index 3eebf5e3b974f4..bb4d33dde3c899 100644
@@ -2638,6 +2638,8 @@ TEST_F(vfio_compat_mock_domain, map)
  	ASSERT_EQ(0, ioctl(self->fd, VFIO_IOMMU_MAP_DMA, &map_cmd));
  	ASSERT_EQ(0, ioctl(self->fd, VFIO_IOMMU_UNMAP_DMA, &unmap_cmd));
  	ASSERT_EQ(BUFFER_SIZE, unmap_cmd.size);
+	/* Unmap of empty is success */
+	ASSERT_EQ(0, ioctl(self->fd, VFIO_IOMMU_UNMAP_DMA, &unmap_cmd));
 
  	/* UNMAP_FLAG_ALL requires 0 iova/size */
  	ASSERT_EQ(0, ioctl(self->fd, VFIO_IOMMU_MAP_DMA, &map_cmd));

base-commit: b09ed52db1e688eb8205b1939ca1345179ecd515

maintainer
#4 lore raw
In reply to Jason Gunthorpe
Quoted 3 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, November 5, 2025 2:12 AM

sorry coming to this late, but why do we leave inconsistent behavior between unmapping all vs. unmapping a range:

drivers/iommu/iommufd/io_pagetable.c int iopt_unmap_iova(struct io_pagetable *iopt, unsigned long iova, · line 819

Quoted 11 lines

int iopt_unmap_all(struct io_pagetable *iopt, unsigned long *unmapped) {

drivers/iommu/iommufd/io_pagetable.c +1 -6
-	int rc;
-
-	rc = iopt_unmap_iova_range(iopt, 0, ULONG_MAX, unmapped);
 	/* If the IOVAs are empty then unmap all succeeds */
-	if (rc == -ENOENT)
-		return 0;
-	return rc;
+	return iopt_unmap_iova_range(iopt, 0, ULONG_MAX, unmapped);
 }

here empty IOVAs succeeds, while...

drivers/iommu/iommufd/ioas.c int iommufd_ioas_unmap(struct iommufd_ucmd · line 375

Quoted 10 lines
drivers/iommu/iommufd/ioas.c +4 -0
@@ -367,6 +367,10 @@ int iommufd_ioas_unmap(struct iommufd_ucmd
 *ucmd)
 				     &unmapped);
 		if (rc)
 			goto out_put;
+		if (!unmapped) {
+			rc = -ENOENT;
+			goto out_put;
+		}
 	}

...here it's a failure.

from uAPI p.o.v. better the two scenarios are consistent?

author maintainer
#5 lore raw
Quoted 34 lines · Kevin
Quoted 3 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, November 5, 2025 2:12 AM

sorry coming to this late, but why do we leave inconsistent behavior between unmapping all vs. unmapping a range:

drivers/iommu/iommufd/io_pagetable.c int iopt_unmap_iova(struct io_pagetable *iopt, unsigned long iova, · line 819

Quoted 11 lines

int iopt_unmap_all(struct io_pagetable *iopt, unsigned long *unmapped) {

drivers/iommu/iommufd/io_pagetable.c +1 -6
-	int rc;
-
-	rc = iopt_unmap_iova_range(iopt, 0, ULONG_MAX, unmapped);
 	/* If the IOVAs are empty then unmap all succeeds */
-	if (rc == -ENOENT)
-		return 0;
-	return rc;
+	return iopt_unmap_iova_range(iopt, 0, ULONG_MAX, unmapped);
 }

here empty IOVAs succeeds, while...

drivers/iommu/iommufd/ioas.c int iommufd_ioas_unmap(struct iommufd_ucmd · line 375

Quoted 10 lines
drivers/iommu/iommufd/ioas.c +4 -0
@@ -367,6 +367,10 @@ int iommufd_ioas_unmap(struct iommufd_ucmd
 *ucmd)
 				     &unmapped);
 		if (rc)
 			goto out_put;
+		if (!unmapped) {
+			rc = -ENOENT;
+			goto out_put;
+		}
 	}

...here it's a failure.

Quoted 1 line

from uAPI p.o.v. better the two scenarios are consistent?

Maybe, but this has been like this from the start, so I don't think we should change it..

This patch was about aligning the type 1 emulation, not changing the existing iommufd uapi.

Jason