Skip to contentSkip to conversation

iommu: Isolate iova_cookie to actual owners

PATCH In review Nicolin Chen 4 participants 14 messages Feb 28, 2025

Subsystems (3)
lore
Share & tools
lore
#7 lore raw

PATCH v2 2/4 iommufd: Move iommufd_sw_msi and related functions to driver.c

In reply to Nicolin Chen

To provide the iommufd_sw_msi() to the iommu core that is under a different Kconfig, move it and its related functions to driver.c. Then, stub it into the public iommufd header. iommufd_sw_msi_install() continues to be used by iommufd internal, so put it in the private header.

---
 drivers/iommu/iommufd/iommufd_private.h |   5 +-
 include/linux/iommufd.h                 |   9 ++
 drivers/iommu/iommufd/device.c          | 122 -----------------------
 drivers/iommu/iommufd/driver.c          | 124 ++++++++++++++++++++++++
 4 files changed, 136 insertions(+), 124 deletions(-)
drivers/iommu/iommufd/iommufd_private.h +3 -2
index 246297452a44..51da18672c74 100644
@@ -32,8 +32,9 @@ struct iommufd_sw_msi_maps {
 	DECLARE_BITMAP(bitmap, 64);
 };
 
-int iommufd_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
-		   phys_addr_t msi_addr);
+int iommufd_sw_msi_install(struct iommufd_ctx *ictx,
+			   struct iommufd_hwpt_paging *hwpt_paging,
+			   struct iommufd_sw_msi_map *msi_map);
 
 struct iommufd_ctx {
 	struct file *file;
include/linux/iommufd.h +9 -0
index 11110c749200..2f272863a207 100644
@@ -8,6 +8,7 @@
 
 #include <linux/err.h>
 #include <linux/errno.h>
+#include <linux/msi.h>
 #include <linux/refcount.h>
 #include <linux/types.h>
 #include <linux/xarray.h>
@@ -187,6 +188,8 @@ struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx,
 					     enum iommufd_object_type type);
 struct device *iommufd_viommu_find_dev(struct iommufd_viommu *viommu,
 				       unsigned long vdev_id);
+int iommufd_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
+		   phys_addr_t msi_addr);
 #else /* !CONFIG_IOMMUFD_DRIVER_CORE */
 static inline struct iommufd_object *
 _iommufd_object_alloc(struct iommufd_ctx *ictx, size_t size,
@@ -200,6 +203,12 @@ iommufd_viommu_find_dev(struct iommufd_viommu *viommu, unsigned long vdev_id)
 {
 	return NULL;
 }
+
+static inline int iommufd_sw_msi(struct iommu_domain *domain,
+				 struct msi_desc *desc, phys_addr_t msi_addr)
+{
+	return -EOPNOTSUPP;
+}
 #endif /* CONFIG_IOMMUFD_DRIVER_CORE */
 
 /*
drivers/iommu/iommufd/device.c +0 -122

128 lines not on this page · raw message

drivers/iommu/iommufd/driver.c +124 -0

129 lines not on this page · raw message

Signature · 1 line
2.43.0
#0 lore raw

v2 cover iommu: Isolate iova_cookie to actual owners

Now, iommufd implements its own sw_msi function that does not touch the domain->iova_cookie but domain->iommufd_hwpt, as a domain owner pointer.

Isolate the iova_cookie from iommufd by putting it into the union where the iommufd_hwpt is located.

This requires a set of preparations for iommu core to know what module owns the domain private data. Since there are only two implementations of the sw_msi function pointer, take an easier approach by calling the two implementations directly according to a new private_data_owner tag.

This is a clean-up series for the sw_msi Part-1 core series, prior to the Part-2/3 series. It's on github:
https://github.com/nicolinc/iommufd/commits/iommufd_msi_cleanup-v2

Changelog
v2
* Drop sw_msi function pointer
* Add a new private_data_owner tag in iommu_domain
* Let iommu core call the two sw_msi implementations directly
v1
https://lore.kernel.org/all/[email protected]/

Thanks
Nicolin

Nicolin Chen (4):
iommu: Add private_data_owner to iommu_domain
iommufd: Move iommufd_sw_msi and related functions to driver.c
iommu: Drop sw_msi from iommu_domain
iommu: Turn iova_cookie to dma-iommu private pointer

 drivers/iommu/dma-iommu.h               |   9 ++
 drivers/iommu/iommufd/iommufd_private.h |   5 +-
 include/linux/iommu.h                   |  24 ++---
 include/linux/iommufd.h                 |   9 ++
 drivers/iommu/dma-iommu.c               |  18 ++--
 drivers/iommu/iommu.c                   |  16 ++-
 drivers/iommu/iommufd/device.c          | 122 -----------------------
 drivers/iommu/iommufd/driver.c          | 124 ++++++++++++++++++++++++
 drivers/iommu/iommufd/hw_pagetable.c    |   6 +-
 9 files changed, 176 insertions(+), 157 deletions(-)

base-commit: 598749522d4254afb33b8a6c1bea614a95896868

Signature · 1 line
2.43.0
#12 lore raw

PATCH v2 3/4 iommu: Drop sw_msi from iommu_domain

There are only two sw_msi implementations in the entire system, thus it's not very necessary to have an sw_msi pointer.

Instead, check domain->private_data_owner to call the two implementations directly from the core code.

---
 drivers/iommu/dma-iommu.h            |  9 +++++++++
 include/linux/iommu.h                | 15 ---------------
 drivers/iommu/dma-iommu.c            | 13 +++----------
 drivers/iommu/iommu.c                | 16 ++++++++++++++--
 drivers/iommu/iommufd/hw_pagetable.c |  3 ---
 5 files changed, 26 insertions(+), 30 deletions(-)
drivers/iommu/dma-iommu.h +9 -0
index c12d63457c76..97f1da1efbb4 100644
@@ -18,6 +18,9 @@ int iommu_dma_init_fq(struct iommu_domain *domain);
 
 void iommu_dma_get_resv_regions(struct device *dev, struct list_head *list);
 
+int iommu_dma_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
+		     phys_addr_t msi_addr);
+
 extern bool iommu_dma_forcedac;
 
 #else /* CONFIG_IOMMU_DMA */
@@ -44,5 +47,11 @@ static inline void iommu_dma_get_resv_regions(struct device *dev, struct list_he
 {
 }
 
+static inline int iommu_dma_sw_msi(struct iommu_domain *domain,
+				   struct msi_desc *desc, phys_addr_t msi_addr)
+{
+	return -ENODEV;
+}
+
 #endif	/* CONFIG_IOMMU_DMA */
 #endif	/* __DMA_IOMMU_H */
include/linux/iommu.h +0 -15
index 4f2774c08262..29400060d648 100644
@@ -224,11 +224,6 @@ struct iommu_domain {
 	struct iommu_dma_cookie *iova_cookie;
 	int (*iopf_handler)(struct iopf_group *group);
 
-#if IS_ENABLED(CONFIG_IRQ_MSI_IOMMU)
-	int (*sw_msi)(struct iommu_domain *domain, struct msi_desc *desc,
-		      phys_addr_t msi_addr);
-#endif
-
 	union { /* Pointer usable by owner of the domain */
 		struct iommufd_hw_pagetable *iommufd_hwpt; /* iommufd */
 	};
@@ -249,16 +244,6 @@ struct iommu_domain {
 	};
 };
 
-static inline void iommu_domain_set_sw_msi(
-	struct iommu_domain *domain,
-	int (*sw_msi)(struct iommu_domain *domain, struct msi_desc *desc,
-		      phys_addr_t msi_addr))
-{
-#if IS_ENABLED(CONFIG_IRQ_MSI_IOMMU)
-	domain->sw_msi = sw_msi;
-#endif
-}
-
 static inline bool iommu_is_dma_domain(struct iommu_domain *domain)
 {
 	return domain->type & __IOMMU_DOMAIN_DMA_API;
drivers/iommu/dma-iommu.c +3 -10
index 78915d74e8fa..7ee71b9c53bd 100644
@@ -103,9 +103,6 @@ static int __init iommu_dma_forcedac_setup(char *str)
 }
 early_param("iommu.forcedac", iommu_dma_forcedac_setup);
 
-static int iommu_dma_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
-			    phys_addr_t msi_addr);
-
 /* Number of entries per flush queue */
 #define IOVA_DEFAULT_FQ_SIZE	256
 #define IOVA_SINGLE_FQ_SIZE	32768
@@ -402,7 +399,6 @@ int iommu_get_dma_cookie(struct iommu_domain *domain)
 		return -ENOMEM;
 
 	mutex_init(&domain->iova_cookie->mutex);
-	iommu_domain_set_sw_msi(domain, iommu_dma_sw_msi);
 	domain->private_data_owner = IOMMU_DOMAIN_DATA_OWNER_DMA;
 	return 0;
 }
@@ -435,7 +431,6 @@ int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base)
 
 	cookie->msi_iova = base;
 	domain->iova_cookie = cookie;
-	iommu_domain_set_sw_msi(domain, iommu_dma_sw_msi);
 	domain->private_data_owner = IOMMU_DOMAIN_DATA_OWNER_DMA;
 	return 0;
 }
@@ -451,10 +446,8 @@ void iommu_put_dma_cookie(struct iommu_domain *domain)
 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
 	struct iommu_dma_msi_page *msi, *tmp;
 
-#if IS_ENABLED(CONFIG_IRQ_MSI_IOMMU)
-	if (domain->sw_msi != iommu_dma_sw_msi)
+	if (domain->private_data_owner != IOMMU_DOMAIN_DATA_OWNER_DMA)
 		return;
-#endif
 
 	if (!cookie)
 		return;
@@ -1813,8 +1806,8 @@ static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
 	return NULL;
 }
 
-static int iommu_dma_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
-			    phys_addr_t msi_addr)
+int iommu_dma_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
+		     phys_addr_t msi_addr)
 {
 	struct device *dev = msi_desc_to_dev(desc);
 	const struct iommu_dma_msi_page *msi_page;
drivers/iommu/iommu.c +14 -2
index 022bf96a18c5..462d7bc0d47a 100644
@@ -18,6 +18,7 @@
 #include <linux/errno.h>
 #include <linux/host1x_context_bus.h>
 #include <linux/iommu.h>
+#include <linux/iommufd.h>
 #include <linux/idr.h>
 #include <linux/err.h>
 #include <linux/pci.h>
@@ -3619,8 +3620,19 @@ int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr)
 		return 0;
 
 	mutex_lock(&group->mutex);
-	if (group->domain && group->domain->sw_msi)
-		ret = group->domain->sw_msi(group->domain, desc, msi_addr);
+	if (group->domain) {
+		switch (group->domain->private_data_owner) {
+		case IOMMU_DOMAIN_DATA_OWNER_DMA:
+			ret = iommu_dma_sw_msi(group->domain, desc, msi_addr);
+			break;
+		case IOMMU_DOMAIN_DATA_OWNER_IOMMUFD:
+			ret = iommufd_sw_msi(group->domain, desc, msi_addr);
+			break;
+		default:
+			ret = -EOPNOTSUPP;
+			break;
+		}
+	}
 	mutex_unlock(&group->mutex);
 	return ret;
 }
drivers/iommu/iommufd/hw_pagetable.c +0 -3
index 5640444de475..ba46f9c0a81f 100644
@@ -156,7 +156,6 @@ iommufd_hwpt_paging_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
 			goto out_abort;
 		}
 	}
-	iommu_domain_set_sw_msi(hwpt->domain, iommufd_sw_msi);
 	hwpt->domain->private_data_owner = IOMMU_DOMAIN_DATA_OWNER_IOMMUFD;
 
 	/*
@@ -253,7 +252,6 @@ iommufd_hwpt_nested_alloc(struct iommufd_ctx *ictx,
 		goto out_abort;
 	}
 	hwpt->domain->owner = ops;
-	iommu_domain_set_sw_msi(hwpt->domain, iommufd_sw_msi);
 	hwpt->domain->private_data_owner = IOMMU_DOMAIN_DATA_OWNER_IOMMUFD;
 
 	if (WARN_ON_ONCE(hwpt->domain->type != IOMMU_DOMAIN_NESTED)) {
@@ -311,7 +309,6 @@ iommufd_viommu_alloc_hwpt_nested(struct iommufd_viommu *viommu, u32 flags,
 		goto out_abort;
 	}
 	hwpt->domain->owner = viommu->iommu_dev->ops;
-	iommu_domain_set_sw_msi(hwpt->domain, iommufd_sw_msi);
 	hwpt->domain->private_data_owner = IOMMU_DOMAIN_DATA_OWNER_IOMMUFD;
 
 	if (WARN_ON_ONCE(hwpt->domain->type != IOMMU_DOMAIN_NESTED)) {
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/4 iommu: Add private_data_owner to iommu_domain

In reply to Nicolin Chen

Steal two bits from the 32-bit "type" in struct iommu_domain to store a new tag for private data owned by either dma-iommu or iommufd.

Set the domain->private_data_owner in dma-iommu and iommufd. These will be used to replace the sw_msi function pointer.

---
 include/linux/iommu.h                | 7 ++++++-
 drivers/iommu/dma-iommu.c            | 2 ++
 drivers/iommu/iommufd/hw_pagetable.c | 3 +++
 3 files changed, 11 insertions(+), 1 deletion(-)
include/linux/iommu.h +6 -1
index e93d2e918599..4f2774c08262 100644
@@ -209,8 +209,13 @@ struct iommu_domain_geometry {
 #define IOMMU_DOMAIN_PLATFORM	(__IOMMU_DOMAIN_PLATFORM)
 #define IOMMU_DOMAIN_NESTED	(__IOMMU_DOMAIN_NESTED)
 
+#define IOMMU_DOMAIN_DATA_OWNER_NONE (0U)
+#define IOMMU_DOMAIN_DATA_OWNER_DMA (1U)
+#define IOMMU_DOMAIN_DATA_OWNER_IOMMUFD (2U)
+
 struct iommu_domain {
-	unsigned type;
+	u32 type : 30;
+	u32 private_data_owner : 2;
 	const struct iommu_domain_ops *ops;
 	const struct iommu_dirty_ops *dirty_ops;
 	const struct iommu_ops *owner; /* Whose domain_alloc we came from */
drivers/iommu/dma-iommu.c +2 -0
index 94263ed2c564..78915d74e8fa 100644
@@ -403,6 +403,7 @@ int iommu_get_dma_cookie(struct iommu_domain *domain)
 
 	mutex_init(&domain->iova_cookie->mutex);
 	iommu_domain_set_sw_msi(domain, iommu_dma_sw_msi);
