linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4 00/16] iommu: Permit modular builds of ARM SMMU[v3] drivers
@ 2019-12-19 12:03 Will Deacon
  2019-12-19 12:03 ` [PATCH v4 01/16] drivers/iommu: Export core IOMMU API symbols to permit modular drivers Will Deacon
                   ` (18 more replies)
  0 siblings, 19 replies; 26+ messages in thread
From: Will Deacon @ 2019-12-19 12:03 UTC (permalink / raw)
  To: linux-kernel, iommu
  Cc: kernel-team, Will Deacon, Jean-Philippe Brucker, Jordan Crouse,
	John Garry, Bjorn Helgaas, Saravana Kannan, Greg Kroah-Hartman,
	Isaac J. Manjarres, Robin Murphy, Lorenzo Pieralisi,
	Joerg Roedel, Ard Biesheuvel

Hi all,

This is version four of the patches I previously posted here:

  v1: https://lore.kernel.org/lkml/20191030145112.19738-1-will@kernel.org/
  v2: https://lore.kernel.org/lkml/20191108151608.20932-1-will@kernel.org
  v3: https://lore.kernel.org/lkml/20191121114918.2293-1-will@kernel.org

Changes since v3 include:

  * Based on v5.5-rc1
  * ACPI/IORT support (thanks to Ard)
  * Export pci_{enable,disable}_ats() (thanks to Greg)
  * Added review tags

I tested this on AMD Seattle by loading arm-smmu-mod.ko from the initrd.

Cheers,

Will

Cc: Jean-Philippe Brucker <jean-philippe@linaro.org>
Cc: Jordan Crouse <jcrouse@codeaurora.org>
Cc: John Garry <john.garry@huawei.com>
Cc: Bjorn Helgaas <bhelgaas@google.com>
Cc: Saravana Kannan <saravanak@google.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: "Isaac J. Manjarres" <isaacm@codeaurora.org>
Cc: Robin Murphy <robin.murphy@arm.com>
Cc: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Cc: Joerg Roedel <joro@8bytes.org>
Cc: Ard Biesheuvel <ardb@kernel.org>

--->8

Ard Biesheuvel (1):
  iommu/arm-smmu: Support SMMU module probing from the IORT

Greg Kroah-Hartman (1):
  PCI/ATS: Restore EXPORT_SYMBOL_GPL() for pci_{enable,disable}_ats()

Will Deacon (14):
  drivers/iommu: Export core IOMMU API symbols to permit modular drivers
  iommu/of: Request ACS from the PCI core when configuring IOMMU linkage
  PCI: Export pci_ats_disabled() as a GPL symbol to modules
  drivers/iommu: Take a ref to the IOMMU driver prior to ->add_device()
  iommu/of: Take a ref to the IOMMU driver during ->of_xlate()
  drivers/iommu: Allow IOMMU bus ops to be unregistered
  Revert "iommu/arm-smmu: Make arm-smmu-v3 explicitly non-modular"
  Revert "iommu/arm-smmu: Make arm-smmu explicitly non-modular"
  iommu/arm-smmu: Prevent forced unbinding of Arm SMMU drivers
  iommu/arm-smmu-v3: Unregister IOMMU and bus ops on device removal
  iommu/arm-smmu-v3: Allow building as a module
  iommu/arm-smmu: Unregister IOMMU and bus ops on device removal
  iommu/arm-smmu: Allow building as a module
  iommu/arm-smmu: Update my email address in MODULE_AUTHOR()

 drivers/acpi/arm64/iort.c   |   4 +-
 drivers/iommu/Kconfig       |  16 ++++-
 drivers/iommu/Makefile      |   3 +-
 drivers/iommu/arm-smmu-v3.c |  94 +++++++++++++++++---------
 drivers/iommu/arm-smmu.c    | 128 +++++++++++++++++++++++++-----------
 drivers/iommu/iommu-sysfs.c |   5 ++
 drivers/iommu/iommu.c       |  32 ++++++++-
 drivers/iommu/of_iommu.c    |  19 ++++--
 drivers/pci/ats.c           |   2 +
 drivers/pci/pci.c           |   1 +
 include/linux/iommu.h       |   4 +-
 11 files changed, 223 insertions(+), 85 deletions(-)

-- 
2.24.1.735.g03f4e72817-goog


^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH v4 01/16] drivers/iommu: Export core IOMMU API symbols to permit modular drivers
  2019-12-19 12:03 [PATCH v4 00/16] iommu: Permit modular builds of ARM SMMU[v3] drivers Will Deacon
@ 2019-12-19 12:03 ` Will Deacon
  2019-12-19 12:03 ` [PATCH v4 02/16] iommu/of: Request ACS from the PCI core when configuring IOMMU linkage Will Deacon
                   ` (17 subsequent siblings)
  18 siblings, 0 replies; 26+ messages in thread
From: Will Deacon @ 2019-12-19 12:03 UTC (permalink / raw)
  To: linux-kernel, iommu
  Cc: kernel-team, Will Deacon, Jean-Philippe Brucker, Jordan Crouse,
	John Garry, Bjorn Helgaas, Saravana Kannan, Greg Kroah-Hartman,
	Isaac J. Manjarres, Robin Murphy, Lorenzo Pieralisi,
	Joerg Roedel, Ard Biesheuvel

Building IOMMU drivers as modules requires that the core IOMMU API
symbols are exported as GPL symbols.

Signed-off-by: Will Deacon <will@kernel.org>
---
 drivers/iommu/iommu-sysfs.c | 5 +++++
 drivers/iommu/iommu.c       | 8 ++++++++
 2 files changed, 13 insertions(+)

diff --git a/drivers/iommu/iommu-sysfs.c b/drivers/iommu/iommu-sysfs.c
index e436ff813e7e..99869217fbec 100644
--- a/drivers/iommu/iommu-sysfs.c
+++ b/drivers/iommu/iommu-sysfs.c
@@ -87,6 +87,7 @@ int iommu_device_sysfs_add(struct iommu_device *iommu,
 	put_device(iommu->dev);
 	return ret;
 }
+EXPORT_SYMBOL_GPL(iommu_device_sysfs_add);
 
 void iommu_device_sysfs_remove(struct iommu_device *iommu)
 {
@@ -94,6 +95,8 @@ void iommu_device_sysfs_remove(struct iommu_device *iommu)
 	device_unregister(iommu->dev);
 	iommu->dev = NULL;
 }
+EXPORT_SYMBOL_GPL(iommu_device_sysfs_remove);
+
 /*
  * IOMMU drivers can indicate a device is managed by a given IOMMU using
  * this interface.  A link to the device will be created in the "devices"
@@ -119,6 +122,7 @@ int iommu_device_link(struct iommu_device *iommu, struct device *link)
 
 	return ret;
 }
+EXPORT_SYMBOL_GPL(iommu_device_link);
 
 void iommu_device_unlink(struct iommu_device *iommu, struct device *link)
 {
@@ -128,3 +132,4 @@ void iommu_device_unlink(struct iommu_device *iommu, struct device *link)
 	sysfs_remove_link(&link->kobj, "iommu");
 	sysfs_remove_link_from_group(&iommu->dev->kobj, "devices", dev_name(link));
 }
+EXPORT_SYMBOL_GPL(iommu_device_unlink);
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index db7bfd4f2d20..7c92197d53f3 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -141,6 +141,7 @@ int iommu_device_register(struct iommu_device *iommu)
 	spin_unlock(&iommu_device_lock);
 	return 0;
 }
+EXPORT_SYMBOL_GPL(iommu_device_register);
 
 void iommu_device_unregister(struct iommu_device *iommu)
 {
@@ -148,6 +149,7 @@ void iommu_device_unregister(struct iommu_device *iommu)
 	list_del(&iommu->list);
 	spin_unlock(&iommu_device_lock);
 }
+EXPORT_SYMBOL_GPL(iommu_device_unregister);
 
 static struct iommu_param *iommu_get_dev_param(struct device *dev)
 {
@@ -886,6 +888,7 @@ struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
 	kobject_get(group->devices_kobj);
 	return group;
 }
+EXPORT_SYMBOL_GPL(iommu_group_ref_get);
 
 /**
  * iommu_group_put - Decrement group reference
@@ -1259,6 +1262,7 @@ struct iommu_group *generic_device_group(struct device *dev)
 {
 	return iommu_group_alloc();
 }
+EXPORT_SYMBOL_GPL(generic_device_group);
 
 /*
  * Use standard PCI bus topology, isolation features, and DMA alias quirks
@@ -1326,6 +1330,7 @@ struct iommu_group *pci_device_group(struct device *dev)
 	/* No shared group found, allocate new */
 	return iommu_group_alloc();
 }
+EXPORT_SYMBOL_GPL(pci_device_group);
 
 /* Get the IOMMU group for device on fsl-mc bus */
 struct iommu_group *fsl_mc_device_group(struct device *dev)
@@ -1338,6 +1343,7 @@ struct iommu_group *fsl_mc_device_group(struct device *dev)
 		group = iommu_group_alloc();
 	return group;
 }
+EXPORT_SYMBOL_GPL(fsl_mc_device_group);
 
 /**
  * iommu_group_get_for_dev - Find or create the IOMMU group for a device
@@ -1406,6 +1412,7 @@ struct iommu_group *iommu_group_get_for_dev(struct device *dev)
 
 	return group;
 }
+EXPORT_SYMBOL(iommu_group_get_for_dev);
 
 struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
 {
@@ -2246,6 +2253,7 @@ struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
 	region->type = type;
 	return region;
 }
+EXPORT_SYMBOL_GPL(iommu_alloc_resv_region);
 
 static int
 request_default_domain_for_dev(struct device *dev, unsigned long type)
-- 
2.24.1.735.g03f4e72817-goog


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v4 02/16] iommu/of: Request ACS from the PCI core when configuring IOMMU linkage
  2019-12-19 12:03 [PATCH v4 00/16] iommu: Permit modular builds of ARM SMMU[v3] drivers Will Deacon
  2019-12-19 12:03 ` [PATCH v4 01/16] drivers/iommu: Export core IOMMU API symbols to permit modular drivers Will Deacon
@ 2019-12-19 12:03 ` Will Deacon
  2019-12-19 12:03 ` [PATCH v4 03/16] PCI/ATS: Restore EXPORT_SYMBOL_GPL() for pci_{enable,disable}_ats() Will Deacon
                   ` (16 subsequent siblings)
  18 siblings, 0 replies; 26+ messages in thread
From: Will Deacon @ 2019-12-19 12:03 UTC (permalink / raw)
  To: linux-kernel, iommu
  Cc: kernel-team, Will Deacon, Jean-Philippe Brucker, Jordan Crouse,
	John Garry, Bjorn Helgaas, Saravana Kannan, Greg Kroah-Hartman,
	Isaac J. Manjarres, Robin Murphy, Lorenzo Pieralisi,
	Joerg Roedel, Ard Biesheuvel

To avoid having to export 'pci_request_acs()' to modular IOMMU drivers,
move the call into the 'of_dma_configure()' path in a similar manner to
the way in which ACS is configured when probing via ACPI/IORT.

Signed-off-by: Will Deacon <will@kernel.org>
---
 drivers/iommu/of_iommu.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/iommu/of_iommu.c b/drivers/iommu/of_iommu.c
index 026ad2b29dcd..4d2f02132e7a 100644
--- a/drivers/iommu/of_iommu.c
+++ b/drivers/iommu/of_iommu.c
@@ -179,6 +179,7 @@ const struct iommu_ops *of_iommu_configure(struct device *dev,
 			.np = master_np,
 		};
 
+		pci_request_acs();
 		err = pci_for_each_dma_alias(to_pci_dev(dev),
 					     of_pci_iommu_init, &info);
 	} else if (dev_is_fsl_mc(dev)) {
-- 
2.24.1.735.g03f4e72817-goog


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v4 03/16] PCI/ATS: Restore EXPORT_SYMBOL_GPL() for pci_{enable,disable}_ats()
  2019-12-19 12:03 [PATCH v4 00/16] iommu: Permit modular builds of ARM SMMU[v3] drivers Will Deacon
  2019-12-19 12:03 ` [PATCH v4 01/16] drivers/iommu: Export core IOMMU API symbols to permit modular drivers Will Deacon
  2019-12-19 12:03 ` [PATCH v4 02/16] iommu/of: Request ACS from the PCI core when configuring IOMMU linkage Will Deacon
@ 2019-12-19 12:03 ` Will Deacon
  2019-12-20  8:43   ` Joerg Roedel
  2019-12-19 12:03 ` [PATCH v4 04/16] PCI: Export pci_ats_disabled() as a GPL symbol to modules Will Deacon
                   ` (15 subsequent siblings)
  18 siblings, 1 reply; 26+ messages in thread
From: Will Deacon @ 2019-12-19 12:03 UTC (permalink / raw)
  To: linux-kernel, iommu
  Cc: kernel-team, Will Deacon, Jean-Philippe Brucker, Jordan Crouse,
	John Garry, Bjorn Helgaas, Saravana Kannan, Greg Kroah-Hartman,
	Isaac J. Manjarres, Robin Murphy, Lorenzo Pieralisi,
	Joerg Roedel, Ard Biesheuvel, Greg Kroah-Hartman, Joerg Roedel

From: Greg Kroah-Hartman <gregkh@google.com>

Commit d355bb209783 ("PCI/ATS: Remove unnecessary EXPORT_SYMBOL_GPL()")
unexported a bunch of symbols from the PCI core since the only external
users were non-modular IOMMU drivers. Although most of those symbols
can remain private for now, 'pci_{enable,disable_ats()' is required for
the ARM SMMUv3 driver to build as a module, otherwise we get a build
failure as follows:

  | ERROR: "pci_enable_ats" [drivers/iommu/arm-smmu-v3.ko] undefined!
  | ERROR: "pci_disable_ats" [drivers/iommu/arm-smmu-v3.ko] undefined!

Re-export these two functions so that the ARM SMMUv3 driver can be build
as a module.

Cc: Bjorn Helgaas <bhelgaas@google.com>
Cc: Joerg Roedel <jroedel@suse.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@google.com>
[will: rewrote commit message]
Signed-off-by: Will Deacon <will@kernel.org>
---
 drivers/pci/ats.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/pci/ats.c b/drivers/pci/ats.c
index 982b46f0a54d..dcbcf1331bb2 100644
--- a/drivers/pci/ats.c
+++ b/drivers/pci/ats.c
@@ -69,6 +69,7 @@ int pci_enable_ats(struct pci_dev *dev, int ps)
 	dev->ats_enabled = 1;
 	return 0;
 }
+EXPORT_SYMBOL_GPL(pci_enable_ats);
 
 /**
  * pci_disable_ats - disable the ATS capability
@@ -87,6 +88,7 @@ void pci_disable_ats(struct pci_dev *dev)
 
 	dev->ats_enabled = 0;
 }
+EXPORT_SYMBOL_GPL(pci_disable_ats);
 
 void pci_restore_ats_state(struct pci_dev *dev)
 {
-- 
2.24.1.735.g03f4e72817-goog


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v4 04/16] PCI: Export pci_ats_disabled() as a GPL symbol to modules
  2019-12-19 12:03 [PATCH v4 00/16] iommu: Permit modular builds of ARM SMMU[v3] drivers Will Deacon
                   ` (2 preceding siblings ...)
  2019-12-19 12:03 ` [PATCH v4 03/16] PCI/ATS: Restore EXPORT_SYMBOL_GPL() for pci_{enable,disable}_ats() Will Deacon
@ 2019-12-19 12:03 ` Will Deacon
  2019-12-19 12:03 ` [PATCH v4 05/16] drivers/iommu: Take a ref to the IOMMU driver prior to ->add_device() Will Deacon
                   ` (14 subsequent siblings)
  18 siblings, 0 replies; 26+ messages in thread
