Skip to contentSkip to conversation

iommu: Fix default domain setup

PATCH Superseded Vasant Hegde 2 participants 4 messages Jun 14, 2023 – Jun 20, 2023 IOMMU

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iommu: Fix default domain setup

Commit 1000dccd5d13 ("iommu: Allow IOMMU_RESV_DIRECT to work on ARM") accidently restored "group->domain" to "old_domain" while keeping "group->default_domain" to new domain. Also freed new domain.

This works fine during boot as 'old_domain' is NULL. But if we try change domain via sysfs using below command then kernel crashes with "kernel NULL pointer dereference".

Change domain command :
- Assumed we have single device in the IOMMU group
- unbind device driver
- echo "<dom type>" > /sys/kernel/iommu_groups/<group id>/type

Fix above described issue by handling error path properly.

Fixes 1000dccd5d13 ("iommu: Allow IOMMU_RESV_DIRECT to work on ARM")

---
Hi Joerg,
This patch is on top of iommu/next branch.

-Vasant

 drivers/iommu/iommu.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
drivers/iommu/iommu.c +8 -6
index 9e0228ef612b..d21d0a217bdf 100644
@@ -2880,7 +2880,6 @@ static int iommu_setup_default_domain(struct iommu_group *group,
 	}
 
 	/* We must set default_domain early for __iommu_device_set_domain */
