Subject: Re: [PATCH v5 15/25] iommufd/selftest: Make the mock iommu driver into a real driver

From: Jason Gunthorpe <[email-masked]>
Date: Wed, 2 Aug 2023 20:50:24 -0300

On Mon, Jul 24, 2023 at 02:22:05PM -0300, Jason Gunthorpe wrote:

> -void __init iommufd_test_init(void)
> +int __init iommufd_test_init(void)
>  {
> +	struct platform_device_info pdevinfo = {
> +		.name = "iommufd_selftest_iommu",
> +	};
> +	int rc;
> +
>  	dbgfs_root =
>  		fault_create_debugfs_attr("fail_iommufd", NULL, &fail_iommufd);
> -	WARN_ON(bus_register(&iommufd_mock_bus_type));
> +
> +	selftest_iommu_dev = platform_device_register_full(&pdevinfo);
> +	if (IS_ERR(selftest_iommu_dev)) {
> +		rc = PTR_ERR(selftest_iommu_dev);
> +		goto err_dbgfs;
> +	}
> +
> +	rc = bus_register(&iommufd_mock_bus_type.bus);
> +	if (rc)
> +		goto err_platform;
> +
> +	mock_iommu_device.dev = &selftest_iommu_dev->dev;
> +	rc = iommu_device_register_bus(&mock_iommu_device, &mock_ops,
> +				  &iommufd_mock_bus_type.bus,
> +				  &iommufd_mock_bus_type.nb);
> +	if (rc)
> +		goto err_bus;
> +	return 0;
> +
> +err_bus:
> +	bus_unregister(&iommufd_mock_bus_type.bus);
> +err_platform:
> +	platform_device_del(selftest_iommu_dev);
> +err_dbgfs:
> +	debugfs_remove_recursive(dbgfs_root);
> +	return rc;
>  }
>  
>  void iommufd_test_exit(void)
>  {
> +	iommu_device_unregister_bus(&mock_iommu_device,
> +				    &iommufd_mock_bus_type.bus,
> +				    &iommufd_mock_bus_type.nb);
> +	bus_unregister(&iommufd_mock_bus_type.bus);
> +	platform_device_del(selftest_iommu_dev);
>  	debugfs_remove_recursive(dbgfs_root);
> -	bus_unregister(&iommufd_mock_bus_type);
>  }

There is a mistake here that started to become visible after one of
the rebases, it needs to call iommu_device_sysfs_add() prior to
iommu_device_register_bus() otherwise the iommu core stuff does not
fully initialize and weird stuff starts happening.

So, it needs this:

diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c
index 5433c9c545526d..d2b59a1157441c 100644
--- a/drivers/iommu/iommufd/selftest.c
+++ b/drivers/iommu/iommufd/selftest.c
@@ -987,14 +987,21 @@ int __init iommufd_test_init(void)
 	if (rc)
 		goto err_platform;
 
-	mock_iommu_device.dev = &selftest_iommu_dev->dev;
+	rc = iommu_device_sysfs_add(&mock_iommu_device,
+				    &selftest_iommu_dev->dev, NULL, "%s",
+				    dev_name(&selftest_iommu_dev->dev));
+	if (rc)
+		goto err_bus;
+
 	rc = iommu_device_register_bus(&mock_iommu_device, &mock_ops,
 				  &iommufd_mock_bus_type.bus,
 				  &iommufd_mock_bus_type.nb);
 	if (rc)
-		goto err_bus;
+		goto err_sysfs;
 	return 0;
 
+err_sysfs:
+	iommu_device_sysfs_remove(&mock_iommu_device);
 err_bus:
 	bus_unregister(&iommufd_mock_bus_type.bus);
 err_platform:
@@ -1006,6 +1013,7 @@ int __init iommufd_test_init(void)
 
 void iommufd_test_exit(void)
 {
+	iommu_device_sysfs_remove(&mock_iommu_device);
 	iommu_device_unregister_bus(&mock_iommu_device,
 				    &iommufd_mock_bus_type.bus,
 				    &iommufd_mock_bus_type.nb);