+	domain->private_data_owner = IOMMU_DOMAIN_DATA_OWNER_DMA;
 	return 0;
 }
 
@@ -435,6 +436,7 @@ int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base)
 	cookie->msi_iova = base;
 	domain->iova_cookie = cookie;
 	iommu_domain_set_sw_msi(domain, iommu_dma_sw_msi);
+	domain->private_data_owner = IOMMU_DOMAIN_DATA_OWNER_DMA;
 	return 0;
 }
 EXPORT_SYMBOL(iommu_get_msi_cookie);
drivers/iommu/iommufd/hw_pagetable.c +3 -0
index 7de6e914232e..5640444de475 100644
@@ -157,6 +157,7 @@ iommufd_hwpt_paging_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
 		}
 	}
 	iommu_domain_set_sw_msi(hwpt->domain, iommufd_sw_msi);
+	hwpt->domain->private_data_owner = IOMMU_DOMAIN_DATA_OWNER_IOMMUFD;
 
 	/*
 	 * Set the coherency mode before we do iopt_table_add_domain() as some
@@ -253,6 +254,7 @@ iommufd_hwpt_nested_alloc(struct iommufd_ctx *ictx,
 	}
 	hwpt->domain->owner = ops;
 	iommu_domain_set_sw_msi(hwpt->domain, iommufd_sw_msi);
+	hwpt->domain->private_data_owner = IOMMU_DOMAIN_DATA_OWNER_IOMMUFD;
 
 	if (WARN_ON_ONCE(hwpt->domain->type != IOMMU_DOMAIN_NESTED)) {
 		rc = -EINVAL;
@@ -310,6 +312,7 @@ iommufd_viommu_alloc_hwpt_nested(struct iommufd_viommu *viommu, u32 flags,
 	}
 	hwpt->domain->owner = viommu->iommu_dev->ops;
 	iommu_domain_set_sw_msi(hwpt->domain, iommufd_sw_msi);
+	hwpt->domain->private_data_owner = IOMMU_DOMAIN_DATA_OWNER_IOMMUFD;
 
 	if (WARN_ON_ONCE(hwpt->domain->type != IOMMU_DOMAIN_NESTED)) {
 		rc = -EINVAL;
Signature · 1 line
2.43.0
#13 lore raw

PATCH v2 4/4 iommu: Turn iova_cookie to dma-iommu private pointer

In reply to Nicolin Chen

Now that iommufd does not rely on dma-iommu.c for any purpose. We can combine the dma-iommu.c iova_cookie and the iommufd_hwpt under the same union. This union is effectively 'owner data' and can be used by the entity that allocated the domain. Note that legacy vfio type1 flows continue to use dma-iommu.c for sw_msi and still need iova_cookie.

---
 include/linux/iommu.h     | 2 +-
 drivers/iommu/dma-iommu.c | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)
include/linux/iommu.h +1 -1
index 29400060d648..5c6bca1ace27 100644
@@ -221,10 +221,10 @@ struct iommu_domain {
 	const struct iommu_ops *owner; /* Whose domain_alloc we came from */
 	unsigned long pgsize_bitmap;	/* Bitmap of page sizes in use */
 	struct iommu_domain_geometry geometry;
