Skip to contentSkip to conversation

iommu: Permit modular builds of ARM SMMU[v3] drivers

PATCH Applied Will Deacon 5 participants 26 messages Dec 19, 2019 – Jan 9, 2020

Subsystems (4)
lore
Share & tools
lore

Will Deacon

author maintainer
#0 lore raw

v4 cover iommu: Permit modular builds of ARM SMMU[v3] drivers

Hi all,

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

v1: https://lore.kernel.org/lkml/[email protected]/
v2: https://lore.kernel.org/lkml/[email protected]
v3: https://lore.kernel.org/lkml/[email protected]

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 <[email protected]>
Cc Jordan Crouse <[email protected]>
Cc John Garry <[email protected]>
Cc Bjorn Helgaas <[email protected]>
Cc Saravana Kannan <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc "Isaac J. Manjarres" <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Lorenzo Pieralisi <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Ard Biesheuvel <[email protected]>

--->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(-)
Signature · 1 line
2.24.1.735.g03f4e72817-goog
author maintainer
#1 lore raw

PATCH v4 1/16 drivers/iommu: Export core IOMMU API symbols to permit modular drivers

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

---
 drivers/iommu/iommu-sysfs.c | 5 +++++
 drivers/iommu/iommu.c       | 8 ++++++++
 2 files changed, 13 insertions(+)
drivers/iommu/iommu-sysfs.c +5 -0
index e436ff813e7e..99869217fbec 100644
@@ -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);
drivers/iommu/iommu.c +8 -0
index db7bfd4f2d20..7c92197d53f3 100644
@@ -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)
Signature · 6 lines
2.24.1.735.g03f4e72817-goog

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
author maintainer
#2 lore raw

PATCH v4 2/16 iommu/of: Request ACS from the PCI core when configuring IOMMU linkage

In reply to Will Deacon

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.

---
 drivers/iommu/of_iommu.c | 1 +
 1 file changed, 1 insertion(+)
