Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH] iommufd: avoid unsigned wrap at the top of the address space
       [not found] <aw-G094ik8fz3XSO@google.com>
@ 2026-12-02 13:14 ` James Widner
  0 siblings, 0 replies; only message in thread
From: James Widner @ 2026-12-02 13:14 UTC (permalink / raw)
  To: Jason Gunthorpe, Kevin Tian
  Cc: James Widner, Samiullah Khawaja, Joerg Roedel, Will Deacon,
	Robin Murphy, iommu, stable

batch_to_domain() computes a PFN run's exclusive IOVA end using
last_iova + 1 even though last_iova is the inclusive area end. When an
area ends at ULONG_MAX this wraps to zero. The first PFN run can then be
extended over the remaining area while later runs are submitted with a
zero length. On paths that accept the zero-length continuation, the map
succeeds with translations that do not match the pinned PFN runs.

Teardown reads PFNs back from the domain. It may consequently put the
unintended page installed by the extended first run while retaining the
pin on the intended later page.

Compute each run size from the remaining inclusive distance instead. Stop
after mapping the terminal byte so the IOVA cursor is never advanced past
ULONG_MAX.

The final change passed an instrumented current-source mock-domain A/B. A
semantically equivalent Linux 6.12 backport passed a separate emulated
device-DMA A/B. One million randomized arithmetic-model cases also passed.

This finding, its tests, this fix, and this changelog were prepared with
substantial assistance from language-model-based coding tools.

Fixes: f394576eb11d ("iommufd: PFN handling for iopt_pages")
Cc: stable@vger.kernel.org
Signed-off-by: James Widner <jameswidner2@gmail.com>
---
 drivers/iommu/iommufd/pages.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/drivers/iommu/iommufd/pages.c b/drivers/iommu/iommufd/pages.c
index fcdbde007a9a..e867cb824164 100644
--- a/drivers/iommu/iommufd/pages.c
+++ b/drivers/iommu/iommufd/pages.c
@@ -512,7 +512,7 @@ static int batch_to_domain(struct pfn_batch *batch, struct iommu_domain *domain,
 	int iommu_prot = area->iommu_prot;
 	unsigned int page_offset = 0;
 	unsigned long start_iova;
-	unsigned long next_iova;
+	unsigned long size;
 	unsigned int cur = 0;
 	unsigned long iova;
 	int rc;
@@ -525,25 +525,27 @@ static int batch_to_domain(struct pfn_batch *batch, struct iommu_domain *domain,
 	/* The first index might be a partial page */
 	if (start_index == iopt_area_index(area))
 		page_offset = area->page_offset;
-	next_iova = iova = start_iova =
-		iopt_area_index_to_iova(area, start_index);
+	start_iova = iopt_area_index_to_iova(area, start_index);
+	iova = start_iova;
 	while (cur < batch->end) {
-		next_iova = min(last_iova + 1,
-				next_iova + batch->npfns[cur] * PAGE_SIZE -
-					page_offset);
+		size = batch->npfns[cur] * PAGE_SIZE - page_offset;
+		/* Avoid representing ULONG_MAX + 1 as an exclusive end. */
+		size = min(size - 1, last_iova - iova) + 1;
 		if (disable_large_pages)
 			rc = batch_iommu_map_small(
 				domain, iova,
 				PFN_PHYS(batch->pfns[cur]) + page_offset,
-				next_iova - iova, iommu_prot);
+				size, iommu_prot);
 		else
 			rc = iommu_map(domain, iova,
 				       PFN_PHYS(batch->pfns[cur]) + page_offset,
-				       next_iova - iova, iommu_prot,
+				       size, iommu_prot,
 				       GFP_KERNEL_ACCOUNT);
 		if (rc)
 			goto err_unmap;
-		iova = next_iova;
+		if (size - 1 == last_iova - iova)
+			return 0;
+		iova += size;
 		page_offset = 0;
 		cur++;
 	}
-- 
2.43.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-02 13:14 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <aw-G094ik8fz3XSO@google.com>
2026-12-02 13:14 ` [PATCH] iommufd: avoid unsigned wrap at the top of the address space James Widner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox