Skip to contentSkip to conversation

iommu: Refactoring domain allocation interface

PATCH Applied Lu Baolu 2 participants 6 messages Oct 9, 2024 – Oct 29, 2024

Subsystems (10)
lore
Share & tools
lore

Lu Baolu

author
#0 lore raw

v4 cover iommu: Refactoring domain allocation interface

The IOMMU subsystem has undergone some changes, including the removal of iommu_ops from the bus structure. Consequently, the existing domain allocation interface, which relies on a bus type argument, is no longer relevant:

struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)

This series is designed to refactor the use of this interface. It proposes a new interface iommu_paging_domain_alloc() to replace iommu_domain_alloc(). This interface is for allocating iommu paging domains for kernel drivers. It takes a device pointer as a parameter, which better reflects the current design of the IOMMU subsystem.

The majority of device drivers currently using iommu_domain_alloc() do so to allocate a domain for a specific device and then attach that domain to the device. These cases can be straightforwardly migrated to the new interface.

The drm/tegra driver is a bit different in that the device pointer passed to the helper, which allocates the iommu domain, is not the one that will be used for the kernel DMA API. Move the existing logic in iommu_domain_alloc() into the driver to ensure it works as intended.

Now that all consumers of iommu_domain_alloc() have switched to the new interface, we can finally remove iommu_domain_alloc(). This removal paves the way for the IOMMU subsystem to support multiple iommu drivers. Additionally, the individual iommu driver implementation for domain allocation could also be simplified, as there will always be a valid device pointer passed along the path.

Change log:

v4:
- 4 patches remained in this series. These patches have been reviewed and ack'ed but missed the merge window of v6.12-rc1. I resent them in a series to make it go through the iommu tree.

v3: https://lore.kernel.org/linux-iommu/[email protected]/
- Remove the proposed iommu_user_domain_alloc() interface.
- Most of the patches have been merged through the subsystem trees.

v2: https://lore.kernel.org/linux-iommu/[email protected]/
- Drop the vt-d patches which implement paging domain support from this series. I will post them in a separate series later.
- Convert all drivers that call iommu_domain_alloc() to use the new interface and remove iommu_domain_alloc() from the tree.
- For the drm/msm driver, make the code compatible with the no-IOMMU
case.
- Various cleanups and refinements.

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

Lu Baolu (4):
remoteproc: Use iommu_paging_domain_alloc()
media: nvidia: tegra: Use iommu_paging_domain_alloc() drm/nouveau/tegra: Use iommu_paging_domain_alloc()
iommu: Remove iommu_domain_alloc()

 include/linux/iommu.h                         |  6 ----
 .../drm/nouveau/nvkm/engine/device/tegra.c    |  4 +--
 drivers/iommu/iommu.c                         | 36 -------------------
 .../media/platform/nvidia/tegra-vde/iommu.c   |  7 ++--
 drivers/remoteproc/remoteproc_core.c          |  6 ++--
 5 files changed, 9 insertions(+), 50 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v4 1/4 remoteproc: Use iommu_paging_domain_alloc()

An iommu domain is allocated in rproc_enable_iommu() and is attached to rproc->dev.parent in the same function.

Use iommu_paging_domain_alloc() to make it explicit.

---
 drivers/remoteproc/remoteproc_core.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/remoteproc/remoteproc_core.c +3 -3
index f276956f2c5c..eb66f78ec8b7 100644
@@ -109,10 +109,10 @@ static int rproc_enable_iommu(struct rproc *rproc)
 		return 0;
 	}
 
-	domain = iommu_domain_alloc(dev->bus);
-	if (!domain) {
+	domain = iommu_paging_domain_alloc(dev);
+	if (IS_ERR(domain)) {
 		dev_err(dev, "can't alloc iommu domain\n");
-		return -ENOMEM;
+		return PTR_ERR(domain);
 	}
 
 	iommu_set_fault_handler(domain, rproc_iommu_fault, rproc);
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH v4 2/4 media: nvidia: tegra: Use iommu_paging_domain_alloc()

In reply to Lu Baolu

An iommu domain is allocated in tegra_vde_iommu_init() and is attached to vde->dev. Use iommu_paging_domain_alloc() to make it explicit.

---
 drivers/media/platform/nvidia/tegra-vde/iommu.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/media/platform/nvidia/tegra-vde/iommu.c +4 -3
index 5521ed3e465f..b1d9d841d944 100644
@@ -78,9 +78,10 @@ int tegra_vde_iommu_init(struct tegra_vde *vde)
 		arm_iommu_release_mapping(mapping);
 	}
 #endif