drivers/iommu/of_iommu.c +1 -0
index 026ad2b29dcd..4d2f02132e7a 100644
@@ -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)) {
Signature · 6 lines
2.24.1.735.g03f4e72817-goog

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
author maintainer
#3 lore raw

PATCH v4 3/16 PCI/ATS: Restore EXPORT_SYMBOL_GPL() for pci_{enable,disable}_ats()

In reply to Will Deacon

From: Greg Kroah-Hartman <[email protected]>

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 <[email protected]>
Cc Joerg Roedel <[email protected]>
[will: rewrote commit message]
---
 drivers/pci/ats.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/pci/ats.c +2 -0
index 982b46f0a54d..dcbcf1331bb2 100644
@@ -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)
 {
Signature · 6 lines
2.24.1.735.g03f4e72817-goog

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
maintainer
#4 lore raw

Re: [PATCH v4 03/16] PCI/ATS: Restore EXPORT_SYMBOL_GPL() for pci_{enable,disable}_ats()

Hi Bjorn,

Quoted 15 lines · Will Deacon

From: Greg Kroah-Hartman <[email protected]>

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.

Quoted 8 lines
Cc Bjorn Helgaas <[email protected]>
Cc Joerg Roedel <[email protected]>
[will: rewrote commit message]
---
 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

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

Acked-by
#5 lore raw

Re: [PATCH v4 03/16] PCI/ATS: Restore EXPORT_SYMBOL_GPL() for pci_{enable,disable}_ats()

Quoted 27 lines · Joerg Roedel

Hi Bjorn,

Quoted 15 lines · Will Deacon

From: Greg Kroah-Hartman <[email protected]>

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.

Quoted 8 lines
Cc Bjorn Helgaas <[email protected]>
Cc Joerg Roedel <[email protected]>
[will: rewrote commit message]
---
 drivers/pci/ats.c | 2 ++
 1 file changed, 2 insertions(+)
Quoted 2 lines

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

Yep, thanks! You can add my

author maintainer
#6 lore raw

PATCH v4 4/16 PCI: Export pci_ats_disabled() as a GPL symbol to modules

In reply to Will Deacon

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

---
 drivers/pci/pci.c | 1 +
 1 file changed, 1 insertion(+)
drivers/pci/pci.c +1 -0
index e87196cc1a7f..ad746d903ceb 100644
@@ -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;
Signature · 6 lines
2.24.1.735.g03f4e72817-goog

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
author maintainer
#7 lore raw

PATCH v4 5/16 drivers/iommu: Take a ref to the IOMMU driver prior to ->add_device()

In reply to Will Deacon

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()'.

---
 drivers/iommu/iommu.c | 19 +++++++++++++++++--
 include/linux/iommu.h |  4 +++-
 2 files changed, 20 insertions(+), 3 deletions(-)
drivers/iommu/iommu.c +17 -2
index 7c92197d53f3..bc8edf90e729 100644
@@ -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,
include/linux/iommu.h +3 -1
index f2223cbb5fd5..e9f94d3f7a04 100644
@@ -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;
 };
 
 /**
Signature · 6 lines
2.24.1.735.g03f4e72817-goog

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#8 lore raw

Re: [PATCH v4 05/16] drivers/iommu: Take a ref to the IOMMU driver prior to ->add_device()

Quoted 76 lines · Will Deacon

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()'.

---
 drivers/iommu/iommu.c | 19 +++++++++++++++++--
 include/linux/iommu.h |  4 +++-
 2 files changed, 20 insertions(+), 3 deletions(-)
drivers/iommu/iommu.c +17 -2
index 7c92197d53f3..bc8edf90e729 100644
@@ -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,
include/linux/iommu.h +2 -1
index f2223cbb5fd5..e9f94d3f7a04 100644
@@ -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);

include/linux/iommu.h struct iommu_ops { · line 322

Quoted 4 lines
include/linux/iommu.h +1 -0
@@ -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

author maintainer
#9 lore raw

Re: [PATCH v4 05/16] drivers/iommu: Take a ref to the IOMMU driver prior to ->add_device()

Hi Greg,

Quoted 20 lines · Greg Kroah-Hartman
Quoted 19 lines · Will Deacon
include/linux/iommu.h +2 -1
index f2223cbb5fd5..e9f94d3f7a04 100644
@@ -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);
 
Quoted 6 lines

include/linux/iommu.h struct iommu_ops { · line 322

Quoted 2 lines
include/linux/iommu.h +1 -0
 	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.

Quoted 7 lines

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)

Quoted 6 lines

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

drivers/iommu/arm-smmu-v3.c +0 -1
index 03dc97842875..e82997a705a8 100644
@@ -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 */
drivers/iommu/arm-smmu.c +0 -1
index 5ef1f2e100d7..93d332423f6f 100644
@@ -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)
include/linux/iommu.h +9 -2
index e9f94d3f7a04..90007c92ad2d 100644
@@ -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)
 {
Reviewed-by
#10 lore raw

Re: [PATCH v4 05/16] drivers/iommu: Take a ref to the IOMMU driver prior to ->add_device()

Quoted 94 lines · Will Deacon

Hi Greg,

Quoted 20 lines · Greg Kroah-Hartman
Quoted 19 lines · Will Deacon
include/linux/iommu.h +2 -1
index f2223cbb5fd5..e9f94d3f7a04 100644
@@ -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);
 
Quoted 6 lines

include/linux/iommu.h struct iommu_ops { · line 322

Quoted 2 lines
include/linux/iommu.h +1 -0
 	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.

Quoted 7 lines

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)

Quoted 6 lines

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

drivers/iommu/arm-smmu-v3.c +0 -1
index 03dc97842875..e82997a705a8 100644
@@ -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 */
drivers/iommu/arm-smmu.c +0 -1
index 5ef1f2e100d7..93d332423f6f 100644
@@ -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)
include/linux/iommu.h +2 -2
index e9f94d3f7a04..90007c92ad2d 100644
@@ -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;
 }
 

include/linux/iommu.h void iommu_device_sysfs_remove(struct iommu_device *iommu); · line 406

Quoted 10 lines
include/linux/iommu.h +7 -0
+#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:

author maintainer
#11 lore raw

PATCH v4 6/16 iommu/of: Take a ref to the IOMMU driver during ->of_xlate()

In reply to Will Deacon

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

---
 drivers/iommu/of_iommu.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
