Skip to contentSkip to conversation

iommu/sva: Fix crash in iommu_sva_unbind_device()

PATCH Applied Lizhi Hou 6 participants 6 messages Mar 5 – Mar 17 IOMMU

lore
Share & tools
lore

Lizhi Hou

author
#0 lore raw

PATCH v2 1/1 iommu/sva: Fix crash in iommu_sva_unbind_device()

domain->mm->iommu_mm can be freed by iommu_domain_free():
iommu_domain_free()

    mmdrop()
      __mmdrop()
        mm_pasid_drop()

After iommu_domain_free() returns, accessing domain->mm->iommu_mm may dereference a freed mm structure, leading to a crash.

Fix this by moving the code that accesses domain->mm->iommu_mm to before the call to iommu_domain_free().

Fixes e37d5a2d60a3 ("iommu/sva: invalidate stale IOTLB entries for kernel address space")
---
 drivers/iommu/iommu-sva.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/iommu/iommu-sva.c +6 -6
index 07d64908a05f..bc7c7232a43e 100644
@@ -182,13 +182,13 @@ void iommu_sva_unbind_device(struct iommu_sva *handle)
 	iommu_detach_device_pasid(domain, dev, iommu_mm->pasid);
 	if (--domain->users == 0) {
 		list_del(&domain->next);
-		iommu_domain_free(domain);
-	}
+		if (list_empty(&iommu_mm->sva_domains)) {
+			list_del(&iommu_mm->mm_list_elm);
+			if (list_empty(&iommu_sva_mms))
+				iommu_sva_present = false;
+		}
 
-	if (list_empty(&iommu_mm->sva_domains)) {
-		list_del(&iommu_mm->mm_list_elm);
-		if (list_empty(&iommu_sva_mms))
-			iommu_sva_present = false;
+		iommu_domain_free(domain);
 	}
 
 	mutex_unlock(&iommu_sva_lock);
Signature · 1 line
2.34.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 5 lines · Lizhi Hou

domain->mm->iommu_mm can be freed by iommu_domain_free():

   iommu_domain_free()
     mmdrop()
       __mmdrop()
         mm_pasid_drop()
Quoted 8 lines

After iommu_domain_free() returns, accessing domain->mm->iommu_mm may dereference a freed mm structure, leading to a crash.

Fix this by moving the code that accesses domain->mm->iommu_mm to before the call to iommu_domain_free().

Fixes e37d5a2d60a3 ("iommu/sva: invalidate stale IOTLB entries for kernel address space")
Reviewed-by on v2 1/1
#2 lore raw
In reply to Lizhi Hou
Quoted 5 lines · Lizhi Hou

domain->mm->iommu_mm can be freed by iommu_domain_free():
iommu_domain_free()

    mmdrop()
      __mmdrop()
        mm_pasid_drop()
Quoted 8 lines

After iommu_domain_free() returns, accessing domain->mm->iommu_mm may dereference a freed mm structure, leading to a crash.

Fix this by moving the code that accesses domain->mm->iommu_mm to before the call to iommu_domain_free().

Fixes e37d5a2d60a3 ("iommu/sva: invalidate stale IOTLB entries for kernel address space")

-Vasant

Reviewed-by on v2 1/1
#3 lore raw
In reply to Lizhi Hou
Quoted 31 lines · Lizhi Hou

domain->mm->iommu_mm can be freed by iommu_domain_free():

   iommu_domain_free()
     mmdrop()
       __mmdrop()
         mm_pasid_drop()

After iommu_domain_free() returns, accessing domain->mm->iommu_mm may dereference a freed mm structure, leading to a crash.

Fix this by moving the code that accesses domain->mm->iommu_mm to before the call to iommu_domain_free().

Fixes e37d5a2d60a3 ("iommu/sva: invalidate stale IOTLB entries for kernel address space")
---
  drivers/iommu/iommu-sva.c | 12 ++++++------
  1 file changed, 6 insertions(+), 6 deletions(-)
drivers/iommu/iommu-sva.c +4 -2
index 07d64908a05f..bc7c7232a43e 100644
@@ -182,13 +182,13 @@ void iommu_sva_unbind_device(struct iommu_sva *handle)
  	iommu_detach_device_pasid(domain, dev, iommu_mm->pasid);
  	if (--domain->users == 0) {
  		list_del(&domain->next);
-		iommu_domain_free(domain);
-	}
+		if (list_empty(&iommu_mm->sva_domains)) {
+			list_del(&iommu_mm->mm_list_elm);
+			if (list_empty(&iommu_sva_mms))
+				iommu_sva_present = false;

drivers/iommu/iommu-sva.c void iommu_sva_unbind_device(struct iommu_sva *handle) · line 194

Quoted 10 lines
drivers/iommu/iommu-sva.c +2 -4
+		}
 
-	if (list_empty(&iommu_mm->sva_domains)) {
-		list_del(&iommu_mm->mm_list_elm);
-		if (list_empty(&iommu_sva_mms))
-			iommu_sva_present = false;
+		iommu_domain_free(domain);
  	}
 
  	mutex_unlock(&iommu_sva_lock);

Regards,
Yi Liu

Reviewed-by on v2 1/1
#4 lore raw
In reply to Lizhi Hou
Quoted 5 lines · Lizhi Hou

domain->mm->iommu_mm can be freed by iommu_domain_free():
iommu_domain_free()

    mmdrop()
      __mmdrop()
        mm_pasid_drop()
Quoted 7 lines

After iommu_domain_free() returns, accessing domain->mm->iommu_mm may dereference a freed mm structure, leading to a crash.

Fix this by moving the code that accesses domain->mm->iommu_mm to before the call to iommu_domain_free().

Fixes e37d5a2d60a3 ("iommu/sva: invalidate stale IOTLB entries for kernel address space")
Quoted 4 lines
---
 drivers/iommu/iommu-sva.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

Jason

maintainer Applied
#5 lore raw
In reply to Lizhi Hou
Quoted 8 lines · Lizhi Hou

domain->mm->iommu_mm can be freed by iommu_domain_free():
iommu_domain_free()

    mmdrop()
      __mmdrop()
        mm_pasid_drop()

After iommu_domain_free() returns, accessing domain->mm->iommu_mm may dereference a freed mm structure, leading to a crash.

Quoted 8 lines

Fix this by moving the code that accesses domain->mm->iommu_mm to before the call to iommu_domain_free().

Fixes e37d5a2d60a3 ("iommu/sva: invalidate stale IOTLB entries for kernel address space")
---
 drivers/iommu/iommu-sva.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

Applied, thanks.