-	group->default_domain = dom;
 	if (!group->domain) {
 		/*
 		 * Drivers are not allowed to fail the first domain attach.
@@ -2896,7 +2895,6 @@ static int iommu_setup_default_domain(struct iommu_group *group,
 		ret = __iommu_group_set_domain(group, dom);
 		if (ret) {
 			iommu_domain_free(dom);
-			group->default_domain = old_dom;
 			return ret;
 		}
 	}
@@ -2915,16 +2913,20 @@ static int iommu_setup_default_domain(struct iommu_group *group,
 		}
 	}
 
+out_free:
+	group->default_domain = dom;
+
+	if (old_dom) {
+		iommu_domain_free(old_dom);
+		old_dom = NULL;
+	}
+
 err_restore:
 	if (old_dom) {
 		__iommu_group_set_domain_internal(
 			group, old_dom, IOMMU_SET_DOMAIN_MUST_SUCCEED);
 		iommu_domain_free(dom);
-		old_dom = NULL;
 	}
-out_free:
-	if (old_dom)
-		iommu_domain_free(old_dom);
 	return ret;
 }
 
Signature · 1 line
2.31.1
#1 lore raw
Quoted 10 lines · Vasant Hegde

Commit 1000dccd5d13 ("iommu: Allow IOMMU_RESV_DIRECT to work on ARM") accidently restored "group->domain" to "old_domain" while keeping "group->default_domain" to new domain. Also freed new domain.

This works fine during boot as 'old_domain' is NULL. But if we try change domain via sysfs using below command then kernel crashes with "kernel NULL pointer dereference".

Change domain command :
- Assumed we have single device in the IOMMU group

Nit: The requirement for singleton group has been removed.

Quoted 24 lines

- unbind device driver
- echo "<dom type>" > /sys/kernel/iommu_groups/<group id>/type

Fix above described issue by handling error path properly.

Fixes 1000dccd5d13 ("iommu: Allow IOMMU_RESV_DIRECT to work on ARM")

---
Hi Joerg,
This patch is on top of iommu/next branch.

(quoted diff) +0 -1
-Vasant
 
  drivers/iommu/iommu.c | 14 ++++++++------
  1 file changed, 8 insertions(+), 6 deletions(-)
drivers/iommu/iommu.c +0 -0
index 9e0228ef612b..d21d0a217bdf 100644

drivers/iommu/iommu.c static int iommu_setup_default_domain(struct iommu_group *group, · line 2882

Quoted 3 lines
drivers/iommu/iommu.c +0 -0
@@ -2880,7 +2880,6 @@ static int iommu_setup_default_domain(struct iommu_group *group,
  	}
 
  	/* We must set default_domain early for __iommu_device_set_domain */

Is the above comment still relevant? If yes, this fix seems to be problematic. Or no?

Quoted 27 lines
drivers/iommu/iommu.c +8 -2
-	group->default_domain = dom;
  	if (!group->domain) {
  		/*
  		 * Drivers are not allowed to fail the first domain attach.
@@ -2896,7 +2895,6 @@ static int iommu_setup_default_domain(struct iommu_group *group,
  		ret = __iommu_group_set_domain(group, dom);
  		if (ret) {
  			iommu_domain_free(dom);
-			group->default_domain = old_dom;
  			return ret;
  		}
  	}
@@ -2915,16 +2913,20 @@ static int iommu_setup_default_domain(struct iommu_group *group,
  		}
  	}
 
+out_free:
+	group->default_domain = dom;
+
+	if (old_dom) {
+		iommu_domain_free(old_dom);
+		old_dom = NULL;
+	}
+
  err_restore:
  	if (old_dom) {
  		__iommu_group_set_domain_internal(

drivers/iommu/iommu.c static int iommu_setup_default_domain(struct iommu_group *group, · line 2932

Quoted 10 lines
drivers/iommu/iommu.c +0 -4
  			group, old_dom, IOMMU_SET_DOMAIN_MUST_SUCCEED);
  		iommu_domain_free(dom);
-		old_dom = NULL;
  	}
-out_free:
-	if (old_dom)
-		iommu_domain_free(old_dom);
  	return ret;
  }
 

It's better to split the error path from the successful one.

Best regards,
baolu

#2 lore raw

Hi Baolu,

Quoted 5 lines · Baolu Lu
Quoted 4 lines · Vasant Hegde

Commit 1000dccd5d13 ("iommu: Allow IOMMU_RESV_DIRECT to work on ARM") accidently restored "group->domain" to "old_domain" while keeping "group->default_domain" to new domain. Also freed new domain.

Quoted 8 lines
Quoted 6 lines

This works fine during boot as 'old_domain' is NULL. But if we try change domain via sysfs using below command then kernel crashes with "kernel NULL pointer dereference".

Change domain command :
   - Assumed we have single device in the IOMMU group

Nit: The requirement for singleton group has been removed.

Yeah. I have seen those changes but forgot while writing commit message. Will fix it.

Quoted 20 lines
Quoted 20 lines

   - unbind device driver
   - echo "<dom type>" > /sys/kernel/iommu_groups/<group id>/type

Fix above described issue by handling error path properly.

Fixes 1000dccd5d13 ("iommu: Allow IOMMU_RESV_DIRECT to work on ARM")

---
Hi Joerg,
   This patch is on top of iommu/next branch.

-Vasant

  drivers/iommu/iommu.c | 14 ++++++++------

  1 file changed, 8 insertions(+), 6 deletions(-)
Quoted 11 lines

drivers/iommu/iommu.c static int iommu_setup_default_domain(struct iommu_group · line 2882

Quoted 8 lines
drivers/iommu/iommu.c +0 -0
index 9e0228ef612b..d21d0a217bdf 100644
@@ -2880,7 +2880,6 @@ static int iommu_setup_default_domain(struct iommu_group
 *group,
       }
         /* We must set default_domain early for __iommu_device_set_domain */

Is the above comment still relevant? If yes, this fix seems to be problematic. Or no?

I think we should keep below change.

Quoted 33 lines
Quoted 33 lines
drivers/iommu/iommu.c +0 -2
-    group->default_domain = dom;
       if (!group->domain) {
           /*
            * Drivers are not allowed to fail the first domain attach.
@@ -2896,7 +2895,6 @@ static int iommu_setup_default_domain(struct iommu_group
 *group,
           ret = __iommu_group_set_domain(group, dom);
           if (ret) {
               iommu_domain_free(dom);
-            group->default_domain = old_dom;
               return ret;
           }

      }

drivers/iommu/iommu.c +0 -0
@@ -2915,16 +2913,20 @@ static int iommu_setup_default_domain(struct
 iommu_group *group,
           }
       }

  +out_free:
+    group->default_domain = dom;

drivers/iommu/iommu.c +6 -0
+
+    if (old_dom) {
+        iommu_domain_free(old_dom);
+        old_dom = NULL;
+    }
+

  err_restore:
      if (old_dom) { __iommu_group_set_domain_internal(
              group, old_dom, IOMMU_SET_DOMAIN_MUST_SUCCEED); iommu_domain_free(dom);
-        old_dom = NULL;
      }

Quoted 8 lines
Quoted 6 lines

-out_free:
-    if (old_dom)
-        iommu_domain_free(old_dom);
      return ret;
  }

It's better to split the error path from the successful one.

Sure. Will fix it.

Thanks
-Vasant

#3 lore raw
Quoted 11 lines · Vasant Hegde
Quoted 10 lines

drivers/iommu/iommu.c static int iommu_setup_default_domain(struct iommu_group · line 2882

Quoted 8 lines
drivers/iommu/iommu.c +0 -0
index 9e0228ef612b..d21d0a217bdf 100644
@@ -2880,7 +2880,6 @@ static int iommu_setup_default_domain(struct iommu_group
 *group,
       }
         /* We must set default_domain early for __iommu_device_set_domain */

Is the above comment still relevant? If yes, this fix seems to be problematic. Or no?

I think we should keep below change.

If the change is necessary then we should remove above comment with sufficient justification.

Best regards,
baolu