drivers/iommu/of_iommu.c +12 -6
index 4d2f02132e7a..e7bc8b721947 100644
@@ -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 {
Signature · 6 lines
2.24.1.735.g03f4e72817-goog

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
author maintainer
#12 lore raw

PATCH v4 7/16 drivers/iommu: Allow IOMMU bus ops to be unregistered

In reply to Will Deacon

'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.

---
 drivers/iommu/iommu.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/iommu/iommu.c +5 -0
index bc8edf90e729..433101f0853c 100644
@@ -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;
 
Signature · 6 lines
2.24.1.735.g03f4e72817-goog

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
author maintainer
#13 lore raw

PATCH v4 8/16 Revert "iommu/arm-smmu: Make arm-smmu-v3 explicitly non-modular"

In reply to Will Deacon

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.

---
 drivers/iommu/arm-smmu-v3.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
drivers/iommu/arm-smmu-v3.c +16 -9
index effe72eb89e7..68163b1d680d 100644
@@ -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 <[email protected]>");
+MODULE_LICENSE("GPL v2");
Signature · 6 lines
2.24.1.735.g03f4e72817-goog

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
author maintainer
#14 lore raw

PATCH v4 9/16 Revert "iommu/arm-smmu: Make arm-smmu explicitly non-modular"

In reply to Will Deacon

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.

---
 drivers/iommu/arm-smmu.c | 32 +++++++++++++++++++-------------
 1 file changed, 19 insertions(+), 13 deletions(-)
drivers/iommu/arm-smmu.c +19 -13
index 4f1a350d9529..7c768f771001 100644
@@ -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 <[email protected]>");
+MODULE_LICENSE("GPL v2");
Signature · 6 lines
2.24.1.735.g03f4e72817-goog

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
author maintainer
#15 lore raw

PATCH v4 10/16 iommu/arm-smmu: Prevent forced unbinding of Arm SMMU drivers

In reply to Will Deacon

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.

---
 drivers/iommu/arm-smmu-v3.c | 5 +++--
 drivers/iommu/arm-smmu.c    | 7 ++++---
 2 files changed, 7 insertions(+), 5 deletions(-)
drivers/iommu/arm-smmu-v3.c +3 -2
index 68163b1d680d..9d4c9de6172d 100644
@@ -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,
drivers/iommu/arm-smmu.c +4 -3
index 7c768f771001..5cbee88a3b83 100644
@@ -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,
Signature · 6 lines
2.24.1.735.g03f4e72817-goog

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
author maintainer
#16 lore raw

PATCH v4 11/16 iommu/arm-smmu-v3: Unregister IOMMU and bus ops on device removal

In reply to Will Deacon

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.

---
 drivers/iommu/arm-smmu-v3.c | 64 +++++++++++++++++++++++++------------
 1 file changed, 43 insertions(+), 21 deletions(-)
drivers/iommu/arm-smmu-v3.c +43 -21
index 9d4c9de6172d..46160a2fec3e 100644
@@ -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;
Signature · 6 lines
2.24.1.735.g03f4e72817-goog

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
author maintainer
#17 lore raw

PATCH v4 12/16 iommu/arm-smmu: Support SMMU module probing from the IORT

In reply to Will Deacon

From: Ard Biesheuvel <[email protected]>

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.

---
 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(-)
drivers/acpi/arm64/iort.c +2 -2
index 33f71983e001..4a560fdf7386 100644
@@ -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;
drivers/iommu/arm-smmu-v3.c +1 -0
index 46160a2fec3e..da9474a02668 100644
@@ -3734,4 +3734,5 @@ module_platform_driver(arm_smmu_driver);
 
 MODULE_DESCRIPTION("IOMMU API for ARM architected SMMUv3 implementations");
 MODULE_AUTHOR("Will Deacon <[email protected]>");
+MODULE_ALIAS("platform:arm-smmu-v3");
 MODULE_LICENSE("GPL v2");
drivers/iommu/arm-smmu.c +1 -0
index 5cbee88a3b83..5d2f60bb9e50 100644
@@ -2281,4 +2281,5 @@ module_platform_driver(arm_smmu_driver);
 
 MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
 MODULE_AUTHOR("Will Deacon <[email protected]>");
+MODULE_ALIAS("platform:arm-smmu");
 MODULE_LICENSE("GPL v2");
Signature · 6 lines
2.24.1.735.g03f4e72817-goog

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
author maintainer
#18 lore raw

PATCH v4 13/16 iommu/arm-smmu-v3: Allow building as a module

In reply to Will Deacon

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

---
 drivers/iommu/Kconfig       | 2 +-
 drivers/iommu/arm-smmu-v3.c | 3 +--
 2 files changed, 2 insertions(+), 3 deletions(-)
drivers/iommu/Kconfig +1 -1
index 0b9d78a0f3ac..31f0f376ad11 100644
@@ -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
drivers/iommu/arm-smmu-v3.c +1 -2
index da9474a02668..2a65d9b87125 100644
@@ -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;
Signature · 6 lines
2.24.1.735.g03f4e72817-goog

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
author maintainer
#19 lore raw

PATCH v4 14/16 iommu/arm-smmu: Unregister IOMMU and bus ops on device removal

In reply to Will Deacon

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.

---
 drivers/iommu/arm-smmu.c | 50 ++++++++++++++++++++++++++++++++--------
 1 file changed, 40 insertions(+), 10 deletions(-)
drivers/iommu/arm-smmu.c +40 -10
index 5d2f60bb9e50..1f0c09bf112a 100644
@@ -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);
Signature · 1 line
2.24.1.735.g03f4e72817-goog
author maintainer
#20 lore raw

