Skip to contentSkip to conversation

iommu: Fix iommu selftest running test mock domain fail

PATCH Superseded Guixin Liu 2 participants 3 messages Sep 25, 2025

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Guixin Liu

author
#0 lore raw

PATCH v4 1/1 iommu: Fix iommu selftest running test mock domain fail

When intel_iommu=on is enabled and the iommu selftests are executed, we observed multiple test case failures. Our investigation revealed that both the Intel iommu driver and the mock iommu driver registered iommu_device instances with fwnode=NULL, and both were added to the iommu_device_list. Later, when the mock iommu driver attempted to create a device, iommu_init_device attempted to retrieve the iommu_ops by searching the iommu_device_list using the provided fwnode (which was NULL).
Since the iommu_device registered by the Intel iommu driver appeared earlier in the list, it was matched first instead of the mock iommu driver's iommu_device. This ultimately caused probe_device to fail.

Fix this by alloc a software fwnode for mock iommu driver's iommu_device, and set it to the device which mock iommu driver created.

Test by "make -C toosl/testing/selftests TARGETS=iommu run_tests":
PASSED: 229 / 229 tests passed.

In addition, this issue is also can be found on amd platform, and also tested on a amd machine.

---
Changes from v3 to v4:
- Remove iommu_mock_device_abort(), rename iommu_mock_device_init() to iommu_mock_device_add(), and fold device_add() into iommu_mock_device_add().

Changes from v2 to v3:
- Add export symbol iommu_mock_device_init() and iommu_mock_device_abort() in iommu.c instead of export iommu_probe_device_lock and iommu_fwspec_free().

Changes from v1 to v2:
- Remove print when iommu_fwspec_init() fail.

 drivers/iommu/iommu-priv.h       |  2 ++
 drivers/iommu/iommu.c            | 28 ++++++++++++++++++++++++++++
 drivers/iommu/iommufd/selftest.c |  2 +-
 3 files changed, 31 insertions(+), 1 deletion(-)
drivers/iommu/iommu-priv.h +2 -0
index e236b932e766..c95394cd03a7 100644
@@ -37,6 +37,8 @@ void iommu_device_unregister_bus(struct iommu_device *iommu,
 				 const struct bus_type *bus,
 				 struct notifier_block *nb);
 
+int iommu_mock_device_add(struct device *dev, struct iommu_device *iommu);
+
 struct iommu_attach_handle *iommu_attach_handle_get(struct iommu_group *group,
 						    ioasid_t pasid,
 						    unsigned int type);
drivers/iommu/iommu.c +28 -0
index 060ebe330ee1..5d4670aa50e6 100644
@@ -304,6 +304,7 @@ void iommu_device_unregister_bus(struct iommu_device *iommu,
 				 struct notifier_block *nb)
 {
 	bus_unregister_notifier(bus, nb);
+	fwnode_remove_software_node(iommu->fwnode);
 	iommu_device_unregister(iommu);
 }
 EXPORT_SYMBOL_GPL(iommu_device_unregister_bus);
@@ -326,6 +327,14 @@ int iommu_device_register_bus(struct iommu_device *iommu,
 	if (err)
 		return err;
 
+	iommu->fwnode = fwnode_create_software_node(NULL, NULL);
+	if (IS_ERR(iommu->fwnode)) {
+		iommu->fwnode = NULL;
+		dev_err(iommu->dev, "add property failed:%d\n", err);
+		bus_unregister_notifier(bus, nb);
+		return err;
+	}
+
 	spin_lock(&iommu_device_lock);
 	list_add_tail(&iommu->list, &iommu_device_list);
 	spin_unlock(&iommu_device_lock);
@@ -335,9 +344,28 @@ int iommu_device_register_bus(struct iommu_device *iommu,
 		iommu_device_unregister_bus(iommu, bus, nb);
 		return err;
 	}
+	WRITE_ONCE(iommu->ready, true);
 	return 0;
 }
 EXPORT_SYMBOL_GPL(iommu_device_register_bus);