-	struct iommu_dma_cookie *iova_cookie;
 	int (*iopf_handler)(struct iopf_group *group);
 
 	union { /* Pointer usable by owner of the domain */
+		struct iommu_dma_cookie *iova_cookie; /* dma-iommu */
 		struct iommufd_hw_pagetable *iommufd_hwpt; /* iommufd */
 	};
 	union { /* Fault handler */
drivers/iommu/dma-iommu.c +2 -1
index 7ee71b9c53bd..ee2fcc058aa3 100644
@@ -443,12 +443,13 @@ EXPORT_SYMBOL(iommu_get_msi_cookie);
  */
 void iommu_put_dma_cookie(struct iommu_domain *domain)
 {
-	struct iommu_dma_cookie *cookie = domain->iova_cookie;
 	struct iommu_dma_msi_page *msi, *tmp;
+	struct iommu_dma_cookie *cookie;
 
 	if (domain->private_data_owner != IOMMU_DOMAIN_DATA_OWNER_DMA)
 		return;
 
+	cookie = domain->iova_cookie;
 	if (!cookie)
 		return;
 
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v2 1/4] iommu: Add private_data_owner to iommu_domain

In reply to Nicolin Chen
Quoted 20 lines · Nicolin Chen

Steal two bits from the 32-bit "type" in struct iommu_domain to store a new tag for private data owned by either dma-iommu or iommufd.

Set the domain->private_data_owner in dma-iommu and iommufd. These will be used to replace the sw_msi function pointer.

---
  include/linux/iommu.h                | 7 ++++++-
  drivers/iommu/dma-iommu.c            | 2 ++
  drivers/iommu/iommufd/hw_pagetable.c | 3 +++
  3 files changed, 11 insertions(+), 1 deletion(-)
include/linux/iommu.h +0 -0
index e93d2e918599..4f2774c08262 100644
@@ -209,8 +209,13 @@ struct iommu_domain_geometry {
  #define IOMMU_DOMAIN_PLATFORM	(__IOMMU_DOMAIN_PLATFORM)

include/linux/iommu.h struct iommu_domain_geometry { · line 218

Quoted 10 lines
include/linux/iommu.h +6 -1
  #define IOMMU_DOMAIN_NESTED	(__IOMMU_DOMAIN_NESTED)
 
+#define IOMMU_DOMAIN_DATA_OWNER_NONE (0U)
+#define IOMMU_DOMAIN_DATA_OWNER_DMA (1U)
+#define IOMMU_DOMAIN_DATA_OWNER_IOMMUFD (2U)
+
  struct iommu_domain {
-	unsigned type;
+	u32 type : 30;
+	u32 private_data_owner : 2;

Is there any special consideration for reserving only 2 bits for the private data owner? Is it possible to allocate more bits so that it could be more extensible for the future?

For example, current iommu core allows a kernel device driver to allocate a paging domain and replace the default domain for kernel DMA. This suggests the private data owner bits may be needed beyond iommu-dma and iommufd.

Thanks,
baolu

#3 lore raw

Re: [PATCH v2 1/4] iommu: Add private_data_owner to iommu_domain

Quoted 26 lines · Baolu Lu
Quoted 25 lines · Nicolin Chen

Steal two bits from the 32-bit "type" in struct iommu_domain to store a new tag for private data owned by either dma-iommu or iommufd.

Set the domain->private_data_owner in dma-iommu and iommufd. These will be used to replace the sw_msi function pointer.

---
  include/linux/iommu.h                | 7 ++++++-
  drivers/iommu/dma-iommu.c            | 2 ++
  drivers/iommu/iommufd/hw_pagetable.c | 3 +++
  3 files changed, 11 insertions(+), 1 deletion(-)
include/linux/iommu.h +4 -0
index e93d2e918599..4f2774c08262 100644
@@ -209,8 +209,13 @@ struct iommu_domain_geometry {
  #define IOMMU_DOMAIN_PLATFORM	(__IOMMU_DOMAIN_PLATFORM)
  #define IOMMU_DOMAIN_NESTED	(__IOMMU_DOMAIN_NESTED)
+#define IOMMU_DOMAIN_DATA_OWNER_NONE (0U)
+#define IOMMU_DOMAIN_DATA_OWNER_DMA (1U)
+#define IOMMU_DOMAIN_DATA_OWNER_IOMMUFD (2U)
+
Quoted 8 lines

include/linux/iommu.h struct iommu_domain_geometry { · line 218

Quoted 4 lines
include/linux/iommu.h +2 -1
  struct iommu_domain {
-	unsigned type;
+	u32 type : 30;
+	u32 private_data_owner : 2;

Is there any special consideration for reserving only 2 bits for the private data owner? Is it possible to allocate more bits so that it could be more extensible for the future?

It could. This "2" is copied from Jason's suggestion:
https://lore.kernel.org/linux-iommu/[email protected]/

Quoted 4 lines

For example, current iommu core allows a kernel device driver to allocate a paging domain and replace the default domain for kernel DMA. This suggests the private data owner bits may be needed beyond iommu-dma and iommufd.

What's the potential "private data" in this case?

Thanks
Nicolin

#4 lore raw

Re: [PATCH v2 1/4] iommu: Add private_data_owner to iommu_domain

Quoted 34 lines · Nicolin Chen
Quoted 25 lines · Baolu Lu
Quoted 24 lines · Nicolin Chen

Steal two bits from the 32-bit "type" in struct iommu_domain to store a new tag for private data owned by either dma-iommu or iommufd.

Set the domain->private_data_owner in dma-iommu and iommufd. These will be used to replace the sw_msi function pointer.

---
   include/linux/iommu.h                | 7 ++++++-
   drivers/iommu/dma-iommu.c            | 2 ++
   drivers/iommu/iommufd/hw_pagetable.c | 3 +++
   3 files changed, 11 insertions(+), 1 deletion(-)
include/linux/iommu.h +3 -0
index e93d2e918599..4f2774c08262 100644
@@ -209,8 +209,13 @@ struct iommu_domain_geometry {
   #define IOMMU_DOMAIN_PLATFORM	(__IOMMU_DOMAIN_PLATFORM)
   #define IOMMU_DOMAIN_NESTED	(__IOMMU_DOMAIN_NESTED)
+#define IOMMU_DOMAIN_DATA_OWNER_NONE (0U)
+#define IOMMU_DOMAIN_DATA_OWNER_DMA (1U)
+#define IOMMU_DOMAIN_DATA_OWNER_IOMMUFD (2U)
Quoted 8 lines

include/linux/iommu.h struct iommu_domain_geometry { · line 218

Quoted 5 lines
include/linux/iommu.h +3 -1
+
   struct iommu_domain {
-	unsigned type;
+	u32 type : 30;
+	u32 private_data_owner : 2;

Is there any special consideration for reserving only 2 bits for the private data owner? Is it possible to allocate more bits so that it could be more extensible for the future?

Quoted 8 lines

It could. This "2" is copied from Jason's suggestion:
https://lore.kernel.org/linux-iommu/[email protected]/

Quoted 4 lines

For example, current iommu core allows a kernel device driver to allocate a paging domain and replace the default domain for kernel DMA. This suggests the private data owner bits may be needed beyond iommu-dma and iommufd.

What's the potential "private data" in this case?

I have no idea, just feeling that we could leave room for tomorrow.

maintainer
#5 lore raw

Re: [PATCH v2 1/4] iommu: Add private_data_owner to iommu_domain

In reply to Nicolin Chen
Quoted 46 lines · Nicolin Chen

Steal two bits from the 32-bit "type" in struct iommu_domain to store a new tag for private data owned by either dma-iommu or iommufd.

Set the domain->private_data_owner in dma-iommu and iommufd. These will be used to replace the sw_msi function pointer.

---
  include/linux/iommu.h                | 7 ++++++-
  drivers/iommu/dma-iommu.c            | 2 ++
  drivers/iommu/iommufd/hw_pagetable.c | 3 +++
  3 files changed, 11 insertions(+), 1 deletion(-)
include/linux/iommu.h +6 -1
index e93d2e918599..4f2774c08262 100644
@@ -209,8 +209,13 @@ struct iommu_domain_geometry {
  #define IOMMU_DOMAIN_PLATFORM	(__IOMMU_DOMAIN_PLATFORM)
  #define IOMMU_DOMAIN_NESTED	(__IOMMU_DOMAIN_NESTED)
 
+#define IOMMU_DOMAIN_DATA_OWNER_NONE (0U)
+#define IOMMU_DOMAIN_DATA_OWNER_DMA (1U)
+#define IOMMU_DOMAIN_DATA_OWNER_IOMMUFD (2U)
+
  struct iommu_domain {
-	unsigned type;
+	u32 type : 30;
+	u32 private_data_owner : 2;
  	const struct iommu_domain_ops *ops;
  	const struct iommu_dirty_ops *dirty_ops;
  	const struct iommu_ops *owner; /* Whose domain_alloc we came from */
drivers/iommu/dma-iommu.c +1 -0
index 94263ed2c564..78915d74e8fa 100644
@@ -403,6 +403,7 @@ int iommu_get_dma_cookie(struct iommu_domain *domain)
 
  	mutex_init(&domain->iova_cookie->mutex);
  	iommu_domain_set_sw_msi(domain, iommu_dma_sw_msi);
+	domain->private_data_owner = IOMMU_DOMAIN_DATA_OWNER_DMA;
  	return 0;
  }
 

drivers/iommu/dma-iommu.c int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base) · line 439

Quoted 4 lines
drivers/iommu/dma-iommu.c +1 -0
@@ -435,6 +436,7 @@ int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base)
  	cookie->msi_iova = base;
  	domain->iova_cookie = cookie;
  	iommu_domain_set_sw_msi(domain, iommu_dma_sw_msi);
+	domain->private_data_owner = IOMMU_DOMAIN_DATA_OWNER_DMA;

This doesn't help all that much when it can still be called on any old unmanaged domain and silently overwrite whatever the previous user thought they owned...

And really the "owner" of MSI cookies is VFIO, it just happens that all the code for them still lives in iommu-dma because it was written to piggyback off the same irqchip hooks. TBH I've long been keen on separating them further from "real" IOVA cookies, and generalising said hooks really removes any remaining reason to keep the implementations tied together at all, should they have cause to diverge further (e.g. with makign the MSI cookie window programmable). What I absolutely want to avoid is a notion of having two types of MSI-mapping cookies, one of which is two types of MSI-mapping cookies.

Hopefully that is all clear in the patch I proposed.

Thanks,
Robin.

Quoted 31 lines
drivers/iommu/dma-iommu.c +0 -0
  	return 0;
  }
  EXPORT_SYMBOL(iommu_get_msi_cookie);
drivers/iommu/iommufd/hw_pagetable.c +3 -0
index 7de6e914232e..5640444de475 100644
@@ -157,6 +157,7 @@ iommufd_hwpt_paging_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
  		}
  	}
  	iommu_domain_set_sw_msi(hwpt->domain, iommufd_sw_msi);
+	hwpt->domain->private_data_owner = IOMMU_DOMAIN_DATA_OWNER_IOMMUFD;
 
  	/*
  	 * Set the coherency mode before we do iopt_table_add_domain() as some
@@ -253,6 +254,7 @@ iommufd_hwpt_nested_alloc(struct iommufd_ctx *ictx,
  	}
  	hwpt->domain->owner = ops;
  	iommu_domain_set_sw_msi(hwpt->domain, iommufd_sw_msi);
+	hwpt->domain->private_data_owner = IOMMU_DOMAIN_DATA_OWNER_IOMMUFD;
 
  	if (WARN_ON_ONCE(hwpt->domain->type != IOMMU_DOMAIN_NESTED)) {
  		rc = -EINVAL;
@@ -310,6 +312,7 @@ iommufd_viommu_alloc_hwpt_nested(struct iommufd_viommu *viommu, u32 flags,
  	}
  	hwpt->domain->owner = viommu->iommu_dev->ops;
  	iommu_domain_set_sw_msi(hwpt->domain, iommufd_sw_msi);
+	hwpt->domain->private_data_owner = IOMMU_DOMAIN_DATA_OWNER_IOMMUFD;
 
  	if (WARN_ON_ONCE(hwpt->domain->type != IOMMU_DOMAIN_NESTED)) {
  		rc = -EINVAL;
maintainer
#8 lore raw

Re: [PATCH v2 2/4] iommufd: Move iommufd_sw_msi and related functions to driver.c

In reply to Nicolin Chen

include/linux/iommufd.h struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx, · line 192

Quoted 6 lines · Nicolin Chen
include/linux/iommufd.h +2 -0
@@ -187,6 +188,8 @@ struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx,
 					     enum iommufd_object_type type);
 struct device *iommufd_viommu_find_dev(struct iommufd_viommu *viommu,
 				       unsigned long vdev_id);
+int iommufd_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
+		   phys_addr_t msi_addr);

This should probably go into drivers/iommu/iommu-priv.h ?

Quoted 13 lines
drivers/iommu/iommufd/driver.c +13 -0
+int iommufd_sw_msi_install(struct iommufd_ctx *ictx,
+			   struct iommufd_hwpt_paging *hwpt_paging,
+			   struct iommufd_sw_msi_map *msi_map)
+{
+	unsigned long iova;
+
+	lockdep_assert_held(&ictx->sw_msi_lock);
+
+	iova = msi_map->sw_msi_start + msi_map->pgoff * PAGE_SIZE;
+	if (!test_bit(msi_map->id, hwpt_paging->present_sw_msi.bitmap)) {
+		int rc;
+
+		rc = iommu_map(hwpt_paging->common.domain, iova,

drivers/iommu/iommufd/driver.c struct device *iommufd_viommu_find_dev(struct iommufd_viommu *viommu, · line 112

Quoted 10 lines
drivers/iommu/iommufd/driver.c +10 -0
+			       msi_map->msi_addr, PAGE_SIZE,
+			       IOMMU_WRITE | IOMMU_READ | IOMMU_MMIO,
+			       GFP_KERNEL_ACCOUNT);
+		if (rc)
+			return rc;
+		__set_bit(msi_map->id, hwpt_paging->present_sw_msi.bitmap);
+	}
+	return 0;
+}
+EXPORT_SYMBOL_NS_GPL(iommufd_sw_msi_install, "IOMMUFD");

Stubbed out too if CONFIG_IRQ_MSI_IOMMU ?

I'm still wondering if we should use a function pointer, how big was this compiled anyhow?

Jason

#6 lore raw

Re: [PATCH v2 1/4] iommu: Add private_data_owner to iommu_domain

In reply to Robin Murphy
Quoted 10 lines · Robin Murphy

drivers/iommu/dma-iommu.c int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base) · line 439

Quoted 5 lines · Nicolin Chen
drivers/iommu/dma-iommu.c +1 -0
@@ -435,6 +436,7 @@ int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base)
  	cookie->msi_iova = base;
  	domain->iova_cookie = cookie;
  	iommu_domain_set_sw_msi(domain, iommu_dma_sw_msi);
+	domain->private_data_owner = IOMMU_DOMAIN_DATA_OWNER_DMA;

This doesn't help all that much when it can still be called on any old unmanaged domain and silently overwrite whatever the previous user thought they owned...

We could add another validation on top of this function to reject private_data_owner != IOMMU_DOMAIN_DATA_OWNER_NONE?

Quoted 11 lines

And really the "owner" of MSI cookies is VFIO, it just happens that all the code for them still lives in iommu-dma because it was written to piggyback off the same irqchip hooks. TBH I've long been keen on separating them further from "real" IOVA cookies, and generalising said hooks really removes any remaining reason to keep the implementations tied together at all, should they have cause to diverge further (e.g. with makign the MSI cookie window programmable). What I absolutely want to avoid is a notion of having two types of MSI-mapping cookies, one of which is two types of MSI-mapping cookies.

Hopefully that is all clear in the patch I proposed.

Yea, decoupling the MSI cookie from the iova_cookie makes sense to me. I had the same feeling last week when I was preparing the cleanup patch that added a iommu_put_msi_cookie(), but hesitated after foreseeing a big rework that I was not sure you would like or not. Glad that you did it..

With your patch now we even merged the two unions, which is nicer from my point of view.

Thanks
Nicolin

#9 lore raw

Re: [PATCH v2 2/4] iommufd: Move iommufd_sw_msi and related functions to driver.c

In reply to Jason Gunthorpe
Quoted 9 lines · Jason Gunthorpe

include/linux/iommufd.h struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx, · line 192

Quoted 6 lines · Nicolin Chen
include/linux/iommufd.h +2 -0
@@ -187,6 +188,8 @@ struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx,
 					     enum iommufd_object_type type);
 struct device *iommufd_viommu_find_dev(struct iommufd_viommu *viommu,
 				       unsigned long vdev_id);
+int iommufd_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
+		   phys_addr_t msi_addr);

This should probably go into drivers/iommu/iommu-priv.h ?

Oh right, I forgot we had that.

Quoted 17 lines
Quoted 17 lines
drivers/iommu/iommufd/driver.c +17 -0
+int iommufd_sw_msi_install(struct iommufd_ctx *ictx,
+			   struct iommufd_hwpt_paging *hwpt_paging,
+			   struct iommufd_sw_msi_map *msi_map)
+{
+	unsigned long iova;
+
+	lockdep_assert_held(&ictx->sw_msi_lock);
+
+	iova = msi_map->sw_msi_start + msi_map->pgoff * PAGE_SIZE;
+	if (!test_bit(msi_map->id, hwpt_paging->present_sw_msi.bitmap)) {
+		int rc;
+
+		rc = iommu_map(hwpt_paging->common.domain, iova,
+			       msi_map->msi_addr, PAGE_SIZE,
+			       IOMMU_WRITE | IOMMU_READ | IOMMU_MMIO,
+			       GFP_KERNEL_ACCOUNT);
+		if (rc)
Quoted 8 lines

drivers/iommu/iommufd/driver.c struct device *iommufd_viommu_find_dev(struct iommufd_viommu *viommu, · line 112

Quoted 6 lines
drivers/iommu/iommufd/driver.c +6 -0
+			return rc;
+		__set_bit(msi_map->id, hwpt_paging->present_sw_msi.bitmap);
+	}
+	return 0;
+}
+EXPORT_SYMBOL_NS_GPL(iommufd_sw_msi_install, "IOMMUFD");

Stubbed out too if CONFIG_IRQ_MSI_IOMMU ?

In that case, the other caller iommufd_group_setup_msi() could be {

#if IS_ENABLED(CONFIG_IRQ_MSI_IOMMU)
	....
	iommufd_sw_msi_install();
	...
#endif
	return 0;
}

?

Quoted 2 lines

I'm still wondering if we should use a function pointer, how big was this compiled anyhow?

Hmm, you mean the size of driver.o? It's 192K (before) vs 212K (after).

Thanks
Nicolin

maintainer
#10 lore raw

Re: [PATCH v2 2/4] iommufd: Move iommufd_sw_msi and related functions to driver.c

Quoted 4 lines · Nicolin Chen
Quoted 1 line

Stubbed out too if CONFIG_IRQ_MSI_IOMMU ?

In that case, the other caller iommufd_group_setup_msi() could be {

Quoted 8 lines
#if IS_ENABLED(CONFIG_IRQ_MSI_IOMMU)
	....
	iommufd_sw_msi_install();
	...
#endif
	return 0;
}

?

Or a empty static inline

Quoted 5 lines
Quoted 2 lines

I'm still wondering if we should use a function pointer, how big was this compiled anyhow?

Hmm, you mean the size of driver.o? It's 192K (before) vs 212K (after).

Yes, but use the 'size' command to measure before/after

Jason

#11 lore raw

Re: [PATCH v2 2/4] iommufd: Move iommufd_sw_msi and related functions to driver.c

Quoted 16 lines · Jason Gunthorpe
Quoted 4 lines · Nicolin Chen
Quoted 1 line

Stubbed out too if CONFIG_IRQ_MSI_IOMMU ?

In that case, the other caller iommufd_group_setup_msi() could be {

Quoted 8 lines
#if IS_ENABLED(CONFIG_IRQ_MSI_IOMMU)
	....
	iommufd_sw_msi_install();
	...
#endif
	return 0;
}

?

Or a empty static inline

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

I'm still wondering if we should use a function pointer, how big was this compiled anyhow?

Hmm, you mean the size of driver.o? It's 192K (before) vs 212K (after).

Yes, but use the 'size' command to measure before/after

Before:

   text	   data	    bss	    dec	    hex	filename
    722	     44	      0	    766	    2fe	drivers/iommu/iommufd/driver.o

After:
text data bss dec hex filename
1888 100 0 1988 7c4 drivers/iommu/iommufd/driver.o

Thanks
Nicolin