[2/2] iommufd/selftest: Use normal IOMMU registration

Message ID 44ee6854da69e86b208f49752f60a4c18205c32a.1701165201.git.robin.murphy@arm.com
State New
Headers
Series iommufd/selftest: Fix and cleanup for bus ops |

Commit Message

Robin Murphy Nov. 28, 2023, 10:42 a.m. UTC
  The IOMMU core now supports coexistence of fwspec-based drivers, which
the mock driver now is, so let's bring the mock bus into iommu_buses,
drop the special interface, and use the normal registration flow. The
one concession we have to make is to ensure that the mock bus is
registered early enough so that bus_for_each_dev() doesn't error out
for other IOMMU drivers registering before iommufd_test_init() runs.

Signed-off-by: Robin Murphy <robin.murphy@arm.com>
---
 drivers/iommu/iommu-priv.h           |  7 -----
 drivers/iommu/iommu.c                | 46 +++-------------------------
 drivers/iommu/iommufd/iommufd_test.h |  2 ++
 drivers/iommu/iommufd/selftest.c     | 36 ++++++++--------------
 4 files changed, 18 insertions(+), 73 deletions(-)
  

Comments

Jason Gunthorpe Nov. 28, 2023, 2:35 p.m. UTC | #1
On Tue, Nov 28, 2023 at 10:42:12AM +0000, Robin Murphy wrote:
> The IOMMU core now supports coexistence of fwspec-based drivers, which
> the mock driver now is, so let's bring the mock bus into iommu_buses,
> drop the special interface, and use the normal registration flow. The
> one concession we have to make is to ensure that the mock bus is
> registered early enough so that bus_for_each_dev() doesn't error out
> for other IOMMU drivers registering before iommufd_test_init() runs.