+
+int iommu_mock_device_add(struct device *dev, struct iommu_device *iommu)
+{
+	int rc;
+
+	mutex_lock(&iommu_probe_device_lock);
+	rc = iommu_fwspec_init(dev, iommu->fwnode);
+	mutex_unlock(&iommu_probe_device_lock);
+
+	if (rc)
+		return rc;
+
+	rc = device_add(dev);
+	if (rc)
+		iommu_fwspec_free(dev);
+	return rc;
+}
+EXPORT_SYMBOL_GPL(iommu_mock_device_add);
 #endif
 
 static struct dev_iommu *dev_iommu_get(struct device *dev)
drivers/iommu/iommufd/selftest.c +1 -1
index 61686603c769..de178827a078 100644
@@ -1126,7 +1126,7 @@ static struct mock_dev *mock_dev_create(unsigned long dev_flags)
 		goto err_put;
 	}
 
-	rc = device_add(&mdev->dev);
+	rc = iommu_mock_device_add(&mdev->dev, &mock_iommu.iommu_dev);
 	if (rc)
 		goto err_put;
 	return mdev;
Signature · 1 line
2.43.0
Reviewed-by on v4 1/1
#1 lore raw
Quoted 65 lines · Guixin Liu

When intel_iommu=on is enabled and the iommu selftests are executed, we observed multiple test case failures. Our investigation revealed that both the Intel iommu driver and the mock iommu driver registered iommu_device instances with fwnode=NULL, and both were added to the iommu_device_list. Later, when the mock iommu driver attempted to create a device, iommu_init_device attempted to retrieve the iommu_ops by searching the iommu_device_list using the provided fwnode (which was NULL).
Since the iommu_device registered by the Intel iommu driver appeared earlier in the list, it was matched first instead of the mock iommu driver's iommu_device. This ultimately caused probe_device to fail.

Fix this by alloc a software fwnode for mock iommu driver's iommu_device, and set it to the device which mock iommu driver created.

Test by "make -C toosl/testing/selftests TARGETS=iommu run_tests":
PASSED: 229 / 229 tests passed.

In addition, this issue is also can be found on amd platform, and also tested on a amd machine.

---
Changes from v3 to v4:
- Remove iommu_mock_device_abort(), rename iommu_mock_device_init() to iommu_mock_device_add(), and fold device_add() into iommu_mock_device_add().

Changes from v2 to v3:
- Add export symbol iommu_mock_device_init() and iommu_mock_device_abort() in iommu.c instead of export iommu_probe_device_lock and iommu_fwspec_free().

Changes from v1 to v2:
- Remove print when iommu_fwspec_init() fail.

  drivers/iommu/iommu-priv.h       |  2 ++
  drivers/iommu/iommu.c            | 28 ++++++++++++++++++++++++++++
  drivers/iommu/iommufd/selftest.c |  2 +-
  3 files changed, 31 insertions(+), 1 deletion(-)
drivers/iommu/iommu-priv.h +2 -0
index e236b932e766..c95394cd03a7 100644
@@ -37,6 +37,8 @@ void iommu_device_unregister_bus(struct iommu_device *iommu,
  				 const struct bus_type *bus,
  				 struct notifier_block *nb);
 
+int iommu_mock_device_add(struct device *dev, struct iommu_device *iommu);
+
  struct iommu_attach_handle *iommu_attach_handle_get(struct iommu_group *group,
  						    ioasid_t pasid,
  						    unsigned int type);
drivers/iommu/iommu.c +1 -0
index 060ebe330ee1..5d4670aa50e6 100644
@@ -304,6 +304,7 @@ void iommu_device_unregister_bus(struct iommu_device *iommu,
  				 struct notifier_block *nb)
  {
  	bus_unregister_notifier(bus, nb);
+	fwnode_remove_software_node(iommu->fwnode);
  	iommu_device_unregister(iommu);
  }
  EXPORT_SYMBOL_GPL(iommu_device_unregister_bus);

