Commit 2a918911 authored by Guixin Liu's avatar Guixin Liu Committed by Jason Gunthorpe
Browse files

iommufd: Register iommufd mock devices with fwspec

Since the bus ops were retired the iommu subsystem changed to using fwspec
to match the iommu driver to the iommu device. If a device has a NULL
fwspec then it is matched to the first iommu driver with a NULL fwspec,
effectively disabling support for systems with more than one non-fwspec
iommu driver.

Thus, if the iommufd selfest are run in an x86 system that registers a
non-fwspec iommu driver they fail to bind their mock devices to the mock
iommu driver.

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

This is done by adding a new helper iommu_mock_device_add() which abuses
the internals of the fwspec system to establish a fwspec before the device
is added and is careful not to leak it. A matching dummy fwspec is
automatically added to the mock iommu driver.

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.

Link: https://patch.msgid.link/r/20250925054730.3877-1-kanie@linux.alibaba.com


Fixes: 17de3f5f ("iommu: Retire bus ops")
Signed-off-by: default avatarGuixin Liu <kanie@linux.alibaba.com>
Reviewed-by: default avatarLu Baolu <baolu.lu@linux.intel.com>
Tested-by: default avatarQinyun Tan <qinyuntan@linux.alibaba.com>
Signed-off-by: default avatarJason Gunthorpe <jgg@nvidia.com>
parent 1d235d84
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -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);
+26 −0
Original line number Diff line number Diff line
@@ -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,12 @@ 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)) {
		bus_unregister_notifier(bus, nb);
		return PTR_ERR(iommu->fwnode);
	}

	spin_lock(&iommu_device_lock);
	list_add_tail(&iommu->list, &iommu_device_list);
	spin_unlock(&iommu_device_lock);
@@ -335,9 +342,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)
+1 −1
Original line number Diff line number Diff line
@@ -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;