This makes iommufd non-modular which becomes a total PITA for development :(

Jason
  
Robin Murphy Nov. 28, 2023, 2:53 p.m. UTC | #2
On 28/11/2023 2:35 pm, Jason Gunthorpe wrote:
> On Tue, Nov 28, 2023 at 10:42:12AM +0000, Robin Murphy wrote:
>> The IOMMU core now supports coexistence of fwspec-based drivers, which
>> the mock driver now is, so let's bring the mock bus into iommu_buses,
>> drop the special interface, and use the normal registration flow. The
>> one concession we have to make is to ensure that the mock bus is
>> registered early enough so that bus_for_each_dev() doesn't error out
>> for other IOMMU drivers registering before iommufd_test_init() runs.
> 
> This makes iommufd non-modular which becomes a total PITA for development :(

Oh fiddle, I misread the makefile, and indeed this doesn't work at all, 
sorry (turns out it fails to even build for IOMMUFD=m...) Guess I should 
have been more wary of how suspiciously easy it seemed :(

Dynamic bus registration in general would be a neat thing to explore at 
some point, since the static iommu_buses array isn't my most favourite 
part of the whole business, but I guess we leave this as-is for now.

Robin.
  
Jason Gunthorpe Nov. 28, 2023, 2:56 p.m. UTC | #3
On Tue, Nov 28, 2023 at 02:53:03PM +0000, Robin Murphy wrote:

> Dynamic bus registration in general would be a neat thing to explore at some
> point, since the static iommu_buses array isn't my most favourite part of
> the whole business, but I guess we leave this as-is for now.

I have an idea for that :) For later!

Jason
  
kernel test robot Dec. 6, 2023, 5:48 a.m. UTC | #4
Hi Robin,

kernel test robot noticed the following build errors:

[auto build test ERROR on next-20231128]
[cannot apply to joro-iommu/next v6.7-rc3 v6.7-rc2 v6.7-rc1 linus/master v6.7-rc4]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Robin-Murphy/iommufd-selftest-Use-a-fwnode-to-distinguish-devices/20231128-185058
base:   next-20231128
patch link:    https://lore.kernel.org/r/44ee6854da69e86b208f49752f60a4c18205c32a.1701165201.git.robin.murphy%40arm.com
patch subject: [PATCH 2/2] iommufd/selftest: Use normal IOMMU registration
config: parisc-allmodconfig (https://download.01.org/0day-ci/archive/20231206/202312061310.UufAZ5vQ-lkp@intel.com/config)
compiler: hppa-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231206/202312061310.UufAZ5vQ-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202312061310.UufAZ5vQ-lkp@intel.com/

All errors (new ones prefixed by >>):

   hppa-linux-ld: drivers/iommu/iommufd/selftest.o: in function `iommufd_bus_init':
>> (.init.text+0x10): multiple definition of `init_module'; drivers/iommu/iommufd/main.o:(.init.text+0x10): first defined here
  
kernel test robot Dec. 6, 2023, 10:42 a.m. UTC | #5
Hi Robin,

kernel test robot noticed the following build warnings:

[auto build test WARNING on next-20231128]
[cannot apply to joro-iommu/next v6.7-rc3 v6.7-rc2 v6.7-rc1 linus/master v6.7-rc4]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Robin-Murphy/iommufd-selftest-Use-a-fwnode-to-distinguish-devices/20231128-185058
base:   next-20231128
patch link:    https://lore.kernel.org/r/44ee6854da69e86b208f49752f60a4c18205c32a.1701165201.git.robin.murphy%40arm.com
patch subject: [PATCH 2/2] iommufd/selftest: Use normal IOMMU registration
config: x86_64-allyesconfig (https://download.01.org/0day-ci/archive/20231206/202312061821.knvqS2Hz-lkp@intel.com/config)
compiler: clang version 16.0.4 (https://github.com/llvm/llvm-project.git ae42196bc493ffe877a7e3dff8be32035dea4d07)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231206/202312061821.knvqS2Hz-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202312061821.knvqS2Hz-lkp@intel.com/

All warnings (new ones prefixed by >>):

>> drivers/iommu/iommufd/selftest.c:1323:12: warning: no previous prototype for function 'iommufd_bus_init' [-Wmissing-prototypes]
   int __init iommufd_bus_init(void)
              ^
   drivers/iommu/iommufd/selftest.c:1323:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   int __init iommufd_bus_init(void)
   ^
   static 
   drivers/iommu/iommufd/selftest.c:515:1: warning: unused function 'get_md_pagetable_nested' [-Wunused-function]
   get_md_pagetable_nested(struct iommufd_ucmd *ucmd, u32 mockpt_id,
   ^
   2 warnings generated.


vim +/iommufd_bus_init +1323 drivers/iommu/iommufd/selftest.c

  1322	
> 1323	int __init iommufd_bus_init(void)
  1324	{
  1325		return bus_register(&iommufd_mock_bus_type);
  1326	}
  1327	subsys_initcall(iommufd_bus_init);
  1328
  
kernel test robot Dec. 6, 2023, 11:35 a.m. UTC | #6
Hi Robin,

kernel test robot noticed the following build errors:

[auto build test ERROR on next-20231128]
[cannot apply to joro-iommu/next v6.7-rc3 v6.7-rc2 v6.7-rc1 linus/master v6.7-rc4]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Robin-Murphy/iommufd-selftest-Use-a-fwnode-to-distinguish-devices/20231128-185058
base:   next-20231128
patch link:    https://lore.kernel.org/r/44ee6854da69e86b208f49752f60a4c18205c32a.1701165201.git.robin.murphy%40arm.com
patch subject: [PATCH 2/2] iommufd/selftest: Use normal IOMMU registration
config: microblaze-allmodconfig (https://download.01.org/0day-ci/archive/20231206/202312061934.2WuVvBEi-lkp@intel.com/config)
compiler: microblaze-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231206/202312061934.2WuVvBEi-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202312061934.2WuVvBEi-lkp@intel.com/

All errors (new ones prefixed by >>):

   microblaze-linux-ld: drivers/iommu/iommufd/selftest.o: in function `iommufd_bus_init':
>> (.init.text+0x0): multiple definition of `init_module'; drivers/iommu/iommufd/main.o:main.o:(.init.text+0x0): first defined here
  

Patch

diff --git a/drivers/iommu/iommu-priv.h b/drivers/iommu/iommu-priv.h
index 2024a2313348..663bd4fa166f 100644
--- a/drivers/iommu/iommu-priv.h
+++ b/drivers/iommu/iommu-priv.h
@@ -20,11 +20,4 @@  static inline const struct iommu_ops *dev_iommu_ops(struct device *dev)
 int iommu_group_replace_domain(struct iommu_group *group,
 			       struct iommu_domain *new_domain);
 
-int iommu_device_register_bus(struct iommu_device *iommu,
-			      const struct iommu_ops *ops, struct bus_type *bus,
-			      struct notifier_block *nb);
-void iommu_device_unregister_bus(struct iommu_device *iommu,
-				 struct bus_type *bus,
-				 struct notifier_block *nb);
-
 #endif /* __LINUX_IOMMU_PRIV_H */
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 824989874dee..559999cfd9d4 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -35,8 +35,8 @@ 
 
 #include "dma-iommu.h"
 #include "iommu-priv.h"
-
 #include "iommu-sva.h"
+#include "iommufd/iommufd_test.h"
 
 static struct kset *iommu_group_kset;
 static DEFINE_IDA(iommu_group_ida);
@@ -165,6 +165,9 @@  static const struct bus_type * const iommu_buses[] = {
 #ifdef CONFIG_CDX_BUS
 	&cdx_bus_type,
 #endif
+#ifdef CONFIG_IOMMUFD_TEST
+	&iommufd_mock_bus_type,
+#endif
 };
 
 /*
@@ -289,47 +292,6 @@  void iommu_device_unregister(struct iommu_device *iommu)
 }
 EXPORT_SYMBOL_GPL(iommu_device_unregister);
 
-#if IS_ENABLED(CONFIG_IOMMUFD_TEST)
-void iommu_device_unregister_bus(struct iommu_device *iommu,
-				 struct bus_type *bus,
-				 struct notifier_block *nb)
-{
-	bus_unregister_notifier(bus, nb);
-	iommu_device_unregister(iommu);
-}
-EXPORT_SYMBOL_GPL(iommu_device_unregister_bus);
-
-/*
- * Register an iommu driver against a single bus. This is only used by iommufd
- * selftest to create a mock iommu driver. The caller must provide
- * some memory to hold a notifier_block.
- */
-int iommu_device_register_bus(struct iommu_device *iommu,
-			      const struct iommu_ops *ops, struct bus_type *bus,
-			      struct notifier_block *nb)
-{
-	int err;
-
-	iommu->ops = ops;
-	nb->notifier_call = iommu_bus_notifier;
-	err = bus_register_notifier(bus, nb);
-	if (err)
-		return err;
-
-	spin_lock(&iommu_device_lock);
-	list_add_tail(&iommu->list, &iommu_device_list);
-	spin_unlock(&iommu_device_lock);
-
-	err = bus_iommu_probe(bus);
-	if (err) {
-		iommu_device_unregister_bus(iommu, bus, nb);
-		return err;
-	}
-	return 0;
-}
-EXPORT_SYMBOL_GPL(iommu_device_register_bus);
-#endif
-
 static struct dev_iommu *dev_iommu_get(struct device *dev)
 {
 	struct dev_iommu *param = dev->iommu;
diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h
index 7910fbe1962d..a2d2c55a9315 100644
--- a/drivers/iommu/iommufd/iommufd_test.h
+++ b/drivers/iommu/iommufd/iommufd_test.h
@@ -148,4 +148,6 @@  struct iommu_hwpt_selftest {
 	__u32 iotlb;
 };
 
+extern struct bus_type iommufd_mock_bus_type;
+
 #endif
diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c
index f46ce0f8808d..bb09abb5bcbb 100644
--- a/drivers/iommu/iommufd/selftest.c
+++ b/drivers/iommu/iommufd/selftest.c
@@ -530,15 +530,8 @@  get_md_pagetable_nested(struct iommufd_ucmd *ucmd, u32 mockpt_id,
 	return hwpt;
 }
 
-struct mock_bus_type {
-	struct bus_type bus;
-	struct notifier_block nb;
-};
-
-static struct mock_bus_type iommufd_mock_bus_type = {
-	.bus = {
-		.name = "iommufd_mock",
-	},
+struct bus_type iommufd_mock_bus_type = {
+	.name = "iommufd_mock",
 };
 
 static atomic_t mock_dev_num;
@@ -566,7 +559,7 @@  static struct mock_dev *mock_dev_create(unsigned long dev_flags)
 	device_initialize(&mdev->dev);
 	mdev->flags = dev_flags;
 	mdev->dev.release = mock_dev_release;
-	mdev->dev.bus = &iommufd_mock_bus_type.bus;
+	mdev->dev.bus = &iommufd_mock_bus_type;
 
 	rc = dev_set_name(&mdev->dev, "iommufd_mock%u",
 			  atomic_inc_return(&mock_dev_num));
@@ -1327,6 +1320,12 @@  bool iommufd_should_fail(void)
 	return should_fail(&fail_iommufd, 1);
 }
 
+int __init iommufd_bus_init(void)
+{
+	return bus_register(&iommufd_mock_bus_type);
+}
+subsys_initcall(iommufd_bus_init);
+
 int __init iommufd_test_init(void)
 {
 	struct platform_device_info pdevinfo = {
@@ -1343,27 +1342,19 @@  int __init iommufd_test_init(void)
 		goto err_dbgfs;
 	}
 
-	rc = bus_register(&iommufd_mock_bus_type.bus);
-	if (rc)
-		goto err_platform;
-
 	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;
+		goto err_platform;
 
-	rc = iommu_device_register_bus(&mock_iommu_device, &mock_ops,
-				  &iommufd_mock_bus_type.bus,
-				  &iommufd_mock_bus_type.nb);
+	rc = iommu_device_register(&mock_iommu_device, &mock_ops, NULL);
 	if (rc)
 		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:
 	platform_device_unregister(selftest_iommu_dev);
 err_dbgfs:
@@ -1374,10 +1365,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);
-	bus_unregister(&iommufd_mock_bus_type.bus);
+	iommu_device_unregister(&mock_iommu_device);
 	platform_device_unregister(selftest_iommu_dev);
 	debugfs_remove_recursive(dbgfs_root);
 }