From: Will Deacon @ 2019-12-19 12:03 UTC (permalink / raw)
  To: linux-kernel, iommu
  Cc: kernel-team, Will Deacon, Jean-Philippe Brucker, Jordan Crouse,
	John Garry, Bjorn Helgaas, Saravana Kannan, Greg Kroah-Hartman,
	Isaac J. Manjarres, Robin Murphy, Lorenzo Pieralisi,
	Joerg Roedel, Ard Biesheuvel

Building drivers for ATS-aware IOMMUs as modules requires access to
pci_ats_disabled(). Export it as a GPL symbol to get things working.

Acked-by: Bjorn Helgaas <bhelgaas@google.com>
Signed-off-by: Will Deacon <will@kernel.org>
---
 drivers/pci/pci.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index e87196cc1a7f..ad746d903ceb 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -131,6 +131,7 @@ bool pci_ats_disabled(void)
 {
 	return pcie_ats_disabled;
 }
+EXPORT_SYMBOL_GPL(pci_ats_disabled);
 
 /* Disable bridge_d3 for all PCIe ports */
 static bool pci_bridge_d3_disable;
-- 
2.24.1.735.g03f4e72817-goog


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v4 05/16] drivers/iommu: Take a ref to the IOMMU driver prior to ->add_device()
  2019-12-19 12:03 [PATCH v4 00/16] iommu: Permit modular builds of ARM SMMU[v3] drivers Will Deacon
                   ` (3 preceding siblings ...)
  2019-12-19 12:03 ` [PATCH v4 04/16] PCI: Export pci_ats_disabled() as a GPL symbol to modules Will Deacon
@ 2019-12-19 12:03 ` Will Deacon
  2019-12-19 14:44   ` Greg Kroah-Hartman
  2019-12-19 12:03 ` [PATCH v4 06/16] iommu/of: Take a ref to the IOMMU driver during ->of_xlate() Will Deacon
                   ` (13 subsequent siblings)
  18 siblings, 1 reply; 26+ messages in thread
From: Will Deacon @ 2019-12-19 12:03 UTC (permalink / raw)
  To: linux-kernel, iommu
  Cc: kernel-team, Will Deacon, Jean-Philippe Brucker, Jordan Crouse,
	John Garry, Bjorn Helgaas, Saravana Kannan, Greg Kroah-Hartman,
	Isaac J. Manjarres, Robin Murphy, Lorenzo Pieralisi,
	Joerg Roedel, Ard Biesheuvel

To avoid accidental removal of an active IOMMU driver module, take a
reference to the driver module in 'iommu_probe_device()' immediately
prior to invoking the '->add_device()' callback and hold it until the
after the device has been removed by '->remove_device()'.

Suggested-by: Joerg Roedel <joro@8bytes.org>
Signed-off-by: Will Deacon <will@kernel.org>
---
 drivers/iommu/iommu.c | 19 +++++++++++++++++--
 include/linux/iommu.h |  4 +++-
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 7c92197d53f3..bc8edf90e729 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -22,6 +22,7 @@
 #include <linux/bitops.h>
 #include <linux/property.h>
 #include <linux/fsl/mc.h>
+#include <linux/module.h>
 #include <trace/events/iommu.h>
 
 static struct kset *iommu_group_kset;
@@ -185,10 +186,21 @@ int iommu_probe_device(struct device *dev)
 	if (!iommu_get_dev_param(dev))
 		return -ENOMEM;
 
+	if (!try_module_get(ops->owner)) {
+		ret = -EINVAL;
+		goto err_free_dev_param;
+	}
+
 	ret = ops->add_device(dev);
 	if (ret)
-		iommu_free_dev_param(dev);
+		goto err_module_put;
+
+	return 0;
 
+err_module_put:
+	module_put(ops->owner);
+err_free_dev_param:
+	iommu_free_dev_param(dev);
 	return ret;
 }
 
@@ -199,7 +211,10 @@ void iommu_release_device(struct device *dev)
 	if (dev->iommu_group)
 		ops->remove_device(dev);
 
-	iommu_free_dev_param(dev);
+	if (dev->iommu_param) {
+		module_put(ops->owner);
+		iommu_free_dev_param(dev);
+	}
 }
 
 static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