PATCH v4 15/16 iommu/arm-smmu: Allow building as a module

In reply to Will Deacon

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.

---
 drivers/iommu/Kconfig    | 14 ++++++++++-
 drivers/iommu/Makefile   |  3 ++-
 drivers/iommu/arm-smmu.c | 52 ++++++++++++++++++++++++----------------
 3 files changed, 47 insertions(+), 22 deletions(-)
drivers/iommu/Kconfig +13 -1
index 31f0f376ad11..d66ace717cf4 100644
@@ -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
drivers/iommu/Makefile +2 -1
index 97814cc861ea..2104fb8afc06 100644
@@ -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
drivers/iommu/arm-smmu.c +32 -20

94 lines not on this page · raw message

Signature · 6 lines
2.24.1.735.g03f4e72817-goog

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
author maintainer
#21 lore raw

PATCH v4 16/16 iommu/arm-smmu: Update my email address in MODULE_AUTHOR()

In reply to Will Deacon

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

---
 drivers/iommu/arm-smmu-v3.c | 2 +-
 drivers/iommu/arm-smmu.c    | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/iommu/arm-smmu-v3.c +1 -1
index 2a65d9b87125..03dc97842875 100644
@@ -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 <[email protected]>");
+MODULE_AUTHOR("Will Deacon <[email protected]>");
 MODULE_ALIAS("platform:arm-smmu-v3");
 MODULE_LICENSE("GPL v2");
drivers/iommu/arm-smmu.c +1 -1
index 3cfa138fbd4f..5ef1f2e100d7 100644
@@ -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 <[email protected]>");
+MODULE_AUTHOR("Will Deacon <[email protected]>");
 MODULE_ALIAS("platform:arm-smmu");
 MODULE_LICENSE("GPL v2");
Signature · 6 lines
2.24.1.735.g03f4e72817-goog

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Tested-by on the v4 series
#22 lore raw
In reply to Will Deacon
Quoted 8 lines · Will Deacon

Hi all,

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

   v1: https://lore.kernel.org/lkml/[email protected]/
   v2: https://lore.kernel.org/lkml/[email protected]
   v3: https://lore.kernel.org/lkml/[email protected]
Quoted 6 lines

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:

[0] https://lore.kernel.org/linux-iommu/[email protected]/

Quoted 56 lines

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

Cheers,

Will

Cc Jean-Philippe Brucker <[email protected]>
Cc Jordan Crouse <[email protected]>
Cc John Garry <[email protected]>
Cc Bjorn Helgaas <[email protected]>
Cc Saravana Kannan <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc "Isaac J. Manjarres" <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Lorenzo Pieralisi <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Ard Biesheuvel <[email protected]>

--->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(-)
Reviewed-by on the v4 series
#23 lore raw
In reply to Will Deacon
Quoted 8 lines · Will Deacon

Hi all,

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

v1: https://lore.kernel.org/lkml/[email protected]/
v2: https://lore.kernel.org/lkml/[email protected]
v3: https://lore.kernel.org/lkml/[email protected]

Quoted 8 lines

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!

maintainer Applied
#24 lore raw
In reply to Will Deacon
Quoted 22 lines · Will Deacon

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()

Quoted 12 lines
 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.

author maintainer
#25 lore raw
Quoted 36 lines · Joerg Roedel
Quoted 22 lines · Will Deacon

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()

Quoted 12 lines
 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(-)
Quoted 1 line

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