-	vde->domain = iommu_domain_alloc(&platform_bus_type);
-	if (!vde->domain) {
-		err = -ENOMEM;
+	vde->domain = iommu_paging_domain_alloc(dev);
+	if (IS_ERR(vde->domain)) {
+		err = PTR_ERR(vde->domain);
+		vde->domain = NULL;
 		goto put_group;
 	}
 
Signature · 1 line
2.43.0
author
#3 lore raw

PATCH v4 3/4 drm/nouveau/tegra: Use iommu_paging_domain_alloc()

In reply to Lu Baolu

In nvkm_device_tegra_probe_iommu(), a paging domain is allocated for @dev and attached to it on success. Use iommu_paging_domain_alloc() to make it explicit.

---
 drivers/gpu/drm/nouveau/nvkm/engine/device/tegra.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/nouveau/nvkm/engine/device/tegra.c +2 -2
index d1c294f00665..78a83f904bbd 100644
@@ -120,8 +120,8 @@ nvkm_device_tegra_probe_iommu(struct nvkm_device_tegra *tdev)
 	mutex_init(&tdev->iommu.mutex);
 
 	if (device_iommu_mapped(dev)) {
-		tdev->iommu.domain = iommu_domain_alloc(&platform_bus_type);
-		if (!tdev->iommu.domain)
+		tdev->iommu.domain = iommu_paging_domain_alloc(dev);
+		if (IS_ERR(tdev->iommu.domain))
 			goto error;
 
 		/*
Signature · 1 line
2.43.0
author
#4 lore raw

PATCH v4 4/4 iommu: Remove iommu_domain_alloc()

In reply to Lu Baolu

The iommu_domain_alloc() interface is no longer used in the tree anymore. Remove it to avoid dead code.

There is increasing demand for supporting multiple IOMMU drivers, and this is the last bus-based thing standing in the way of that.

---
 include/linux/iommu.h |  6 ------
 drivers/iommu/iommu.c | 36 ------------------------------------
 2 files changed, 42 deletions(-)
include/linux/iommu.h +0 -6
index bd722f473635..6d809f6e6c8d 100644
@@ -788,7 +788,6 @@ extern int bus_iommu_probe(const struct bus_type *bus);
 extern bool iommu_present(const struct bus_type *bus);
 extern bool device_iommu_capable(struct device *dev, enum iommu_cap cap);
 extern bool iommu_group_has_isolated_msi(struct iommu_group *group);
-extern struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus);
 struct iommu_domain *iommu_paging_domain_alloc(struct device *dev);
 extern void iommu_domain_free(struct iommu_domain *domain);
 extern int iommu_attach_device(struct iommu_domain *domain,
@@ -1091,11 +1090,6 @@ static inline bool device_iommu_capable(struct device *dev, enum iommu_cap cap)
 	return false;
 }
 
-static inline struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus)
-{
-	return NULL;
-}
-
 static inline struct iommu_domain *iommu_paging_domain_alloc(struct device *dev)
 {
 	return ERR_PTR(-ENODEV);
drivers/iommu/iommu.c +0 -36
index 83c8e617a2c5..521471706400 100644
@@ -1994,42 +1994,6 @@ __iommu_group_domain_alloc(struct iommu_group *group, unsigned int type)
 	return __iommu_domain_alloc(dev_iommu_ops(dev), dev, type);
 }
 
-static int __iommu_domain_alloc_dev(struct device *dev, void *data)
-{
-	const struct iommu_ops **ops = data;
-
-	if (!dev_has_iommu(dev))
-		return 0;
-
-	if (WARN_ONCE(*ops && *ops != dev_iommu_ops(dev),
-		      "Multiple IOMMU drivers present for bus %s, which the public IOMMU API can't fully support yet. You will still need to disable one or more for this to work, sorry!\n",
-		      dev_bus_name(dev)))
-		return -EBUSY;
-
-	*ops = dev_iommu_ops(dev);
-	return 0;
-}
-
-/*
- * The iommu ops in bus has been retired. Do not use this interface in
- * new drivers.
- */
-struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus)
-{
-	const struct iommu_ops *ops = NULL;
-	int err = bus_for_each_dev(bus, NULL, &ops, __iommu_domain_alloc_dev);
-	struct iommu_domain *domain;
-
-	if (err || !ops)
-		return NULL;
-
-	domain = __iommu_domain_alloc(ops, NULL, IOMMU_DOMAIN_UNMANAGED);
-	if (IS_ERR(domain))
-		return NULL;
-	return domain;
-}
-EXPORT_SYMBOL_GPL(iommu_domain_alloc);
-
 /**
  * iommu_paging_domain_alloc() - Allocate a paging domain
  * @dev: device for which the domain is allocated
Signature · 1 line
2.43.0
maintainer Applied
#5 lore raw
In reply to Lu Baolu
Quoted 6 lines · Lu Baolu

Lu Baolu (4):
remoteproc: Use iommu_paging_domain_alloc()
media: nvidia: tegra: Use iommu_paging_domain_alloc() drm/nouveau/tegra: Use iommu_paging_domain_alloc()
iommu: Remove iommu_domain_alloc()

Quoted 6 lines
 include/linux/iommu.h                         |  6 ----
 .../drm/nouveau/nvkm/engine/device/tegra.c    |  4 +--
 drivers/iommu/iommu.c                         | 36 -------------------
 .../media/platform/nvidia/tegra-vde/iommu.c   |  7 ++--
 drivers/remoteproc/remoteproc_core.c          |  6 ++--
 5 files changed, 9 insertions(+), 50 deletions(-)

Applied, thanks.