Skip to content

Commit ceabf05

Browse files
sina-openaigregkh
authored andcommitted
iommufd: Fix a race with concurrent allocation and unmap
commit 8602018 upstream. iopt_unmap_iova_range() releases the lock on iova_rwsem inside the loop body when getting to the more expensive unmap operations. This is fine on its own, except the loop condition is based on the first area that matches the unmap address range. If a concurrent call to map picks an area that was unmapped in previous iterations, the loop mistakenly tries to unmap it. This is reproducible by having one userspace thread map buffers and pass them to another thread that unmaps them. The problem manifests as EBUSY errors with single page mappings. Fix this by advancing the start pointer after unmapping an area. This ensures each iteration only examines the IOVA range that remains mapped, which is guaranteed not to have overlaps. Cc: stable@vger.kernel.org Fixes: 51fe614 ("iommufd: Data structure to provide IOVA to PFN mapping") Link: https://patch.msgid.link/r/CAAJpGJSR4r_ds1JOjmkqHtsBPyxu8GntoeW08Sk5RNQPmgi+tg@mail.gmail.com Signed-off-by: Sina Hassani <sina@openai.com> Reviewed-by: Kevin Tian <kevin.tian@intel.com> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 342829e commit ceabf05

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

drivers/iommu/iommufd/io_pagetable.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,16 @@ static int iopt_unmap_iova_range(struct io_pagetable *iopt, unsigned long start,
814814
unmapped_bytes += area_last - area_first + 1;
815815

816816
down_write(&iopt->iova_rwsem);
817+
818+
/*
819+
* After releasing the iova_rwsem concurrent allocation could
820+
* place new areas at IOVAs we have already unmapped. Keep
821+
* moving the start of the search forward to ignore the area
822+
* already unmapped.
823+
*/
824+
if (area_last >= last)
825+
break;
826+
start = area_last + 1;
817827
}
818828

819829
out_unlock_iova:

0 commit comments

Comments
 (0)