drivers/iommu/iommu.c int iommu_device_register_bus(struct iommu_device *iommu, · line 336

Quoted 10 lines
drivers/iommu/iommu.c +7 -0
@@ -326,6 +327,14 @@ int iommu_device_register_bus(struct iommu_device *iommu,
  	if (err)
  		return err;
 
+	iommu->fwnode = fwnode_create_software_node(NULL, NULL);
+	if (IS_ERR(iommu->fwnode)) {
+		iommu->fwnode = NULL;
+		dev_err(iommu->dev,"add property failed:%d\n", err);
+		bus_unregister_notifier(bus, nb);
+		return err;
+	}

Nit: I don't think we need a dev_err() here. Also, there's no need for "iommu->fwnode = NULL;" since you don't do this in iommu_device_unregister_bus().

Just simply

	if (IS_ERR(iommu->fwnode)) {
		bus_unregister_notifier(bus, nb);
		return err;
	}

?

Otherwise, it looks good to me.

author
#2 lore raw
Quoted 74 lines · Baolu Lu
Quoted 73 lines · Guixin Liu

When intel_iommu=on is enabled and the iommu selftests are executed, we observed multiple test case failures. Our investigation revealed that both the Intel iommu driver and the mock iommu driver registered iommu_device instances with fwnode=NULL, and both were added to the iommu_device_list. Later, when the mock iommu driver attempted to create a device, iommu_init_device attempted to retrieve the iommu_ops by searching the iommu_device_list using the provided fwnode (which was NULL).
Since the iommu_device registered by the Intel iommu driver appeared earlier in the list, it was matched first instead of the mock iommu driver's iommu_device. This ultimately caused probe_device to fail.

Fix this by alloc a software fwnode for mock iommu driver's iommu_device, and set it to the device which mock iommu driver created.

Test by "make -C toosl/testing/selftests TARGETS=iommu run_tests":
PASSED: 229 / 229 tests passed.

In addition, this issue is also can be found on amd platform, and also tested on a amd machine.

---
Changes from v3 to v4:
- Remove iommu_mock_device_abort(), rename iommu_mock_device_init() to iommu_mock_device_add(), and fold device_add() into iommu_mock_device_add().

Changes from v2 to v3:
- Add export symbol iommu_mock_device_init() and iommu_mock_device_abort()
in iommu.c instead of export iommu_probe_device_lock and iommu_fwspec_free().

Changes from v1 to v2:
- Remove print when iommu_fwspec_init() fail. drivers/iommu/iommu-priv.h       |  2 ++ drivers/iommu/iommu.c            | 28 ++++++++++++++++++++++++++++ drivers/iommu/iommufd/selftest.c |  2 +-

  3 files changed, 31 insertions(+), 1 deletion(-)
drivers/iommu/iommu-priv.h +1 -0
index e236b932e766..c95394cd03a7 100644
@@ -37,6 +37,8 @@ void iommu_device_unregister_bus(struct 
 iommu_device *iommu,
                    const struct bus_type *bus,
                    struct notifier_block *nb);
   +int iommu_mock_device_add(struct device *dev, struct iommu_device *iommu);
+
   struct iommu_attach_handle *iommu_attach_handle_get(struct iommu_group *group,
                               ioasid_t pasid,

                              unsigned int type);

drivers/iommu/iommu.c +1 -0
index 060ebe330ee1..5d4670aa50e6 100644
@@ -304,6 +304,7 @@ void iommu_device_unregister_bus(struct 
 iommu_device *iommu,
                    struct notifier_block *nb)
   {
       bus_unregister_notifier(bus, nb);
+    fwnode_remove_software_node(iommu->fwnode);
       iommu_device_unregister(iommu);
   }

  EXPORT_SYMBOL_GPL(iommu_device_unregister_bus);

drivers/iommu/iommu.c +2 -0
@@ -326,6 +327,14 @@ int iommu_device_register_bus(struct 
 iommu_device *iommu,
       if (err)
           return err;
   +    iommu->fwnode = fwnode_create_software_node(NULL, NULL);
+    if (IS_ERR(iommu->fwnode)) {
+        iommu->fwnode = NULL;
Quoted 8 lines

drivers/iommu/iommu.c int iommu_device_register_bus(struct iommu_device *iommu, · line 336

Quoted 4 lines
drivers/iommu/iommu.c +4 -0
+        dev_err(iommu->dev,"add property failed:%d\n", err);
+        bus_unregister_notifier(bus, nb);
+        return err;
+    }

Nit: I don't think we need a dev_err() here. Also, there's no need for "iommu->fwnode = NULL;" since you don't do this in iommu_device_unregister_bus().

Changed in v5, thanks.

Quoted 8 lines

Just simply

    if (IS_ERR(iommu->fwnode)) {
        bus_unregister_notifier(bus, nb);
        return err;
    }

Quoted 5 lines

?

Otherwise, it looks good to me.

Best Regards,

Guixin Liu