]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
iommufd: Make vfio_compat's unmap succeed if the range is already empty
authorJason Gunthorpe <jgg@nvidia.com>
Tue, 4 Nov 2025 18:11:49 +0000 (14:11 -0400)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Mon, 24 Nov 2025 09:35:45 +0000 (10:35 +0100)
[ Upstream commit afb47765f9235181fddc61c8633b5a8cfae29fd2 ]

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")
Link: https://patch.msgid.link/r/0-v1-76be45eff0be+5d-iommufd_unmap_compat_jgg@nvidia.com
Reviewed-by: Nicolin Chen <nicolinc@nvidia.com>
Reviewed-by: Alex Mastro <amastro@fb.com>
Reported-by: Alex Mastro <amastro@fb.com>
Closes: https://lore.kernel.org/r/aP0S5ZF9l3sWkJ1G@devgpu012.nha5.facebook.com
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/iommu/iommufd/io_pagetable.c
drivers/iommu/iommufd/ioas.c
tools/testing/selftests/iommu/iommufd.c

index 067222b238b7e1c5f5bc53a585b7be254a89ded1..f0f094cc7e520af826efbb9542dcc03d2d7a0134 100644 (file)
@@ -660,7 +660,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
@@ -724,8 +725,6 @@ again:
 
                down_write(&iopt->iova_rwsem);
        }
-       if (unmapped_bytes)
-               rc = 0;
 
 out_unlock_iova:
        up_write(&iopt->iova_rwsem);
@@ -762,13 +761,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. */
index 2c4b2bb11e78ce4de21450a3b2e3c0efe4da7594..4885293bd94f1ee74b56133fad89a0d122112f33 100644 (file)
@@ -317,6 +317,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;
index a81c22d520070d432bc5b37b86d506a2aa5a6434..7a535c590245f0a48dc8f919551378a6a19d2afa 100644 (file)
@@ -2329,6 +2329,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));