diff --git a/include/linux/iommu.h b/include/linux/iommu.h
index f2223cbb5fd5..e9f94d3f7a04 100644
--- a/include/linux/iommu.h
+++ b/include/linux/iommu.h
@@ -246,9 +246,10 @@ struct iommu_iotlb_gather {
  * @sva_get_pasid: Get PASID associated to a SVA handle
  * @page_response: handle page request response
  * @cache_invalidate: invalidate translation caches
- * @pgsize_bitmap: bitmap of all possible supported page sizes
  * @sva_bind_gpasid: bind guest pasid and mm
  * @sva_unbind_gpasid: unbind guest pasid and mm
+ * @pgsize_bitmap: bitmap of all possible supported page sizes
+ * @owner: Driver module providing these ops
  */
 struct iommu_ops {
 	bool (*capable)(enum iommu_cap);
@@ -318,6 +319,7 @@ struct iommu_ops {
 	int (*sva_unbind_gpasid)(struct device *dev, int pasid);
 
 	unsigned long pgsize_bitmap;
+	struct module *owner;
 };
 
 /**
-- 
2.24.1.735.g03f4e72817-goog


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v4 06/16] iommu/of: Take a ref to the IOMMU driver during ->of_xlate()
  2019-12-19 12:03 [PATCH v4 00/16] iommu: Permit modular builds of ARM SMMU[v3] drivers Will Deacon
                   ` (4 preceding siblings ...)
  2019-12-19 12:03 ` [PATCH v4 05/16] drivers/iommu: Take a ref to the IOMMU driver prior to ->add_device() Will Deacon
@ 2019-12-19 12:03 ` Will Deacon
  2019-12-19 12:03 ` [PATCH v4 07/16] drivers/iommu: Allow IOMMU bus ops to be unregistered Will Deacon
                   ` (12 subsequent siblings)
  18 siblings, 0 replies; 26+ messages in thread
From: Will Deacon @ 2019-12-19 12:03 UTC (permalink / raw)
  To: linux-kernel, iommu
  Cc: kernel-team, Will Deacon, Jean-Philippe Brucker, Jordan Crouse,
	John Garry, Bjorn Helgaas, Saravana Kannan, Greg Kroah-Hartman,
	Isaac J. Manjarres, Robin Murphy, Lorenzo Pieralisi,
	Joerg Roedel, Ard Biesheuvel

Ensure that we hold a reference to the IOMMU driver module while calling
the '->of_xlate()' callback during early device probing.

Signed-off-by: Will Deacon <will@kernel.org>
---
 drivers/iommu/of_iommu.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/drivers/iommu/of_iommu.c b/drivers/iommu/of_iommu.c
index 4d2f02132e7a..e7bc8b721947 100644
--- a/drivers/iommu/of_iommu.c
+++ b/drivers/iommu/of_iommu.c
@@ -8,11 +8,12 @@
 #include <linux/export.h>
 #include <linux/iommu.h>
 #include <linux/limits.h>
-#include <linux/pci.h>
+#include <linux/module.h>
 #include <linux/msi.h>
 #include <linux/of.h>
 #include <linux/of_iommu.h>
 #include <linux/of_pci.h>
+#include <linux/pci.h>
 #include <linux/slab.h>
 #include <linux/fsl/mc.h>
 
@@ -91,16 +92,16 @@ static int of_iommu_xlate(struct device *dev,
 {
 	const struct iommu_ops *ops;
 	struct fwnode_handle *fwnode = &iommu_spec->np->fwnode;
-	int err;
+	int ret;
 
 	ops = iommu_ops_from_fwnode(fwnode);
 	if ((ops && !ops->of_xlate) ||
 	    !of_device_is_available(iommu_spec->np))
 		return NO_IOMMU;
 
-	err = iommu_fwspec_init(dev, &iommu_spec->np->fwnode, ops);
-	if (err)
-		return err;
+	ret = iommu_fwspec_init(dev, &iommu_spec->np->fwnode, ops);
+	if (ret)
+		return ret;
 	/*
 	 * The otherwise-empty fwspec handily serves to indicate the specific
 	 * IOMMU device we're waiting for, which will be useful if we ever get
@@ -109,7 +110,12 @@ static int of_iommu_xlate(struct device *dev,
 	if (!ops)
 		return driver_deferred_probe_check_state(dev);
 
-	return ops->of_xlate(dev, iommu_spec);
+	if (!try_module_get(ops->owner))
+		return -ENODEV;
+
+	ret = ops->of_xlate(dev, iommu_spec);
+	module_put(ops->owner);
+	return ret;
 }
 
 struct of_pci_iommu_alias_info {
-- 
2.24.1.735.g03f4e72817-goog


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v4 07/16] drivers/iommu: Allow IOMMU bus ops to be unregistered
  2019-12-19 12:03 [PATCH v4 00/16] iommu: Permit modular builds of ARM SMMU[v3] drivers Will Deacon
                   ` (5 preceding siblings ...)
  2019-12-19 12:03 ` [PATCH v4 06/16] iommu/of: Take a ref to the IOMMU driver during ->of_xlate() Will Deacon
@ 2019-12-19 12:03 ` Will Deacon
  2019-12-19 12:03 ` [PATCH v4 08/16] Revert "iommu/arm-smmu: Make arm-smmu-v3 explicitly non-modular" Will Deacon
                   ` (11 subsequent siblings)
  18 siblings, 0 replies; 26+ messages in thread
From: Will Deacon @ 2019-12-19 12:03 UTC (permalink / raw)
  To: linux-kernel, iommu
  Cc: kernel-team, Will Deacon, Jean-Philippe Brucker, Jordan Crouse,
	John Garry, Bjorn Helgaas, Saravana Kannan, Greg Kroah-Hartman,
	Isaac J. Manjarres, Robin Murphy, Lorenzo Pieralisi,
	Joerg Roedel, Ard Biesheuvel

'bus_set_iommu()' allows IOMMU drivers to register their ops for a given
bus type. Unfortunately, it then doesn't allow them to be removed, which
is necessary for modular drivers to shutdown cleanly so that they can be
reloaded later on.

Allow 'bus_set_iommu()' to take a NULL 'ops' argument, which clear the
ops pointer for the selected bus_type.

Signed-off-by: Will Deacon <will@kernel.org>
---
 drivers/iommu/iommu.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index bc8edf90e729..433101f0853c 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -1558,6 +1558,11 @@ int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
 {
 	int err;
 
+	if (ops == NULL) {
+		bus->iommu_ops = NULL;
+		return 0;
+	}
+
 	if (bus->iommu_ops != NULL)
 		return -EBUSY;
 
-- 
2.24.1.735.g03f4e72817-goog


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v4 08/16] Revert "iommu/arm-smmu: Make arm-smmu-v3 explicitly non-modular"
  2019-12-19 12:03 [PATCH v4 00/16] iommu: Permit modular builds of ARM SMMU[v3] drivers Will Deacon
                   ` (6 preceding siblings ...)
  2019-12-19 12:03 ` [PATCH v4 07/16] drivers/iommu: Allow IOMMU bus ops to be unregistered Will Deacon
@ 2019-12-19 12:03 ` Will Deacon
  2019-12-19 12:03 ` [PATCH v4 09/16] Revert "iommu/arm-smmu: Make arm-smmu " Will Deacon
                   ` (10 subsequent siblings)
  18 siblings, 0 replies; 26+ messages in thread
From: Will Deacon @ 2019-12-19 12:03 UTC (permalink / raw)
  To: linux-kernel, iommu
  Cc: kernel-team, Will Deacon, Jean-Philippe Brucker, Jordan Crouse,
	John Garry, Bjorn Helgaas, Saravana Kannan, Greg Kroah-Hartman,
	Isaac J. Manjarres, Robin Murphy, Lorenzo Pieralisi,
	Joerg Roedel, Ard Biesheuvel

This reverts commit c07b6426df922d21a13a959cf785d46e9c531941.

Let's get the SMMUv3 driver building as a module, which means putting
back some dead code that we used to carry.

Signed-off-by: Will Deacon <will@kernel.org>
---
 drivers/iommu/arm-smmu-v3.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/drivers/iommu/arm-smmu-v3.c b/drivers/iommu/arm-smmu-v3.c
index effe72eb89e7..68163b1d680d 100644
--- a/drivers/iommu/arm-smmu-v3.c
+++ b/drivers/iommu/arm-smmu-v3.c
@@ -21,8 +21,7 @@
 #include <linux/io-pgtable.h>
 #include <linux/iommu.h>
 #include <linux/iopoll.h>
-#include <linux/init.h>
-#include <linux/moduleparam.h>
+#include <linux/module.h>
 #include <linux/msi.h>
 #include <linux/of.h>
 #include <linux/of_address.h>
@@ -384,10 +383,6 @@
 #define MSI_IOVA_BASE			0x8000000
 #define MSI_IOVA_LENGTH			0x100000
 
-/*
- * not really modular, but the easiest way to keep compat with existing
- * bootargs behaviour is to continue using module_param_named here.
- */
 static bool disable_bypass = 1;
 module_param_named(disable_bypass, disable_bypass, bool, S_IRUGO);
 MODULE_PARM_DESC(disable_bypass,
@@ -3683,25 +3678,37 @@ static int arm_smmu_device_probe(struct platform_device *pdev)
 	return 0;
 }
 
-static void arm_smmu_device_shutdown(struct platform_device *pdev)
+static int arm_smmu_device_remove(struct platform_device *pdev)
 {
 	struct arm_smmu_device *smmu = platform_get_drvdata(pdev);
 
 	arm_smmu_device_disable(smmu);
+
+	return 0;
+}
+
+static void arm_smmu_device_shutdown(struct platform_device *pdev)
+{
+	arm_smmu_device_remove(pdev);
 }
 
 static const struct of_device_id arm_smmu_of_match[] = {
 	{ .compatible = "arm,smmu-v3", },
 	{ },
 };
+MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
 
 static struct platform_driver arm_smmu_driver = {
 	.driver	= {
 		.name		= "arm-smmu-v3",
 		.of_match_table	= of_match_ptr(arm_smmu_of_match),
-		.suppress_bind_attrs = true,
 	},
 	.probe	= arm_smmu_device_probe,
+	.remove	= arm_smmu_device_remove,
 	.shutdown = arm_smmu_device_shutdown,
 };
-builtin_platform_driver(arm_smmu_driver);
+module_platform_driver(arm_smmu_driver);
+
+MODULE_DESCRIPTION("IOMMU API for ARM architected SMMUv3 implementations");
+MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
+MODULE_LICENSE("GPL v2");
-- 
2.24.1.735.g03f4e72817-goog


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v4 09/16] Revert "iommu/arm-smmu: Make arm-smmu explicitly non-modular"
  2019-12-19 12:03 [PATCH v4 00/16] iommu: Permit modular builds of ARM SMMU[v3] drivers Will Deacon
                   ` (7 preceding siblings ...)
  2019-12-19 12:03 ` [PATCH v4 08/16] Revert "iommu/arm-smmu: Make arm-smmu-v3 explicitly non-modular" Will Deacon
@ 2019-12-19 12:03 ` Will Deacon
  2019-12-19 12:03 ` [PATCH v4 10/16] iommu/arm-smmu: Prevent forced unbinding of Arm SMMU drivers Will Deacon
                   ` (9 subsequent siblings)
  18 siblings, 0 replies; 26+ messages in thread
From: Will Deacon @ 2019-12-19 12:03 UTC (permalink / raw)
  To: linux-kernel, iommu
  Cc: kernel-team, Will Deacon, Jean-Philippe Brucker, Jordan Crouse,
	John Garry, Bjorn Helgaas, Saravana Kannan, Greg Kroah-Hartman,
	Isaac J. Manjarres, Robin Murphy, Lorenzo Pieralisi,
	Joerg Roedel, Ard Biesheuvel

This reverts commit addb672f200f4e99368270da205320b83efe01a0.

Let's get the SMMU driver building as a module, which means putting
back some dead code that we used to carry.

Signed-off-by: Will Deacon <will@kernel.org>
---
 drivers/iommu/arm-smmu.c | 32 +++++++++++++++++++-------------
 1 file changed, 19 insertions(+), 13 deletions(-)

diff --git a/drivers/iommu/arm-smmu.c b/drivers/iommu/arm-smmu.c
index 4f1a350d9529..7c768f771001 100644
--- a/drivers/iommu/arm-smmu.c
+++ b/drivers/iommu/arm-smmu.c
@@ -27,8 +27,7 @@
 #include <linux/interrupt.h>
 #include <linux/io.h>
 #include <linux/iopoll.h>
-#include <linux/init.h>
-#include <linux/moduleparam.h>
+#include <linux/module.h>
 #include <linux/of.h>
 #include <linux/of_address.h>
 #include <linux/of_device.h>
@@ -60,10 +59,6 @@
 #define MSI_IOVA_LENGTH			0x100000
 
 static int force_stage;
-/*
- * not really modular, but the easiest way to keep compat with existing
- * bootargs behaviour is to continue using module_param() here.
- */
 module_param(force_stage, int, S_IRUGO);
 MODULE_PARM_DESC(force_stage,
 	"Force SMMU mappings to be installed at a particular stage of translation. A value of '1' or '2' forces the corresponding stage. All other values are ignored (i.e. no stage is forced). Note that selecting a specific stage will disable support for nested translation.");
@@ -1911,6 +1906,7 @@ static const struct of_device_id arm_smmu_of_match[] = {
 	{ .compatible = "qcom,smmu-v2", .data = &qcom_smmuv2 },
 	{ },
 };
+MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
 
 #ifdef CONFIG_ACPI
 static int acpi_smmu_get_data(u32 model, struct arm_smmu_device *smmu)
@@ -2196,12 +2192,12 @@ static int arm_smmu_legacy_bus_init(void)
 }
 device_initcall_sync(arm_smmu_legacy_bus_init);
 
-static void arm_smmu_device_shutdown(struct platform_device *pdev)
+static int arm_smmu_device_remove(struct platform_device *pdev)
 {
 	struct arm_smmu_device *smmu = platform_get_drvdata(pdev);
 
 	if (!smmu)
-		return;
+		return -ENODEV;
 
 	if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
 		dev_err(&pdev->dev, "removing device with active domains!\n");
@@ -2217,6 +2213,12 @@ static void arm_smmu_device_shutdown(struct platform_device *pdev)
 		clk_bulk_disable(smmu->num_clks, smmu->clks);
 
 	clk_bulk_unprepare(smmu->num_clks, smmu->clks);
+	return 0;
+}
+
+static void arm_smmu_device_shutdown(struct platform_device *pdev)
+{
+	arm_smmu_device_remove(pdev);
 }
 
 static int __maybe_unused arm_smmu_runtime_resume(struct device *dev)
@@ -2266,12 +2268,16 @@ static const struct dev_pm_ops arm_smmu_pm_ops = {
 
 static struct platform_driver arm_smmu_driver = {
 	.driver	= {
-		.name			= "arm-smmu",
-		.of_match_table		= of_match_ptr(arm_smmu_of_match),
-		.pm			= &arm_smmu_pm_ops,
-		.suppress_bind_attrs	= true,
+		.name		= "arm-smmu",
+		.of_match_table	= of_match_ptr(arm_smmu_of_match),
+		.pm		= &arm_smmu_pm_ops,
 	},
 	.probe	= arm_smmu_device_probe,
+	.remove	= arm_smmu_device_remove,
 	.shutdown = arm_smmu_device_shutdown,
 };
-builtin_platform_driver(arm_smmu_driver);
+module_platform_driver(arm_smmu_driver);
+
+MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
+MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
+MODULE_LICENSE("GPL v2");
-- 
2.24.1.735.g03f4e72817-goog


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v4 10/16] iommu/arm-smmu: Prevent forced unbinding of Arm SMMU drivers
  2019-12-19 12:03 [PATCH v4 00/16] iommu: Permit modular builds of ARM SMMU[v3] drivers Will Deacon
                   ` (8 preceding siblings ...)
  2019-12-19 12:03 ` [PATCH v4 09/16] Revert "iommu/arm-smmu: Make arm-smmu " Will Deacon
@ 2019-12-19 12:03 ` Will Deacon
  2019-12-19 12:03 ` [PATCH v4 11/16] iommu/arm-smmu-v3: Unregister IOMMU and bus ops on device removal Will Deacon
                   ` (8 subsequent siblings)
  18 siblings, 0 replies; 26+ messages in thread
From: Will Deacon @ 2019-12-19 12:03 UTC (permalink / raw)
  To: linux-kernel, iommu
  Cc: kernel-team, Will Deacon, Jean-Philippe Brucker, Jordan Crouse,
	John Garry, Bjorn Helgaas, Saravana Kannan, Greg Kroah-Hartman,
	Isaac J. Manjarres, Robin Murphy, Lorenzo Pieralisi,
	Joerg Roedel, Ard Biesheuvel

Forcefully unbinding the Arm SMMU drivers is a pretty dangerous operation,
since it will likely lead to catastrophic failure for any DMA devices
mastering through the SMMU being unbound. When the driver then attempts
to "handle" the fatal faults, it's very easy to trip over dead data
structures, leading to use-after-free.

On John's machine, he reports that the machine was "unusable" due to
loss of the storage controller following a forced unbind of the SMMUv3
driver:

  | # cd ./bus/platform/drivers/arm-smmu-v3
  | # echo arm-smmu-v3.0.auto > unbind
  | hisi_sas_v2_hw HISI0162:01: CQE_AXI_W_ERR (0x800) found!
  | platform arm-smmu-v3.0.auto: CMD_SYNC timeout at 0x00000146
  | [hwprod 0x00000146, hwcons 0x00000000]

Prevent this forced unbinding of the drivers by setting "suppress_bind_attrs"
to true.

Link: https://lore.kernel.org/lkml/06dfd385-1af0-3106-4cc5-6a5b8e864759@huawei.com
Reported-by: John Garry <john.garry@huawei.com>
Signed-off-by: Will Deacon <will@kernel.org>
---
 drivers/iommu/arm-smmu-v3.c | 5 +++--
 drivers/iommu/arm-smmu.c    | 7 ++++---
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/drivers/iommu/arm-smmu-v3.c b/drivers/iommu/arm-smmu-v3.c
index 68163b1d680d..9d4c9de6172d 100644
--- a/drivers/iommu/arm-smmu-v3.c
+++ b/drivers/iommu/arm-smmu-v3.c
@@ -3700,8 +3700,9 @@ MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
 
 static struct platform_driver arm_smmu_driver = {
 	.driver	= {
-		.name		= "arm-smmu-v3",
-		.of_match_table	= of_match_ptr(arm_smmu_of_match),
+		.name			= "arm-smmu-v3",
+		.of_match_table		= of_match_ptr(arm_smmu_of_match),
+		.suppress_bind_attrs	= true,
 	},
 	.probe	= arm_smmu_device_probe,
 	.remove	= arm_smmu_device_remove,
diff --git a/drivers/iommu/arm-smmu.c b/drivers/iommu/arm-smmu.c
index 7c768f771001..5cbee88a3b83 100644
--- a/drivers/iommu/arm-smmu.c
+++ b/drivers/iommu/arm-smmu.c
@@ -2268,9 +2268,10 @@ static const struct dev_pm_ops arm_smmu_pm_ops = {
 
 static struct platform_driver arm_smmu_driver = {
 	.driver	= {
-		.name		= "arm-smmu",
-		.of_match_table	= of_match_ptr(arm_smmu_of_match),
-		.pm		= &arm_smmu_pm_ops,
+		.name			= "arm-smmu",
+		.of_match_table		= of_match_ptr(arm_smmu_of_match),
+		.pm			= &arm_smmu_pm_ops,
+		.suppress_bind_attrs    = true,
 	},
 	.probe	= arm_smmu_device_probe,
 	.remove	= arm_smmu_device_remove,
-- 
2.24.1.735.g03f4e72817-goog


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v4 11/16] iommu/arm-smmu-v3: Unregister IOMMU and bus ops on device removal
  2019-12-19 12:03 [PATCH v4 00/16] iommu: Permit modular builds of ARM SMMU[v3] drivers Will Deacon
                   ` (9 preceding siblings ...)
  2019-12-19 12:03 ` [PATCH v4 10/16] iommu/arm-smmu: Prevent forced unbinding of Arm SMMU drivers Will Deacon
@ 2019-12-19 12:03 ` Will Deacon
  2019-12-19 12:03 ` [PATCH v4 12/16] iommu/arm-smmu: Support SMMU module probing from the IORT Will Deacon
                   ` (7 subsequent siblings)
  18 siblings, 0 replies; 26+ messages in thread
From: Will Deacon @ 2019-12-19 12:03 UTC (permalink / raw)
  To: linux-kernel, iommu
  Cc: kernel-team, Will Deacon, Jean-Philippe Brucker, Jordan Crouse,
	John Garry, Bjorn Helgaas, Saravana Kannan, Greg Kroah-Hartman,
	Isaac J. Manjarres, Robin Murphy, Lorenzo Pieralisi,
	Joerg Roedel, Ard Biesheuvel

When removing the SMMUv3 driver, we need to clear any state that we
registered during probe. This includes our bus ops, sysfs entries and
the IOMMU device registered for early firmware probing of masters.

Signed-off-by: Will Deacon <will@kernel.org>
---
 drivers/iommu/arm-smmu-v3.c | 64 +++++++++++++++++++++++++------------
 1 file changed, 43 insertions(+), 21 deletions(-)

diff --git a/drivers/iommu/arm-smmu-v3.c b/drivers/iommu/arm-smmu-v3.c
index 9d4c9de6172d..46160a2fec3e 100644
--- a/drivers/iommu/arm-smmu-v3.c
+++ b/drivers/iommu/arm-smmu-v3.c
@@ -3565,6 +3565,45 @@ static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu)
 		return SZ_128K;
 }
 
+static int arm_smmu_set_bus_ops(struct iommu_ops *ops)
+{
+	int err;
+
+#ifdef CONFIG_PCI
+	if (pci_bus_type.iommu_ops != ops) {
+		if (ops)
+			pci_request_acs();
+		err = bus_set_iommu(&pci_bus_type, ops);
+		if (err)
+			return err;
+	}
+#endif
+#ifdef CONFIG_ARM_AMBA
+	if (amba_bustype.iommu_ops != ops) {
+		err = bus_set_iommu(&amba_bustype, ops);
+		if (err)
+			goto err_reset_pci_ops;
+	}
+#endif
+	if (platform_bus_type.iommu_ops != ops) {
+		err = bus_set_iommu(&platform_bus_type, ops);
+		if (err)
+			goto err_reset_amba_ops;
+	}
+
+	return 0;
+
+err_reset_amba_ops:
+#ifdef CONFIG_ARM_AMBA
+	bus_set_iommu(&amba_bustype, NULL);
+#endif
+err_reset_pci_ops: __maybe_unused;
+#ifdef CONFIG_PCI
+	bus_set_iommu(&pci_bus_type, NULL);
+#endif
+	return err;
+}
+
 static int arm_smmu_device_probe(struct platform_device *pdev)
 {
 	int irq, ret;
@@ -3655,33 +3694,16 @@ static int arm_smmu_device_probe(struct platform_device *pdev)
 		return ret;
 	}
 
-#ifdef CONFIG_PCI
-	if (pci_bus_type.iommu_ops != &arm_smmu_ops) {
-		pci_request_acs();
-		ret = bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
-		if (ret)
-			return ret;
-	}
-#endif
-#ifdef CONFIG_ARM_AMBA
-	if (amba_bustype.iommu_ops != &arm_smmu_ops) {
-		ret = bus_set_iommu(&amba_bustype, &arm_smmu_ops);
-		if (ret)
-			return ret;
-	}
-#endif
-	if (platform_bus_type.iommu_ops != &arm_smmu_ops) {
-		ret = bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
-		if (ret)
-			return ret;
-	}
-	return 0;
+	return arm_smmu_set_bus_ops(&arm_smmu_ops);
 }
 
 static int arm_smmu_device_remove(struct platform_device *pdev)
 {
 	struct arm_smmu_device *smmu = platform_get_drvdata(pdev);
 
+	arm_smmu_set_bus_ops(NULL);
+	iommu_device_unregister(&smmu->iommu);
+	iommu_device_sysfs_remove(&smmu->iommu);
 	arm_smmu_device_disable(smmu);
 
 	return 0;
-- 
2.24.1.735.g03f4e72817-goog


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v4 12/16] iommu/arm-smmu: Support SMMU module probing from the IORT
  2019-12-19 12:03 [PATCH v4 00/16] iommu: Permit modular builds of ARM SMMU[v3] drivers Will Deacon
                   ` (10 preceding siblings ...)
  2019-12-19 12:03 ` [PATCH v4 11/16] iommu/arm-smmu-v3: Unregister IOMMU and bus ops on device removal Will Deacon
@ 2019-12-19 12:03 ` Will Deacon
  2019-12-19 12:03 ` [PATCH v4 13/16] iommu/arm-smmu-v3: Allow building as a module Will Deacon
                   ` (6 subsequent siblings)
  18 siblings, 0 replies; 26+ messages in thread
From: Will Deacon @ 2019-12-19 12:03 UTC (permalink / raw)
  To: linux-kernel, iommu
  Cc: kernel-team, Will Deacon, Jean-Philippe Brucker, Jordan Crouse,
	John Garry, Bjorn Helgaas, Saravana Kannan, Greg Kroah-Hartman,
	Isaac J. Manjarres, Robin Murphy, Lorenzo Pieralisi,
	Joerg Roedel, Ard Biesheuvel

From: Ard Biesheuvel <ardb@kernel.org>

Add support for SMMU drivers built as modules to the ACPI/IORT device
probing path, by deferring the probe of the master if the SMMU driver is
known to exist but has not been loaded yet. Given that the IORT code
registers a platform device for each SMMU that it discovers, we can
easily trigger the udev based autoloading of the SMMU drivers by making
the platform device identifier part of the module alias.

Reviewed-by: Robin Murphy <robin.murphy@arm.com>
Acked-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Tested-by: John Garry <john.garry@huawei.com> # only manual smmu ko loading
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Will Deacon <will@kernel.org>
---
 drivers/acpi/arm64/iort.c   | 4 ++--
 drivers/iommu/arm-smmu-v3.c | 1 +
 drivers/iommu/arm-smmu.c    | 1 +
 3 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/acpi/arm64/iort.c b/drivers/acpi/arm64/iort.c
index 33f71983e001..4a560fdf7386 100644
--- a/drivers/acpi/arm64/iort.c
+++ b/drivers/acpi/arm64/iort.c
@@ -850,9 +850,9 @@ static inline bool iort_iommu_driver_enabled(u8 type)
 {
 	switch (type) {
 	case ACPI_IORT_NODE_SMMU_V3:
-		return IS_BUILTIN(CONFIG_ARM_SMMU_V3);
+		return IS_ENABLED(CONFIG_ARM_SMMU_V3);
 	case ACPI_IORT_NODE_SMMU:
-		return IS_BUILTIN(CONFIG_ARM_SMMU);
+		return IS_ENABLED(CONFIG_ARM_SMMU);
 	default:
 		pr_warn("IORT node type %u does not describe an SMMU\n", type);
 		return false;
diff --git a/drivers/iommu/arm-smmu-v3.c b/drivers/iommu/arm-smmu-v3.c
index 46160a2fec3e..da9474a02668 100644
--- a/drivers/iommu/arm-smmu-v3.c
+++ b/drivers/iommu/arm-smmu-v3.c
@@ -3734,4 +3734,5 @@ module_platform_driver(arm_smmu_driver);
 
 MODULE_DESCRIPTION("IOMMU API for ARM architected SMMUv3 implementations");
 MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
+MODULE_ALIAS("platform:arm-smmu-v3");
 MODULE_LICENSE("GPL v2");
diff --git a/drivers/iommu/arm-smmu.c b/drivers/iommu/arm-smmu.c
index 5cbee88a3b83..5d2f60bb9e50 100644
--- a/drivers/iommu/arm-smmu.c
+++ b/drivers/iommu/arm-smmu.c
@@ -2281,4 +2281,5 @@ module_platform_driver(arm_smmu_driver);
 
 MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
 MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
+MODULE_ALIAS("platform:arm-smmu");
 MODULE_LICENSE("GPL v2");
-- 
2.24.1.735.g03f4e72817-goog


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v4 13/16] iommu/arm-smmu-v3: Allow building as a module
  2019-12-19 12:03 [PATCH v4 00/16] iommu: Permit modular builds of ARM SMMU[v3] drivers Will Deacon
                   ` (11 preceding siblings ...)
  2019-12-19 12:03 ` [PATCH v4 12/16] iommu/arm-smmu: Support SMMU module probing from the IORT Will Deacon
@ 2019-12-19 12:03 ` Will Deacon
  2019-12-19 12:03 ` [PATCH v4 14/16] iommu/arm-smmu: Unregister IOMMU and bus ops on device removal Will Deacon
                   ` (5 subsequent siblings)
  18 siblings, 0 replies; 26+ messages in thread
From: Will Deacon @ 2019-12-19 12:03 UTC (permalink / raw)
  To: linux-kernel, iommu
  Cc: kernel-team, Will Deacon, Jean-Philippe Brucker, Jordan Crouse,
	John Garry, Bjorn Helgaas, Saravana Kannan, Greg Kroah-Hartman,
	Isaac J. Manjarres, Robin Murphy, Lorenzo Pieralisi,
	Joerg Roedel, Ard Biesheuvel

By removing the redundant call to 'pci_request_acs()' we can allow the
ARM SMMUv3 driver to be built as a module.

Signed-off-by: Will Deacon <will@kernel.org>
---
 drivers/iommu/Kconfig       | 2 +-
 drivers/iommu/arm-smmu-v3.c | 3 +--
 2 files changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig
index 0b9d78a0f3ac..31f0f376ad11 100644
--- a/drivers/iommu/Kconfig
+++ b/drivers/iommu/Kconfig
@@ -394,7 +394,7 @@ config ARM_SMMU_DISABLE_BYPASS_BY_DEFAULT
 	  config.
 
 config ARM_SMMU_V3
-	bool "ARM Ltd. System MMU Version 3 (SMMUv3) Support"
+	tristate "ARM Ltd. System MMU Version 3 (SMMUv3) Support"
 	depends on ARM64
 	select IOMMU_API
 	select IOMMU_IO_PGTABLE_LPAE
diff --git a/drivers/iommu/arm-smmu-v3.c b/drivers/iommu/arm-smmu-v3.c
index da9474a02668..2a65d9b87125 100644
--- a/drivers/iommu/arm-smmu-v3.c
+++ b/drivers/iommu/arm-smmu-v3.c
@@ -2733,6 +2733,7 @@ static struct iommu_ops arm_smmu_ops = {
 	.get_resv_regions	= arm_smmu_get_resv_regions,
 	.put_resv_regions	= arm_smmu_put_resv_regions,
 	.pgsize_bitmap		= -1UL, /* Restricted during device attach */
+	.owner			= THIS_MODULE,
 };
 
 /* Probing and initialisation functions */
@@ -3571,8 +3572,6 @@ static int arm_smmu_set_bus_ops(struct iommu_ops *ops)
 
 #ifdef CONFIG_PCI
 	if (pci_bus_type.iommu_ops != ops) {
-		if (ops)
-			pci_request_acs();
 		err = bus_set_iommu(&pci_bus_type, ops);
 		if (err)
 			return err;
-- 
2.24.1.735.g03f4e72817-goog


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v4 14/16] iommu/arm-smmu: Unregister IOMMU and bus ops on device removal
  2019-12-19 12:03 [PATCH v4 00/16] iommu: Permit modular builds of ARM SMMU[v3] drivers Will Deacon
                   ` (12 preceding siblings ...)
  2019-12-19 12:03 ` [PATCH v4 13/16] iommu/arm-smmu-v3: Allow building as a module Will Deacon
@ 2019-12-19 12:03 ` Will Deacon
  2019-12-19 12:03 ` [PATCH v4 15/16] iommu/arm-smmu: Allow building as a module Will Deacon
                   ` (4 subsequent siblings)
  18 siblings, 0 replies; 26+ messages in thread
From: Will Deacon @ 2019-12-19 12:03 UTC (permalink / raw)
  To: linux-kernel, iommu
  Cc: kernel-team, Will Deacon, Jean-Philippe Brucker, Jordan Crouse,
	John Garry, Bjorn Helgaas, Saravana Kannan, Greg Kroah-Hartman,
	Isaac J. Manjarres, Robin Murphy, Lorenzo Pieralisi,
	Joerg Roedel, Ard Biesheuvel

When removing the SMMU driver, we need to clear any state that we
registered during probe. This includes our bus ops, sysfs entries and
the IOMMU device registered for early firmware probing of masters.

Signed-off-by: Will Deacon <will@kernel.org>
---
 drivers/iommu/arm-smmu.c | 50 ++++++++++++++++++++++++++++++++--------
 1 file changed, 40 insertions(+), 10 deletions(-)

diff --git a/drivers/iommu/arm-smmu.c b/drivers/iommu/arm-smmu.c
index 5d2f60bb9e50..1f0c09bf112a 100644
--- a/drivers/iommu/arm-smmu.c
+++ b/drivers/iommu/arm-smmu.c
@@ -2009,25 +2009,51 @@ static int arm_smmu_device_dt_probe(struct platform_device *pdev,
 	return 0;
 }
 
-static void arm_smmu_bus_init(void)
+static int arm_smmu_bus_init(struct iommu_ops *ops)
 {
+	int err;
+
 	/* Oh, for a proper bus abstraction */
-	if (!iommu_present(&platform_bus_type))
-		bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
+	if (!iommu_present(&platform_bus_type)) {
+		err = bus_set_iommu(&platform_bus_type, ops);
+		if (err)
+			return err;
+	}
 #ifdef CONFIG_ARM_AMBA
-	if (!iommu_present(&amba_bustype))
-		bus_set_iommu(&amba_bustype, &arm_smmu_ops);
+	if (!iommu_present(&amba_bustype)) {
+		err = bus_set_iommu(&amba_bustype, ops);
+		if (err)
+			goto err_reset_platform_ops;
+	}
 #endif
 #ifdef CONFIG_PCI
 	if (!iommu_present(&pci_bus_type)) {
 		pci_request_acs();
-		bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
+		err = bus_set_iommu(&pci_bus_type, ops);
+		if (err)
+			goto err_reset_amba_ops;
 	}
 #endif
 #ifdef CONFIG_FSL_MC_BUS
-	if (!iommu_present(&fsl_mc_bus_type))
-		bus_set_iommu(&fsl_mc_bus_type, &arm_smmu_ops);
+	if (!iommu_present(&fsl_mc_bus_type)) {
+		err = bus_set_iommu(&fsl_mc_bus_type, ops);
+		if (err)
+			goto err_reset_pci_ops;
+	}
+#endif
+	return 0;
+
+err_reset_pci_ops: __maybe_unused;
+#ifdef CONFIG_PCI
+	bus_set_iommu(&pci_bus_type, NULL);
 #endif
+err_reset_amba_ops: __maybe_unused;
+#ifdef CONFIG_ARM_AMBA
+	bus_set_iommu(&amba_bustype, NULL);
+#endif
+err_reset_platform_ops: __maybe_unused;
+	bus_set_iommu(&platform_bus_type, NULL);
+	return err;
 }
 
 static int arm_smmu_device_probe(struct platform_device *pdev)
@@ -2173,7 +2199,7 @@ static int arm_smmu_device_probe(struct platform_device *pdev)
 	 * ready to handle default domain setup as soon as any SMMU exists.
 	 */
 	if (!using_legacy_binding)
-		arm_smmu_bus_init();
+		return arm_smmu_bus_init(&arm_smmu_ops);
 
 	return 0;
 }
@@ -2187,7 +2213,7 @@ static int arm_smmu_device_probe(struct platform_device *pdev)
 static int arm_smmu_legacy_bus_init(void)
 {
 	if (using_legacy_binding)
-		arm_smmu_bus_init();
+		return arm_smmu_bus_init(&arm_smmu_ops);
 	return 0;
 }
 device_initcall_sync(arm_smmu_legacy_bus_init);
@@ -2202,6 +2228,10 @@ static int arm_smmu_device_remove(struct platform_device *pdev)
 	if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
 		dev_err(&pdev->dev, "removing device with active domains!\n");
 
+	arm_smmu_bus_init(NULL);
+	iommu_device_unregister(&smmu->iommu);
+	iommu_device_sysfs_remove(&smmu->iommu);
+
 	arm_smmu_rpm_get(smmu);
 	/* Turn the thing off */
 	arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_sCR0, sCR0_CLIENTPD);
-- 
2.24.1.735.g03f4e72817-goog


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v4 15/16] iommu/arm-smmu: Allow building as a module
  2019-12-19 12:03 [PATCH v4 00/16] iommu: Permit modular builds of ARM SMMU[v3] drivers Will Deacon
                   ` (13 preceding siblings ...)
  2019-12-19 12:03 ` [PATCH v4 14/16] iommu/arm-smmu: Unregister IOMMU and bus ops on device removal Will Deacon
@ 2019-12-19 12:03 ` Will Deacon
  2019-12-19 12:03 ` [PATCH v4 16/16] iommu/arm-smmu: Update my email address in MODULE_AUTHOR() Will Deacon
                   ` (3 subsequent siblings)
  18 siblings, 0 replies; 26+ messages in thread
From: Will Deacon @ 2019-12-19 12:03 UTC (permalink / raw)
  To: linux-kernel, iommu
  Cc: kernel-team, Will Deacon, Jean-Philippe Brucker, Jordan Crouse,
	John Garry, Bjorn Helgaas, Saravana Kannan, Greg Kroah-Hartman,
	Isaac J. Manjarres, Robin Murphy, Lorenzo Pieralisi,
	Joerg Roedel, Ard Biesheuvel

By conditionally dropping support for the legacy binding and exporting
the newly introduced 'arm_smmu_impl_init()' function we can allow the
ARM SMMU driver to be built as a module.

Signed-off-by: Will Deacon <will@kernel.org>
---
 drivers/iommu/Kconfig    | 14 ++++++++++-
 drivers/iommu/Makefile   |  3 ++-
 drivers/iommu/arm-smmu.c | 52 ++++++++++++++++++++++++----------------
 3 files changed, 47 insertions(+), 22 deletions(-)

diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig
index 31f0f376ad11..d66ace717cf4 100644
--- a/drivers/iommu/Kconfig
+++ b/drivers/iommu/Kconfig
@@ -356,7 +356,7 @@ config SPAPR_TCE_IOMMU
 
 # ARM IOMMU support
 config ARM_SMMU
-	bool "ARM Ltd. System MMU (SMMU) Support"
+	tristate "ARM Ltd. System MMU (SMMU) Support"
 	depends on (ARM64 || ARM) && MMU
 	select IOMMU_API
 	select IOMMU_IO_PGTABLE_LPAE
@@ -368,6 +368,18 @@ config ARM_SMMU
 	  Say Y here if your SoC includes an IOMMU device implementing
 	  the ARM SMMU architecture.
 
+config ARM_SMMU_LEGACY_DT_BINDINGS
+	bool "Support the legacy \"mmu-masters\" devicetree bindings"
+	depends on ARM_SMMU=y && OF
+	help
+	  Support for the badly designed and deprecated "mmu-masters"
+	  devicetree bindings. This allows some DMA masters to attach
+	  to the SMMU but does not provide any support via the DMA API.
+	  If you're lucky, you might be able to get VFIO up and running.
+
+	  If you say Y here then you'll make me very sad. Instead, say N
+	  and move your firmware to the utopian future that was 2016.
+
 config ARM_SMMU_DISABLE_BYPASS_BY_DEFAULT
 	bool "Default to disabling bypass on ARM SMMU v1 and v2"
 	depends on ARM_SMMU
diff --git a/drivers/iommu/Makefile b/drivers/iommu/Makefile
index 97814cc861ea..2104fb8afc06 100644
--- a/drivers/iommu/Makefile
+++ b/drivers/iommu/Makefile
@@ -14,7 +14,8 @@ obj-$(CONFIG_MSM_IOMMU) += msm_iommu.o
 obj-$(CONFIG_AMD_IOMMU) += amd_iommu.o amd_iommu_init.o amd_iommu_quirks.o
 obj-$(CONFIG_AMD_IOMMU_DEBUGFS) += amd_iommu_debugfs.o
 obj-$(CONFIG_AMD_IOMMU_V2) += amd_iommu_v2.o
-obj-$(CONFIG_ARM_SMMU) += arm-smmu.o arm-smmu-impl.o arm-smmu-qcom.o
+obj-$(CONFIG_ARM_SMMU) += arm-smmu-mod.o
+arm-smmu-mod-objs += arm-smmu.o arm-smmu-impl.o arm-smmu-qcom.o
 obj-$(CONFIG_ARM_SMMU_V3) += arm-smmu-v3.o
 obj-$(CONFIG_DMAR_TABLE) += dmar.o
 obj-$(CONFIG_INTEL_IOMMU) += intel-iommu.o intel-pasid.o
diff --git a/drivers/iommu/arm-smmu.c b/drivers/iommu/arm-smmu.c
index 1f0c09bf112a..3cfa138fbd4f 100644
--- a/drivers/iommu/arm-smmu.c
+++ b/drivers/iommu/arm-smmu.c
@@ -126,6 +126,12 @@ static struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
 	return container_of(dom, struct arm_smmu_domain, domain);
 }
 
+static struct platform_driver arm_smmu_driver;
+static struct iommu_ops arm_smmu_ops;
+
+#ifdef CONFIG_ARM_SMMU_LEGACY_DT_BINDINGS
+static int arm_smmu_bus_init(struct iommu_ops *ops);
+
 static struct device_node *dev_get_dev_node(struct device *dev)
 {
 	if (dev_is_pci(dev)) {
@@ -161,9 +167,6 @@ static int __find_legacy_master_phandle(struct device *dev, void *data)
 	return err == -ENOENT ? 0 : err;
 }
 
-static struct platform_driver arm_smmu_driver;
-static struct iommu_ops arm_smmu_ops;
-
 static int arm_smmu_register_legacy_master(struct device *dev,
 					   struct arm_smmu_device **smmu)
 {
@@ -215,6 +218,27 @@ static int arm_smmu_register_legacy_master(struct device *dev,
 	return err;
 }
 
+/*
+ * With the legacy DT binding in play, we have no guarantees about
+ * probe order, but then we're also not doing default domains, so we can
+ * delay setting bus ops until we're sure every possible SMMU is ready,
+ * and that way ensure that no add_device() calls get missed.
+ */
+static int arm_smmu_legacy_bus_init(void)
+{
+	if (using_legacy_binding)
+		return arm_smmu_bus_init(&arm_smmu_ops);
+	return 0;
+}
+device_initcall_sync(arm_smmu_legacy_bus_init);
+#else
+static int arm_smmu_register_legacy_master(struct device *dev,
+					   struct arm_smmu_device **smmu)
+{
+	return -ENODEV;
+}
+#endif /* CONFIG_ARM_SMMU_LEGACY_DT_BINDINGS */
+
 static int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end)
 {
 	int idx;
@@ -1599,6 +1623,7 @@ static struct iommu_ops arm_smmu_ops = {
 	.get_resv_regions	= arm_smmu_get_resv_regions,
 	.put_resv_regions	= arm_smmu_put_resv_regions,
 	.pgsize_bitmap		= -1UL, /* Restricted during device attach */
+	.owner			= THIS_MODULE,
 };
 
 static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
@@ -1993,8 +2018,10 @@ static int arm_smmu_device_dt_probe(struct platform_device *pdev,
 
 	legacy_binding = of_find_property(dev->of_node, "mmu-masters", NULL);
 	if (legacy_binding && !using_generic_binding) {
-		if (!using_legacy_binding)
-			pr_notice("deprecated \"mmu-masters\" DT property in use; DMA API support unavailable\n");
+		if (!using_legacy_binding) {
+			pr_notice("deprecated \"mmu-masters\" DT property in use; %s support unavailable\n",
+				  IS_ENABLED(CONFIG_ARM_SMMU_LEGACY_DT_BINDINGS) ? "DMA API" : "SMMU");
+		}
 		using_legacy_binding = true;
 	} else if (!legacy_binding && !using_legacy_binding) {
 		using_generic_binding = true;
@@ -2028,7 +2055,6 @@ static int arm_smmu_bus_init(struct iommu_ops *ops)
 #endif
 #ifdef CONFIG_PCI
 	if (!iommu_present(&pci_bus_type)) {
-		pci_request_acs();
 		err = bus_set_iommu(&pci_bus_type, ops);
 		if (err)
 			goto err_reset_amba_ops;
@@ -2204,20 +2230,6 @@ static int arm_smmu_device_probe(struct platform_device *pdev)
 	return 0;
 }
 
-/*
- * With the legacy DT binding in play, though, we have no guarantees about
- * probe order, but then we're also not doing default domains, so we can
- * delay setting bus ops until we're sure every possible SMMU is ready,
- * and that way ensure that no add_device() calls get missed.
- */
-static int arm_smmu_legacy_bus_init(void)
-{
-	if (using_legacy_binding)
-		return arm_smmu_bus_init(&arm_smmu_ops);
-	return 0;
-}
-device_initcall_sync(arm_smmu_legacy_bus_init);
-
 static int arm_smmu_device_remove(struct platform_device *pdev)
 {
 	struct arm_smmu_device *smmu = platform_get_drvdata(pdev);
-- 
2.24.1.735.g03f4e72817-goog


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v4 16/16] iommu/arm-smmu: Update my email address in MODULE_AUTHOR()
  2019-12-19 12:03 [PATCH v4 00/16] iommu: Permit modular builds of ARM SMMU[v3] drivers Will Deacon
                   ` (14 preceding siblings ...)
  2019-12-19 12:03 ` [PATCH v4 15/16] iommu/arm-smmu: Allow building as a module Will Deacon
@ 2019-12-19 12:03 ` Will Deacon
  2019-12-19 12:12 ` [PATCH v4 00/16] iommu: Permit modular builds of ARM SMMU[v3] drivers John Garry
                   ` (2 subsequent siblings)
  18 siblings, 0 replies; 26+ messages in thread
From: Will Deacon @ 2019-12-19 12:03 UTC (permalink / raw)
  To: linux-kernel, iommu
  Cc: kernel-team, Will Deacon, Jean-Philippe Brucker, Jordan Crouse,
	John Garry, Bjorn Helgaas, Saravana Kannan, Greg Kroah-Hartman,
	Isaac J. Manjarres, Robin Murphy, Lorenzo Pieralisi,
	Joerg Roedel, Ard Biesheuvel

I no longer work for Arm, so update the stale reference to my old email
address.

Signed-off-by: Will Deacon <will@kernel.org>
---
 drivers/iommu/arm-smmu-v3.c | 2 +-
 drivers/iommu/arm-smmu.c    | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/iommu/arm-smmu-v3.c b/drivers/iommu/arm-smmu-v3.c
index 2a65d9b87125..03dc97842875 100644
--- a/drivers/iommu/arm-smmu-v3.c
+++ b/drivers/iommu/arm-smmu-v3.c
@@ -3732,6 +3732,6 @@ static struct platform_driver arm_smmu_driver = {
 module_platform_driver(arm_smmu_driver);
 
 MODULE_DESCRIPTION("IOMMU API for ARM architected SMMUv3 implementations");
-MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
+MODULE_AUTHOR("Will Deacon <will@kernel.org>");
 MODULE_ALIAS("platform:arm-smmu-v3");
 MODULE_LICENSE("GPL v2");
diff --git a/drivers/iommu/arm-smmu.c b/drivers/iommu/arm-smmu.c
index 3cfa138fbd4f..5ef1f2e100d7 100644
--- a/drivers/iommu/arm-smmu.c
+++ b/drivers/iommu/arm-smmu.c
@@ -2322,6 +2322,6 @@ static struct platform_driver arm_smmu_driver = {
 module_platform_driver(arm_smmu_driver);
 
 MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
-MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
+MODULE_AUTHOR("Will Deacon <will@kernel.org>");
 MODULE_ALIAS("platform:arm-smmu");
 MODULE_LICENSE("GPL v2");
-- 
2.24.1.735.g03f4e72817-goog


^ permalink raw reply related	[flat|nested] 26+ messages in thread

* Re: [PATCH v4 00/16] iommu: Permit modular builds of ARM SMMU[v3] drivers
  2019-12-19 12:03 [PATCH v4 00/16] iommu: Permit modular builds of ARM SMMU[v3] drivers Will Deacon
                   ` (15 preceding siblings ...)
  2019-12-19 12:03 ` [PATCH v4 16/16] iommu/arm-smmu: Update my email address in MODULE_AUTHOR() Will Deacon
@ 2019-12-19 12:12 ` John Garry
  2019-12-19 14:44 ` Greg Kroah-Hartman
  2019-12-20 16:13 ` Joerg Roedel
  18 siblings, 0 replies; 26+ messages in thread
From: John Garry @ 2019-12-19 12:12 UTC (permalink / raw)
  To: Will Deacon, linux-kernel, iommu
  Cc: kernel-team, Jean-Philippe Brucker, Jordan Crouse, Bjorn Helgaas,
	Saravana Kannan, Greg Kroah-Hartman, Isaac J. Manjarres,
	Robin Murphy, Lorenzo Pieralisi, Joerg Roedel, Ard Biesheuvel

On 19/12/2019 12:03, Will Deacon wrote:
> Hi all,
> 
> This is version four of the patches I previously posted here:
> 
>    v1: https://lore.kernel.org/lkml/20191030145112.19738-1-will@kernel.org/
>    v2: https://lore.kernel.org/lkml/20191108151608.20932-1-will@kernel.org
>    v3: https://lore.kernel.org/lkml/20191121114918.2293-1-will@kernel.org
> 
> Changes since v3 include:
> 
>    * Based on v5.5-rc1
>    * ACPI/IORT support (thanks to Ard)
>    * Export pci_{enable,disable}_ats() (thanks to Greg)
>    * Added review tags

Since it looks like not much has changed since v3, and I even tested 
removal of smmu-v3.ko [0] - this should be ok for the series:

Tested-by: John Garry <john.garry@huawei.com> # smmu v3

[0] 
https://lore.kernel.org/linux-iommu/c8eb97b1-dab5-cc25-7669-2819f64a885d@huawei.com/

> 
> I tested this on AMD Seattle by loading arm-smmu-mod.ko from the initrd.
> 
> Cheers,
> 
> Will
> 
> Cc: Jean-Philippe Brucker <jean-philippe@linaro.org>
> Cc: Jordan Crouse <jcrouse@codeaurora.org>
> Cc: John Garry <john.garry@huawei.com>
> Cc: Bjorn Helgaas <bhelgaas@google.com>
> Cc: Saravana Kannan <saravanak@google.com>
> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Cc: "Isaac J. Manjarres" <isaacm@codeaurora.org>
> Cc: Robin Murphy <robin.murphy@arm.com>
> Cc: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
> Cc: Joerg Roedel <joro@8bytes.org>
> Cc: Ard Biesheuvel <ardb@kernel.org>
> 
> --->8
> 
> Ard Biesheuvel (1):
>    iommu/arm-smmu: Support SMMU module probing from the IORT
> 
> Greg Kroah-Hartman (1):
>    PCI/ATS: Restore EXPORT_SYMBOL_GPL() for pci_{enable,disable}_ats()
> 
> Will Deacon (14):
>    drivers/iommu: Export core IOMMU API symbols to permit modular drivers
>    iommu/of: Request ACS from the PCI core when configuring IOMMU linkage
>    PCI: Export pci_ats_disabled() as a GPL symbol to modules
>    drivers/iommu: Take a ref to the IOMMU driver prior to ->add_device()
>    iommu/of: Take a ref to the IOMMU driver during ->of_xlate()
>    drivers/iommu: Allow IOMMU bus ops to be unregistered
>    Revert "iommu/arm-smmu: Make arm-smmu-v3 explicitly non-modular"
>    Revert "iommu/arm-smmu: Make arm-smmu explicitly non-modular"
>    iommu/arm-smmu: Prevent forced unbinding of Arm SMMU drivers
>    iommu/arm-smmu-v3: Unregister IOMMU and bus ops on device removal
>    iommu/arm-smmu-v3: Allow building as a module
>    iommu/arm-smmu: Unregister IOMMU and bus ops on device removal
>    iommu/arm-smmu: Allow building as a module
>    iommu/arm-smmu: Update my email address in MODULE_AUTHOR()
> 
>   drivers/acpi/arm64/iort.c   |   4 +-
>   drivers/iommu/Kconfig       |  16 ++++-
>   drivers/iommu/Makefile      |   3 +-
>   drivers/iommu/arm-smmu-v3.c |  94 +++++++++++++++++---------
>   drivers/iommu/arm-smmu.c    | 128 +++++++++++++++++++++++++-----------
>   drivers/iommu/iommu-sysfs.c |   5 ++
>   drivers/iommu/iommu.c       |  32 ++++++++-
>   drivers/iommu/of_iommu.c    |  19 ++++--
>   drivers/pci/ats.c           |   2 +
>   drivers/pci/pci.c           |   1 +
>   include/linux/iommu.h       |   4 +-
>   11 files changed, 223 insertions(+), 85 deletions(-)
> 


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v4 05/16] drivers/iommu: Take a ref to the IOMMU driver prior to ->add_device()
  2019-12-19 12:03 ` [PATCH v4 05/16] drivers/iommu: Take a ref to the IOMMU driver prior to ->add_device() Will Deacon
@ 2019-12-19 14:44   ` Greg Kroah-Hartman
  2020-01-09 14:16     ` Will Deacon
  0 siblings, 1 reply; 26+ messages in thread
From: Greg Kroah-Hartman @ 2019-12-19 14:44 UTC (permalink / raw)
  To: Will Deacon
  Cc: linux-kernel, iommu, kernel-team, Jean-Philippe Brucker,
	Jordan Crouse, John Garry, Bjorn Helgaas, Saravana Kannan,
	Isaac J. Manjarres, Robin Murphy, Lorenzo Pieralisi,
	Joerg Roedel, Ard Biesheuvel

On Thu, Dec 19, 2019 at 12:03:41PM +0000, Will Deacon wrote:
> To avoid accidental removal of an active IOMMU driver module, take a
> reference to the driver module in 'iommu_probe_device()' immediately
> prior to invoking the '->add_device()' callback and hold it until the
> after the device has been removed by '->remove_device()'.
> 
> Suggested-by: Joerg Roedel <joro@8bytes.org>
> Signed-off-by: Will Deacon <will@kernel.org>
> ---
>  drivers/iommu/iommu.c | 19 +++++++++++++++++--
>  include/linux/iommu.h |  4 +++-
>  2 files changed, 20 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
> index 7c92197d53f3..bc8edf90e729 100644
> --- a/drivers/iommu/iommu.c
> +++ b/drivers/iommu/iommu.c
> @@ -22,6 +22,7 @@
>  #include <linux/bitops.h>
>  #include <linux/property.h>
>  #include <linux/fsl/mc.h>
> +#include <linux/module.h>
>  #include <trace/events/iommu.h>
>  
>  static struct kset *iommu_group_kset;
> @@ -185,10 +186,21 @@ int iommu_probe_device(struct device *dev)
>  	if (!iommu_get_dev_param(dev))
>  		return -ENOMEM;
>  
> +	if (!try_module_get(ops->owner)) {
> +		ret = -EINVAL;
> +		goto err_free_dev_param;
> +	}
> +
>  	ret = ops->add_device(dev);
>  	if (ret)
> -		iommu_free_dev_param(dev);
> +		goto err_module_put;
> +
> +	return 0;
>  
> +err_module_put:
> +	module_put(ops->owner);
> +err_free_dev_param:
> +	iommu_free_dev_param(dev);
>  	return ret;
>  }
>  
> @@ -199,7 +211,10 @@ void iommu_release_device(struct device *dev)
>  	if (dev->iommu_group)
>  		ops->remove_device(dev);
>  
> -	iommu_free_dev_param(dev);
> +	if (dev->iommu_param) {
> +		module_put(ops->owner);
> +		iommu_free_dev_param(dev);
> +	}
>  }
>  
>  static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
> diff --git a/include/linux/iommu.h b/include/linux/iommu.h
> index f2223cbb5fd5..e9f94d3f7a04 100644
> --- a/include/linux/iommu.h
> +++ b/include/linux/iommu.h
> @@ -246,9 +246,10 @@ struct iommu_iotlb_gather {
>   * @sva_get_pasid: Get PASID associated to a SVA handle
>   * @page_response: handle page request response
>   * @cache_invalidate: invalidate translation caches
> - * @pgsize_bitmap: bitmap of all possible supported page sizes
>   * @sva_bind_gpasid: bind guest pasid and mm
>   * @sva_unbind_gpasid: unbind guest pasid and mm
> + * @pgsize_bitmap: bitmap of all possible supported page sizes
> + * @owner: Driver module providing these ops
>   */
>  struct iommu_ops {
>  	bool (*capable)(enum iommu_cap);
> @@ -318,6 +319,7 @@ struct iommu_ops {
>  	int (*sva_unbind_gpasid)(struct device *dev, int pasid);
>  
>  	unsigned long pgsize_bitmap;
> +	struct module *owner;

Everyone is always going to forget to set this field.  I don't think you
even set it for all of the different iommu_ops possible in this series,
right?

The "trick" we did to keep people from having to remember this is to do
what we did for the bus registering functions.

Look at pci_register_driver in pci.h:
#define pci_register_driver(driver)             \
        __pci_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)

Then we set the .owner field in the "real" __pci_register_driver() call.

Same thing for USB and lots, if not all, other driver register
functions.

You can do the same thing here, and I would recommend it.

No need to stop this series from happening now, just an add-on that is
easy to make to ensure that no one ever forgets to set this field
properly.

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v4 00/16] iommu: Permit modular builds of ARM SMMU[v3] drivers
  2019-12-19 12:03 [PATCH v4 00/16] iommu: Permit modular builds of ARM SMMU[v3] drivers Will Deacon
                   ` (16 preceding siblings ...)
  2019-12-19 12:12 ` [PATCH v4 00/16] iommu: Permit modular builds of ARM SMMU[v3] drivers John Garry
@ 2019-12-19 14:44 ` Greg Kroah-Hartman
  2019-12-20 16:13 ` Joerg Roedel
  18 siblings, 0 replies; 26+ messages in thread
From: Greg Kroah-Hartman @ 2019-12-19 14:44 UTC (permalink / raw)
  To: Will Deacon
  Cc: linux-kernel, iommu, kernel-team, Jean-Philippe Brucker,
	Jordan Crouse, John Garry, Bjorn Helgaas, Saravana Kannan,
	Isaac J. Manjarres, Robin Murphy, Lorenzo Pieralisi,
	Joerg Roedel, Ard Biesheuvel

On Thu, Dec 19, 2019 at 12:03:36PM +0000, Will Deacon wrote:
> Hi all,
> 
> This is version four of the patches I previously posted here:
> 
>   v1: https://lore.kernel.org/lkml/20191030145112.19738-1-will@kernel.org/
>   v2: https://lore.kernel.org/lkml/20191108151608.20932-1-will@kernel.org
>   v3: https://lore.kernel.org/lkml/20191121114918.2293-1-will@kernel.org
> 
> Changes since v3 include:
> 
>   * Based on v5.5-rc1
>   * ACPI/IORT support (thanks to Ard)
>   * Export pci_{enable,disable}_ats() (thanks to Greg)
>   * Added review tags
> 
> I tested this on AMD Seattle by loading arm-smmu-mod.ko from the initrd.

All look good to me!

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v4 03/16] PCI/ATS: Restore EXPORT_SYMBOL_GPL() for pci_{enable,disable}_ats()
  2019-12-19 12:03 ` [PATCH v4 03/16] PCI/ATS: Restore EXPORT_SYMBOL_GPL() for pci_{enable,disable}_ats() Will Deacon
@ 2019-12-20  8:43   ` Joerg Roedel
  2019-12-20 15:03     ` Bjorn Helgaas
  0 siblings, 1 reply; 26+ messages in thread
From: Joerg Roedel @ 2019-12-20  8:43 UTC (permalink / raw)
  To: Will Deacon, Bjorn Helgaas
  Cc: linux-kernel, iommu, kernel-team, Jean-Philippe Brucker,
	Jordan Crouse, John Garry, Saravana Kannan, Greg Kroah-Hartman,
	Isaac J. Manjarres, Robin Murphy, Lorenzo Pieralisi,
	Ard Biesheuvel, Greg Kroah-Hartman, Joerg Roedel

Hi Bjorn,

On Thu, Dec 19, 2019 at 12:03:39PM +0000, Will Deacon wrote:
> From: Greg Kroah-Hartman <gregkh@google.com>
> 
> Commit d355bb209783 ("PCI/ATS: Remove unnecessary EXPORT_SYMBOL_GPL()")
> unexported a bunch of symbols from the PCI core since the only external
> users were non-modular IOMMU drivers. Although most of those symbols
> can remain private for now, 'pci_{enable,disable_ats()' is required for
> the ARM SMMUv3 driver to build as a module, otherwise we get a build
> failure as follows:
> 
>   | ERROR: "pci_enable_ats" [drivers/iommu/arm-smmu-v3.ko] undefined!
>   | ERROR: "pci_disable_ats" [drivers/iommu/arm-smmu-v3.ko] undefined!
> 
> Re-export these two functions so that the ARM SMMUv3 driver can be build
> as a module.
> 
> Cc: Bjorn Helgaas <bhelgaas@google.com>
> Cc: Joerg Roedel <jroedel@suse.de>
> Signed-off-by: Greg Kroah-Hartman <gregkh@google.com>
> [will: rewrote commit message]
> Signed-off-by: Will Deacon <will@kernel.org>
> ---
>  drivers/pci/ats.c | 2 ++
>  1 file changed, 2 insertions(+)

Are you fine with this change? I would apply this series to my tree
then.

Regards,

	Joerg


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v4 03/16] PCI/ATS: Restore EXPORT_SYMBOL_GPL() for pci_{enable,disable}_ats()
  2019-12-20  8:43   ` Joerg Roedel
@ 2019-12-20 15:03     ` Bjorn Helgaas
  0 siblings, 0 replies; 26+ messages in thread
From: Bjorn Helgaas @ 2019-12-20 15:03 UTC (permalink / raw)
  To: Joerg Roedel
  Cc: Will Deacon, linux-kernel, iommu, kernel-team,
	Jean-Philippe Brucker, Jordan Crouse, John Garry,
	Saravana Kannan, Greg Kroah-Hartman, Isaac J. Manjarres,
	Robin Murphy, Lorenzo Pieralisi, Ard Biesheuvel,
	Greg Kroah-Hartman, Joerg Roedel

On Fri, Dec 20, 2019 at 09:43:03AM +0100, Joerg Roedel wrote:
> Hi Bjorn,
> 
> On Thu, Dec 19, 2019 at 12:03:39PM +0000, Will Deacon wrote:
> > From: Greg Kroah-Hartman <gregkh@google.com>
> > 
> > Commit d355bb209783 ("PCI/ATS: Remove unnecessary EXPORT_SYMBOL_GPL()")
> > unexported a bunch of symbols from the PCI core since the only external
> > users were non-modular IOMMU drivers. Although most of those symbols
> > can remain private for now, 'pci_{enable,disable_ats()' is required for
> > the ARM SMMUv3 driver to build as a module, otherwise we get a build
> > failure as follows:
> > 
> >   | ERROR: "pci_enable_ats" [drivers/iommu/arm-smmu-v3.ko] undefined!
> >   | ERROR: "pci_disable_ats" [drivers/iommu/arm-smmu-v3.ko] undefined!
> > 
> > Re-export these two functions so that the ARM SMMUv3 driver can be build
> > as a module.
> > 
> > Cc: Bjorn Helgaas <bhelgaas@google.com>
> > Cc: Joerg Roedel <jroedel@suse.de>
> > Signed-off-by: Greg Kroah-Hartman <gregkh@google.com>
> > [will: rewrote commit message]
> > Signed-off-by: Will Deacon <will@kernel.org>
> > ---
> >  drivers/pci/ats.c | 2 ++
> >  1 file changed, 2 insertions(+)
> 
> Are you fine with this change? I would apply this series to my tree
> then.

Yep, thanks!  You can add my

Acked-by: Bjorn Helgaas <bhelgaas@google.com>


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v4 00/16] iommu: Permit modular builds of ARM SMMU[v3] drivers
  2019-12-19 12:03 [PATCH v4 00/16] iommu: Permit modular builds of ARM SMMU[v3] drivers Will Deacon
                   ` (17 preceding siblings ...)
  2019-12-19 14:44 ` Greg Kroah-Hartman
@ 2019-12-20 16:13 ` Joerg Roedel
  2020-01-06 10:48   ` Will Deacon
  18 siblings, 1 reply; 26+ messages in thread
From: Joerg Roedel @ 2019-12-20 16:13 UTC (permalink / raw)
  To: Will Deacon
  Cc: linux-kernel, iommu, kernel-team, Jean-Philippe Brucker,
	Jordan Crouse, John Garry, Bjorn Helgaas, Saravana Kannan,
	Greg Kroah-Hartman, Isaac J. Manjarres, Robin Murphy,
	Lorenzo Pieralisi, Ard Biesheuvel

On Thu, Dec 19, 2019 at 12:03:36PM +0000, Will Deacon wrote:
> Ard Biesheuvel (1):
>   iommu/arm-smmu: Support SMMU module probing from the IORT
> 
> Greg Kroah-Hartman (1):
>   PCI/ATS: Restore EXPORT_SYMBOL_GPL() for pci_{enable,disable}_ats()
> 
> Will Deacon (14):
>   drivers/iommu: Export core IOMMU API symbols to permit modular drivers
>   iommu/of: Request ACS from the PCI core when configuring IOMMU linkage
>   PCI: Export pci_ats_disabled() as a GPL symbol to modules
>   drivers/iommu: Take a ref to the IOMMU driver prior to ->add_device()
>   iommu/of: Take a ref to the IOMMU driver during ->of_xlate()
>   drivers/iommu: Allow IOMMU bus ops to be unregistered
>   Revert "iommu/arm-smmu: Make arm-smmu-v3 explicitly non-modular"
>   Revert "iommu/arm-smmu: Make arm-smmu explicitly non-modular"
>   iommu/arm-smmu: Prevent forced unbinding of Arm SMMU drivers
>   iommu/arm-smmu-v3: Unregister IOMMU and bus ops on device removal
>   iommu/arm-smmu-v3: Allow building as a module
>   iommu/arm-smmu: Unregister IOMMU and bus ops on device removal
>   iommu/arm-smmu: Allow building as a module
>   iommu/arm-smmu: Update my email address in MODULE_AUTHOR()
> 
>  drivers/acpi/arm64/iort.c   |   4 +-
>  drivers/iommu/Kconfig       |  16 ++++-
>  drivers/iommu/Makefile      |   3 +-
>  drivers/iommu/arm-smmu-v3.c |  94 +++++++++++++++++---------
>  drivers/iommu/arm-smmu.c    | 128 +++++++++++++++++++++++++-----------
>  drivers/iommu/iommu-sysfs.c |   5 ++
>  drivers/iommu/iommu.c       |  32 ++++++++-
>  drivers/iommu/of_iommu.c    |  19 ++++--
>  drivers/pci/ats.c           |   2 +
>  drivers/pci/pci.c           |   1 +
>  include/linux/iommu.h       |   4 +-
>  11 files changed, 223 insertions(+), 85 deletions(-)

Applied, thanks.

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v4 00/16] iommu: Permit modular builds of ARM SMMU[v3] drivers
  2019-12-20 16:13 ` Joerg Roedel
@ 2020-01-06 10:48   ` Will Deacon
  0 siblings, 0 replies; 26+ messages in thread
From: Will Deacon @ 2020-01-06 10:48 UTC (permalink / raw)
  To: Joerg Roedel
  Cc: linux-kernel, iommu, kernel-team, Jean-Philippe Brucker,
	Jordan Crouse, John Garry, Bjorn Helgaas, Saravana Kannan,
	Greg Kroah-Hartman, Isaac J. Manjarres, Robin Murphy,
	Lorenzo Pieralisi, Ard Biesheuvel

On Fri, Dec 20, 2019 at 05:13:13PM +0100, Joerg Roedel wrote:
> On Thu, Dec 19, 2019 at 12:03:36PM +0000, Will Deacon wrote:
> > Ard Biesheuvel (1):
> >   iommu/arm-smmu: Support SMMU module probing from the IORT
> > 
> > Greg Kroah-Hartman (1):
> >   PCI/ATS: Restore EXPORT_SYMBOL_GPL() for pci_{enable,disable}_ats()
> > 
> > Will Deacon (14):
> >   drivers/iommu: Export core IOMMU API symbols to permit modular drivers
> >   iommu/of: Request ACS from the PCI core when configuring IOMMU linkage
> >   PCI: Export pci_ats_disabled() as a GPL symbol to modules
> >   drivers/iommu: Take a ref to the IOMMU driver prior to ->add_device()
> >   iommu/of: Take a ref to the IOMMU driver during ->of_xlate()
> >   drivers/iommu: Allow IOMMU bus ops to be unregistered
> >   Revert "iommu/arm-smmu: Make arm-smmu-v3 explicitly non-modular"
> >   Revert "iommu/arm-smmu: Make arm-smmu explicitly non-modular"
> >   iommu/arm-smmu: Prevent forced unbinding of Arm SMMU drivers
> >   iommu/arm-smmu-v3: Unregister IOMMU and bus ops on device removal
> >   iommu/arm-smmu-v3: Allow building as a module
> >   iommu/arm-smmu: Unregister IOMMU and bus ops on device removal
> >   iommu/arm-smmu: Allow building as a module
> >   iommu/arm-smmu: Update my email address in MODULE_AUTHOR()
> > 
> >  drivers/acpi/arm64/iort.c   |   4 +-
> >  drivers/iommu/Kconfig       |  16 ++++-
> >  drivers/iommu/Makefile      |   3 +-
> >  drivers/iommu/arm-smmu-v3.c |  94 +++++++++++++++++---------
> >  drivers/iommu/arm-smmu.c    | 128 +++++++++++++++++++++++++-----------
> >  drivers/iommu/iommu-sysfs.c |   5 ++
> >  drivers/iommu/iommu.c       |  32 ++++++++-
> >  drivers/iommu/of_iommu.c    |  19 ++++--
> >  drivers/pci/ats.c           |   2 +
> >  drivers/pci/pci.c           |   1 +
> >  include/linux/iommu.h       |   4 +-
> >  11 files changed, 223 insertions(+), 85 deletions(-)
> 
> Applied, thanks.

Thanks, Joerg! I'll look into the suggestion from Greg regarding 'automatic'
setting of the module 'owner' field and send a follow-up patch if I come up
with something.

Will


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v4 05/16] drivers/iommu: Take a ref to the IOMMU driver prior to ->add_device()
  2019-12-19 14:44   ` Greg Kroah-Hartman
@ 2020-01-09 14:16     ` Will Deacon
  2020-01-09 18:17       ` Greg Kroah-Hartman
  0 siblings, 1 reply; 26+ messages in thread
From: Will Deacon @ 2020-01-09 14:16 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: linux-kernel, iommu, kernel-team, Jean-Philippe Brucker,
	Jordan Crouse, John Garry, Bjorn Helgaas, Saravana Kannan,
	Isaac J. Manjarres, Robin Murphy, Lorenzo Pieralisi,
	Joerg Roedel, Ard Biesheuvel

Hi Greg,

On Thu, Dec 19, 2019 at 03:44:37PM +0100, Greg Kroah-Hartman wrote:
> On Thu, Dec 19, 2019 at 12:03:41PM +0000, Will Deacon wrote:
> > diff --git a/include/linux/iommu.h b/include/linux/iommu.h
> > index f2223cbb5fd5..e9f94d3f7a04 100644
> > --- a/include/linux/iommu.h
> > +++ b/include/linux/iommu.h
> > @@ -246,9 +246,10 @@ struct iommu_iotlb_gather {
> >   * @sva_get_pasid: Get PASID associated to a SVA handle
> >   * @page_response: handle page request response
> >   * @cache_invalidate: invalidate translation caches
> > - * @pgsize_bitmap: bitmap of all possible supported page sizes
> >   * @sva_bind_gpasid: bind guest pasid and mm
> >   * @sva_unbind_gpasid: unbind guest pasid and mm
> > + * @pgsize_bitmap: bitmap of all possible supported page sizes
> > + * @owner: Driver module providing these ops
> >   */
> >  struct iommu_ops {
> >  	bool (*capable)(enum iommu_cap);
> > @@ -318,6 +319,7 @@ struct iommu_ops {
> >  	int (*sva_unbind_gpasid)(struct device *dev, int pasid);
> >  
> >  	unsigned long pgsize_bitmap;
> > +	struct module *owner;
> 
> Everyone is always going to forget to set this field.  I don't think you
> even set it for all of the different iommu_ops possible in this series,
> right?

I only initialised the field for those drivers which can actually be built
as a module, but I take your point about this being error-prone.

> The "trick" we did to keep people from having to remember this is to do
> what we did for the bus registering functions.
> 
> Look at pci_register_driver in pci.h:
> #define pci_register_driver(driver)             \
>         __pci_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
> 
> Then we set the .owner field in the "real" __pci_register_driver() call.
> 
> Same thing for USB and lots, if not all, other driver register
> functions.
> 
> You can do the same thing here, and I would recommend it.

Yes, that makes sense, cheers. Diff below. I'll send it to Joerg along
with some other SMMU patches that have come in since the holiday.

Will

--->8

diff --git a/drivers/iommu/arm-smmu-v3.c b/drivers/iommu/arm-smmu-v3.c
index 03dc97842875..e82997a705a8 100644
--- a/drivers/iommu/arm-smmu-v3.c
+++ b/drivers/iommu/arm-smmu-v3.c
@@ -2733,7 +2733,6 @@ static struct iommu_ops arm_smmu_ops = {
 	.get_resv_regions	= arm_smmu_get_resv_regions,
 	.put_resv_regions	= arm_smmu_put_resv_regions,
 	.pgsize_bitmap		= -1UL, /* Restricted during device attach */
-	.owner			= THIS_MODULE,
 };
 
 /* Probing and initialisation functions */
diff --git a/drivers/iommu/arm-smmu.c b/drivers/iommu/arm-smmu.c
index 5ef1f2e100d7..93d332423f6f 100644
--- a/drivers/iommu/arm-smmu.c
+++ b/drivers/iommu/arm-smmu.c
@@ -1623,7 +1623,6 @@ static struct iommu_ops arm_smmu_ops = {
 	.get_resv_regions	= arm_smmu_get_resv_regions,
 	.put_resv_regions	= arm_smmu_put_resv_regions,
 	.pgsize_bitmap		= -1UL, /* Restricted during device attach */
-	.owner			= THIS_MODULE,
 };
 
 static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
diff --git a/include/linux/iommu.h b/include/linux/iommu.h
index e9f94d3f7a04..90007c92ad2d 100644
--- a/include/linux/iommu.h
+++ b/include/linux/iommu.h
@@ -388,12 +388,19 @@ void iommu_device_sysfs_remove(struct iommu_device *iommu);
 int  iommu_device_link(struct iommu_device   *iommu, struct device *link);
 void iommu_device_unlink(struct iommu_device *iommu, struct device *link);
 
-static inline void iommu_device_set_ops(struct iommu_device *iommu,
-					const struct iommu_ops *ops)
+static inline void __iommu_device_set_ops(struct iommu_device *iommu,
+					  const struct iommu_ops *ops)
 {
 	iommu->ops = ops;
 }
 
+#define iommu_device_set_ops(iommu, ops)				\
+do {									\
+	struct iommu_ops *__ops = (struct iommu_ops *)(ops);		\
+	__ops->owner = THIS_MODULE;					\
+	__iommu_device_set_ops(iommu, __ops);				\
+} while (0)
+
 static inline void iommu_device_set_fwnode(struct iommu_device *iommu,
 					   struct fwnode_handle *fwnode)
 {

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* Re: [PATCH v4 05/16] drivers/iommu: Take a ref to the IOMMU driver prior to ->add_device()
  2020-01-09 14:16     ` Will Deacon
@ 2020-01-09 18:17       ` Greg Kroah-Hartman
  0 siblings, 0 replies; 26+ messages in thread
From: Greg Kroah-Hartman @ 2020-01-09 18:17 UTC (permalink / raw)
  To: Will Deacon
  Cc: linux-kernel, iommu, kernel-team, Jean-Philippe Brucker,
	Jordan Crouse, John Garry, Bjorn Helgaas, Saravana Kannan,
	Isaac J. Manjarres, Robin Murphy, Lorenzo Pieralisi,
	Joerg Roedel, Ard Biesheuvel

On Thu, Jan 09, 2020 at 02:16:03PM +0000, Will Deacon wrote:
> Hi Greg,
> 
> On Thu, Dec 19, 2019 at 03:44:37PM +0100, Greg Kroah-Hartman wrote:
> > On Thu, Dec 19, 2019 at 12:03:41PM +0000, Will Deacon wrote:
> > > diff --git a/include/linux/iommu.h b/include/linux/iommu.h
> > > index f2223cbb5fd5..e9f94d3f7a04 100644
> > > --- a/include/linux/iommu.h
> > > +++ b/include/linux/iommu.h
> > > @@ -246,9 +246,10 @@ struct iommu_iotlb_gather {
> > >   * @sva_get_pasid: Get PASID associated to a SVA handle
> > >   * @page_response: handle page request response
> > >   * @cache_invalidate: invalidate translation caches
> > > - * @pgsize_bitmap: bitmap of all possible supported page sizes
> > >   * @sva_bind_gpasid: bind guest pasid and mm
> > >   * @sva_unbind_gpasid: unbind guest pasid and mm
> > > + * @pgsize_bitmap: bitmap of all possible supported page sizes
> > > + * @owner: Driver module providing these ops
> > >   */
> > >  struct iommu_ops {
> > >  	bool (*capable)(enum iommu_cap);
> > > @@ -318,6 +319,7 @@ struct iommu_ops {
> > >  	int (*sva_unbind_gpasid)(struct device *dev, int pasid);
> > >  
> > >  	unsigned long pgsize_bitmap;
> > > +	struct module *owner;
> > 
> > Everyone is always going to forget to set this field.  I don't think you
> > even set it for all of the different iommu_ops possible in this series,
> > right?
> 
> I only initialised the field for those drivers which can actually be built
> as a module, but I take your point about this being error-prone.
> 
> > The "trick" we did to keep people from having to remember this is to do
> > what we did for the bus registering functions.
> > 
> > Look at pci_register_driver in pci.h:
> > #define pci_register_driver(driver)             \
> >         __pci_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
> > 
> > Then we set the .owner field in the "real" __pci_register_driver() call.
> > 
> > Same thing for USB and lots, if not all, other driver register
> > functions.
> > 
> > You can do the same thing here, and I would recommend it.
> 
> Yes, that makes sense, cheers. Diff below. I'll send it to Joerg along
> with some other SMMU patches that have come in since the holiday.
> 
> Will
> 
> --->8
> 
> diff --git a/drivers/iommu/arm-smmu-v3.c b/drivers/iommu/arm-smmu-v3.c
> index 03dc97842875..e82997a705a8 100644
> --- a/drivers/iommu/arm-smmu-v3.c
> +++ b/drivers/iommu/arm-smmu-v3.c
> @@ -2733,7 +2733,6 @@ static struct iommu_ops arm_smmu_ops = {
>  	.get_resv_regions	= arm_smmu_get_resv_regions,
>  	.put_resv_regions	= arm_smmu_put_resv_regions,
>  	.pgsize_bitmap		= -1UL, /* Restricted during device attach */
> -	.owner			= THIS_MODULE,
>  };
>  
>  /* Probing and initialisation functions */
> diff --git a/drivers/iommu/arm-smmu.c b/drivers/iommu/arm-smmu.c
> index 5ef1f2e100d7..93d332423f6f 100644
> --- a/drivers/iommu/arm-smmu.c
> +++ b/drivers/iommu/arm-smmu.c
> @@ -1623,7 +1623,6 @@ static struct iommu_ops arm_smmu_ops = {
>  	.get_resv_regions	= arm_smmu_get_resv_regions,
>  	.put_resv_regions	= arm_smmu_put_resv_regions,
>  	.pgsize_bitmap		= -1UL, /* Restricted during device attach */
> -	.owner			= THIS_MODULE,
>  };
>  
>  static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
> diff --git a/include/linux/iommu.h b/include/linux/iommu.h
> index e9f94d3f7a04..90007c92ad2d 100644
> --- a/include/linux/iommu.h
> +++ b/include/linux/iommu.h
> @@ -388,12 +388,19 @@ void iommu_device_sysfs_remove(struct iommu_device *iommu);
>  int  iommu_device_link(struct iommu_device   *iommu, struct device *link);
>  void iommu_device_unlink(struct iommu_device *iommu, struct device *link);
>  
> -static inline void iommu_device_set_ops(struct iommu_device *iommu,
> -					const struct iommu_ops *ops)
> +static inline void __iommu_device_set_ops(struct iommu_device *iommu,
> +					  const struct iommu_ops *ops)
>  {
>  	iommu->ops = ops;
>  }
>  
> +#define iommu_device_set_ops(iommu, ops)				\
> +do {									\
> +	struct iommu_ops *__ops = (struct iommu_ops *)(ops);		\
> +	__ops->owner = THIS_MODULE;					\
> +	__iommu_device_set_ops(iommu, __ops);				\
> +} while (0)
> +
>  static inline void iommu_device_set_fwnode(struct iommu_device *iommu,
>  					   struct fwnode_handle *fwnode)
>  {

Looks good:

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

^ permalink raw reply	[flat|nested] 26+ messages in thread

end of thread, other threads:[~2020-01-09 18:21 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-19 12:03 [PATCH v4 00/16] iommu: Permit modular builds of ARM SMMU[v3] drivers Will Deacon
2019-12-19 12:03 ` [PATCH v4 01/16] drivers/iommu: Export core IOMMU API symbols to permit modular drivers Will Deacon
2019-12-19 12:03 ` [PATCH v4 02/16] iommu/of: Request ACS from the PCI core when configuring IOMMU linkage Will Deacon
2019-12-19 12:03 ` [PATCH v4 03/16] PCI/ATS: Restore EXPORT_SYMBOL_GPL() for pci_{enable,disable}_ats() Will Deacon
2019-12-20  8:43   ` Joerg Roedel
2019-12-20 15:03     ` Bjorn Helgaas
2019-12-19 12:03 ` [PATCH v4 04/16] PCI: Export pci_ats_disabled() as a GPL symbol to modules Will Deacon
2019-12-19 12:03 ` [PATCH v4 05/16] drivers/iommu: Take a ref to the IOMMU driver prior to ->add_device() Will Deacon
2019-12-19 14:44   ` Greg Kroah-Hartman
2020-01-09 14:16     ` Will Deacon
2020-01-09 18:17       ` Greg Kroah-Hartman
2019-12-19 12:03 ` [PATCH v4 06/16] iommu/of: Take a ref to the IOMMU driver during ->of_xlate() Will Deacon
2019-12-19 12:03 ` [PATCH v4 07/16] drivers/iommu: Allow IOMMU bus ops to be unregistered Will Deacon
2019-12-19 12:03 ` [PATCH v4 08/16] Revert "iommu/arm-smmu: Make arm-smmu-v3 explicitly non-modular" Will Deacon
2019-12-19 12:03 ` [PATCH v4 09/16] Revert "iommu/arm-smmu: Make arm-smmu " Will Deacon
2019-12-19 12:03 ` [PATCH v4 10/16] iommu/arm-smmu: Prevent forced unbinding of Arm SMMU drivers Will Deacon
2019-12-19 12:03 ` [PATCH v4 11/16] iommu/arm-smmu-v3: Unregister IOMMU and bus ops on device removal Will Deacon
2019-12-19 12:03 ` [PATCH v4 12/16] iommu/arm-smmu: Support SMMU module probing from the IORT Will Deacon
2019-12-19 12:03 ` [PATCH v4 13/16] iommu/arm-smmu-v3: Allow building as a module Will Deacon
2019-12-19 12:03 ` [PATCH v4 14/16] iommu/arm-smmu: Unregister IOMMU and bus ops on device removal Will Deacon
2019-12-19 12:03 ` [PATCH v4 15/16] iommu/arm-smmu: Allow building as a module Will Deacon
2019-12-19 12:03 ` [PATCH v4 16/16] iommu/arm-smmu: Update my email address in MODULE_AUTHOR() Will Deacon
2019-12-19 12:12 ` [PATCH v4 00/16] iommu: Permit modular builds of ARM SMMU[v3] drivers John Garry
2019-12-19 14:44 ` Greg Kroah-Hartman
2019-12-20 16:13 ` Joerg Roedel
2020-01-06 10:48   ` Will Deacon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).