Skip to contentSkip to conversation

iommu: Fix SVA, iommufd, and iommupt error paths

PATCH Applied Shuai Xue 5 participants 14 messages Jul 26 – Aug 10

Subsystems (2)
lore
Share & tools
lore
author
#5 lore raw

PATCH 2/4 iommufd: Avoid locking internal accesses during unmap

In reply to Shuai Xue

iommufd_access_notify_unmap() skips internal accesses because they do not have an external unmap callback to invoke.

However, the current test calls iommufd_lock_obj() before checking whether the access is internal. If iommufd_lock_obj() succeeds, the loop then sees the internal access and continues, bypassing the matching iommufd_put_object() used by the normal unmap path. This leaks the object reference taken by iommufd_lock_obj().

Check for internal accesses first so skipped entries are never locked.

Fixes 27b77ea5feaa ("iommufd/access: Bypass access->ops->unmap for internal use")
Assisted-by Qoder:Qwen-3.8-MAX-Preview
---
 drivers/iommu/iommufd/device.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iommu/iommufd/device.c +2 -2
index 170a7005f0bc..e4537db724d5 100644
@@ -1307,8 +1307,8 @@ void iommufd_access_notify_unmap(struct io_pagetable *iopt, unsigned long iova,
 
 	xa_lock(&ioas->iopt.access_list);
 	xa_for_each(&ioas->iopt.access_list, index, access) {
-		if (!iommufd_lock_obj(&access->obj) ||
-		    iommufd_access_is_internal(access))
+		if (iommufd_access_is_internal(access) ||
+		    !iommufd_lock_obj(&access->obj))
 			continue;
 		xa_unlock(&ioas->iopt.access_list);
 
Signature · 1 line
2.39.3
author
#7 lore raw

PATCH 3/4 iommufd: Release current IOAS on xa_store() failure

In reply to Shuai Xue

iommufd_take_all_iova_rwsem() takes an object reference and the iova_rwsem write lock before storing the IOAS in the temporary ioas_list xarray.

If xa_store() fails, the current IOAS has not been inserted into ioas_list yet. iommufd_release_all_iova_rwsem() only unwinds IOAS objects already present in that xarray, so it cannot release the current IOAS.

Release the current IOAS rwsem and object reference before unwinding the previously stored entries.

Fixes 051ae5aa73d7 ("iommufd: Lock all IOAS objects")
Assisted-by Qoder:Qwen-3.8-MAX-Preview
---
 drivers/iommu/iommufd/ioas.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iommu/iommufd/ioas.c +2 -0
index fed06c2b728e..28616465ea6a 100644
@@ -427,6 +427,8 @@ static int iommufd_take_all_iova_rwsem(struct iommufd_ctx *ictx,
 
 		rc = xa_err(xa_store(ioas_list, index, ioas, GFP_KERNEL));
 		if (rc) {
+			up_write(&ioas->iopt.iova_rwsem);
+			refcount_dec(&ioas->obj.users);
 			iommufd_release_all_iova_rwsem(ictx, ioas_list);
 			return rc;
 		}
Signature · 1 line
2.39.3

Shuai Xue

author
#0 lore raw

cover iommu: Fix SVA, iommufd, and iommupt error paths

This series fixes four independent error-path/lifetime issues in the IOMMU core helpers, iommufd, and generic page table code.

Patch 1 fixes an SVA race where an attach handle becomes visible before handle->dev is initialized. A racing bind can reuse the handle and later unbind can dereference a NULL handle->dev.

Patches 2 and 3 fix iommufd reference/lock leaks. Patch 2 avoids taking an object reference for internal accesses that are skipped by the unmap notification path. Patch 3 releases the current IOAS rwsem and object reference if xa_store() fails before the IOAS is inserted into the unwind list.

Patch 4 fixes the generic page table iova_to_phys() op to return 0 on range validation failures. Returning a negative errno from a phys_addr_t function can make callers consume a bogus physical address.

All patches are small fixes with Fixes tags and Cc: stable.

Shuai Xue (4):
iommu/sva: Set handle->dev before the SVA handle is visible
iommufd: Avoid locking internal accesses during unmap
iommufd: Release current IOAS on xa_store() failure
iommupt: Return zero for invalid iova_to_phys() ranges

 drivers/iommu/generic_pt/iommu_pt.h | 2 +-
 drivers/iommu/iommu-sva.c           | 2 +-
 drivers/iommu/iommufd/device.c      | 4 ++--
 drivers/iommu/iommufd/ioas.c        | 2 ++
 4 files changed, 6 insertions(+), 4 deletions(-)
Signature · 1 line
2.39.3
author
#1 lore raw

PATCH 1/4 iommu/sva: Set handle->dev before the SVA handle is visible

iommu_attach_device_pasid() installs the new SVA attach handle in the group PASID lookup before iommu_sva_bind_device() returns. A concurrent bind can therefore find and reuse the same handle after iommu_sva_lock is dropped.

handle->dev was initialized after dropping iommu_sva_lock. This leaves a window where a racing bind can return a handle whose dev pointer is still NULL. A subsequent iommu_sva_unbind_device() can then dereference it via handle->dev->iommu_group.

Initialize handle->dev before releasing iommu_sva_lock so any visible SVA handle is fully initialized.

Fixes be51b1d6bbff ("iommu/sva: Refactoring iommu_sva_bind/unbind_device()")
Assisted-by Qoder:Qwen-3.8-MAX-Preview
---
 drivers/iommu/iommu-sva.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/iommu-sva.c +1 -1
index bc7c7232a43e..9742cb593577 100644
@@ -145,8 +145,8 @@ struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm
 	list_add(&domain->next, &iommu_mm->sva_domains);
 out:
 	refcount_set(&handle->users, 1);
-	mutex_unlock(&iommu_sva_lock);
 	handle->dev = dev;
+	mutex_unlock(&iommu_sva_lock);
 	return handle;
 
 out_free_domain:
Signature · 1 line
2.39.3
author
#9 lore raw

PATCH 4/4 iommupt: Return zero for invalid iova_to_phys() ranges

In reply to Shuai Xue

DOMAIN_NS(iova_to_phys) returns a phys_addr_t and follows the IOMMU iova_to_phys() convention of returning 0 when there is no valid translation.

make_range() returns a negative errno if the input IOVA cannot be represented by this page table format. Returning that errno directly casts it to phys_addr_t. Callers treat zero as no translation and use non-zero values as physical addresses, so this can make them consume a bogus physical address instead of seeing a failed translation.

Match the page-table walk failure path and return 0 for range validation failures too.

Fixes 9d4c274cd7d5 ("iommupt: Add iova_to_phys op")
Assisted-by Qoder:Qwen-3.8-MAX-Preview
---
 drivers/iommu/generic_pt/iommu_pt.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/generic_pt/iommu_pt.h +1 -1
index c2752151c80a..07ec2b3ab986 100644
@@ -224,7 +224,7 @@ phys_addr_t DOMAIN_NS(iova_to_phys)(struct iommu_domain *domain,
 
 	ret = make_range(common_from_iommu(iommu_table), &range, iova, 1);
 	if (ret)
-		return ret;
+		return 0;
 
 	ret = pt_walk_range(&range, __iova_to_phys, &res);
 	/* PHYS_ADDR_MAX would be a better error code */
Signature · 1 line
2.39.3
maintainer Reviewed-by on v1 1/4
#2 lore raw

RE: [PATCH 1/4] iommu/sva: Set handle->dev before the SVA handle is visible

In reply to Shuai Xue
Quoted 8 lines

From: Shuai Xue <[email protected]>
Sent: Sunday, July 26, 2026 3:43 PM

iommu_attach_device_pasid() installs the new SVA attach handle in the group PASID lookup before iommu_sva_bind_device() returns. A concurrent bind can therefore find and reuse the same handle after iommu_sva_lock is dropped.

Quoted 4 lines

handle->dev was initialized after dropping iommu_sva_lock. This leaves a window where a racing bind can return a handle whose dev pointer is still NULL. A subsequent iommu_sva_unbind_device() can then dereference it via handle->dev->iommu_group.

ideally this will bail out early:

        mutex_lock(&iommu_sva_lock);
        if (!refcount_dec_and_test(&handle->users)) {
                mutex_unlock(&iommu_sva_lock);
                return;
        }
Quoted 9 lines

Initialize handle->dev before releasing iommu_sva_lock so any visible SVA handle is fully initialized.

Fixes be51b1d6bbff ("iommu/sva: Refactoring

iommu_sva_bind/unbind_device()")

Assisted-by Qoder:Qwen-3.8-MAX-Preview

but the fix is correct.

maintainer Reviewed-by on v1 2/4
#6 lore raw

RE: [PATCH 2/4] iommufd: Avoid locking internal accesses during unmap

In reply to Shuai Xue
Quoted 12 lines

From: Shuai Xue <[email protected]>
Sent: Sunday, July 26, 2026 3:44 PM

iommufd_access_notify_unmap() skips internal accesses because they do not have an external unmap callback to invoke.

However, the current test calls iommufd_lock_obj() before checking whether the access is internal. If iommufd_lock_obj() succeeds, the loop then sees the internal access and continues, bypassing the matching iommufd_put_object() used by the normal unmap path. This leaks the object reference taken by iommufd_lock_obj().

Quoted 7 lines

Check for internal accesses first so skipped entries are never locked.

Fixes 27b77ea5feaa ("iommufd/access: Bypass access->ops->unmap for

internal use")

Assisted-by Qoder:Qwen-3.8-MAX-Preview
maintainer Reviewed-by on v1 3/4
#8 lore raw

RE: [PATCH 3/4] iommufd: Release current IOAS on xa_store() failure

In reply to Shuai Xue
Quoted 12 lines

From: Shuai Xue <[email protected]>
Sent: Sunday, July 26, 2026 3:44 PM

iommufd_take_all_iova_rwsem() takes an object reference and the iova_rwsem write lock before storing the IOAS in the temporary ioas_list xarray.

If xa_store() fails, the current IOAS has not been inserted into ioas_list yet. iommufd_release_all_iova_rwsem() only unwinds IOAS objects already present in that xarray, so it cannot release the current IOAS.

Quoted 7 lines

Release the current IOAS rwsem and object reference before unwinding the previously stored entries.

Fixes 051ae5aa73d7 ("iommufd: Lock all IOAS objects")
Assisted-by Qoder:Qwen-3.8-MAX-Preview
maintainer Reviewed-by on v1 4/4
#10 lore raw

RE: [PATCH 4/4] iommupt: Return zero for invalid iova_to_phys() ranges

In reply to Shuai Xue
Quoted 13 lines

From: Shuai Xue <[email protected]>
Sent: Sunday, July 26, 2026 3:44 PM

DOMAIN_NS(iova_to_phys) returns a phys_addr_t and follows the IOMMU iova_to_phys() convention of returning 0 when there is no valid translation.

make_range() returns a negative errno if the input IOVA cannot be represented by this page table format. Returning that errno directly casts it to phys_addr_t. Callers treat zero as no translation and use non-zero values as physical addresses, so this can make them consume a bogus physical address instead of seeing a failed translation.

Quoted 7 lines

Match the page-table walk failure path and return 0 for range validation failures too.

Fixes 9d4c274cd7d5 ("iommupt: Add iova_to_phys op")
Assisted-by Qoder:Qwen-3.8-MAX-Preview
author
#3 lore raw

Re: [PATCH 1/4] iommu/sva: Set handle->dev before the SVA handle is visible

In reply to Tian, Kevin
Quoted 13 lines · Kevin
Quoted 8 lines

From: Shuai Xue <[email protected]>
Sent: Sunday, July 26, 2026 3:43 PM

iommu_attach_device_pasid() installs the new SVA attach handle in the group PASID lookup before iommu_sva_bind_device() returns. A concurrent bind can therefore find and reuse the same handle after iommu_sva_lock is dropped.

Quoted 4 lines

handle->dev was initialized after dropping iommu_sva_lock. This leaves a window where a racing bind can return a handle whose dev pointer is still NULL. A subsequent iommu_sva_unbind_device() can then dereference it via handle->dev->iommu_group.

Quoted 8 lines

ideally this will bail out early:

         mutex_lock(&iommu_sva_lock);
         if (!refcount_dec_and_test(&handle->users)) {
                 mutex_unlock(&iommu_sva_lock);
                 return;
         }

Thanks Kevin for pointing this out.

Yes, the non-last reference case should bail out before using handle->dev. I will wait a bit for other review comments and then decide whether a v2 is needed to clarify the commit message.

Quoted 10 lines
Quoted 9 lines

Initialize handle->dev before releasing iommu_sva_lock so any visible SVA handle is fully initialized.

Fixes be51b1d6bbff ("iommu/sva: Refactoring

iommu_sva_bind/unbind_device()")

Assisted-by Qoder:Qwen-3.8-MAX-Preview
Quoted 3 lines

but the fix is correct.

Thanks for the review.

Best Regards,
Shuai

Reviewed-by on v1 1/4
#4 lore raw

Re: [PATCH 1/4] iommu/sva: Set handle->dev before the SVA handle is visible

In reply to Shuai Xue
Quoted 13 lines · Shuai Xue

iommu_attach_device_pasid() installs the new SVA attach handle in the group PASID lookup before iommu_sva_bind_device() returns. A concurrent bind can therefore find and reuse the same handle after iommu_sva_lock is dropped.

handle->dev was initialized after dropping iommu_sva_lock. This leaves a window where a racing bind can return a handle whose dev pointer is still NULL. A subsequent iommu_sva_unbind_device() can then dereference it via handle->dev->iommu_group.

Initialize handle->dev before releasing iommu_sva_lock so any visible SVA handle is fully initialized.

Quoted 7 lines
Fixes be51b1d6bbff ("iommu/sva: Refactoring iommu_sva_bind/unbind_device()")
Assisted-by Qoder:Qwen-3.8-MAX-Preview
---
  drivers/iommu/iommu-sva.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Reviewed-by on v1 4/4
#11 lore raw

Re: [PATCH 4/4] iommupt: Return zero for invalid iova_to_phys() ranges

In reply to Shuai Xue
Quoted 13 lines · Shuai Xue

DOMAIN_NS(iova_to_phys) returns a phys_addr_t and follows the IOMMU iova_to_phys() convention of returning 0 when there is no valid translation.

make_range() returns a negative errno if the input IOVA cannot be represented by this page table format. Returning that errno directly casts it to phys_addr_t. Callers treat zero as no translation and use non-zero values as physical addresses, so this can make them consume a bogus physical address instead of seeing a failed translation.

Match the page-table walk failure path and return 0 for range validation failures too.

Quoted 7 lines
Fixes 9d4c274cd7d5 ("iommupt: Add iova_to_phys op")
Assisted-by Qoder:Qwen-3.8-MAX-Preview
---
  drivers/iommu/generic_pt/iommu_pt.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
maintainer Reviewed-by on the v1 series
#12 lore raw
In reply to Shuai Xue
Quoted 7 lines

All patches are small fixes with Fixes tags and Cc: stable.

Shuai Xue (4):
iommu/sva: Set handle->dev before the SVA handle is visible
iommufd: Avoid locking internal accesses during unmap
iommufd: Release current IOAS on xa_store() failure
iommupt: Return zero for invalid iova_to_phys() ranges

Joerg, can you pick this up please?

Thanks,

Signature · 1 line
Jason
maintainer Applied
#13 lore raw
In reply to Shuai Xue
Quoted 5 lines · Shuai Xue

Shuai Xue (4):
iommu/sva: Set handle->dev before the SVA handle is visible
iommufd: Avoid locking internal accesses during unmap
iommufd: Release current IOAS on xa_store() failure
iommupt: Return zero for invalid iova_to_phys() ranges

Applied, thanks.