Skip to contentSkip to conversation

iommu: SVA and IOPF refactoring

PATCH Superseded Lu Baolu 4 participants 43 messages Aug 26, 2022 – Sep 2, 2022

Subsystems (9)

v12 of 14 Superseded Latest is v14 →

lore
Share & tools
lore

Lu Baolu

author maintainer
#0 lore raw

v12 cover iommu: SVA and IOPF refactoring

Hi folks,

The former part of this series introduces the IOMMU interfaces to attach or detach an iommu domain to/from a pasid of a device, and refactors the exsiting IOMMU SVA implementation by assigning an SVA type of iommu domain to a shared virtual address and replacing sva_bind/unbind iommu ops with a set_dev_pasid domain ops.

The latter part changes the existing I/O page fault handling framework from only serving SVA to a generic one. Any driver or component could handle the I/O page faults for its domain in its own way by installing an I/O page fault handler.

This series has been functionally tested by Tony Zhu on Intel hardware and Zhangfei Gao on arm64 (Kunpeng920) hardware. Thanks a lot for the efforts.

This series is also available on github:
https://github.com/LuBaolu/intel-iommu/commits/iommu-sva-refactoring-v12

Please review and suggest.

Best regards,
baolu

Change log:
v12:
- Add blocking domain support in both vt-d and smmuv3 drivers and make the set blocking domain through its own domain ops.
- Add a type parameter in iommu_get_domain_for_dev_pasid() to matach the interested domain type.
- Only enforce ACS RR & UF in pci_enable_pasid() and refine the commit messages according to Bjorn's suggestions.
- Misc code and comment refinement.

v11:
- https://lore.kernel.org/linux-iommu/[email protected]/
- [PATCH 04/13] PCI: Allow PASID only when ACS enforced on upstreaming path
- new patch
- [PATCH 05/13] iommu: Add attach/detach_dev_pasid iommu interface
- Remove block_dev_pasid domain ops and use setting group blocking domain instead.
- Remove iommu_group_immutable_singleton(). Move the PCI/ACS
requirement into the pci_enable_pasid(). All devices in an iommu
group share a same iommu domain for each pasid.
- [PATCH 06/13] iommu: Add IOMMU SVA domain support
- Add a refcount for SVA multiple bindings.
- [PATCH 07/13] iommu/vt-d: Add SVA domain support
- Use set_dev_pasid for both domain attaching and detaching.
- [PATCH 08/13] arm-smmu-v3/sva: Add SVA domain support
- Use set_dev_pasid for both domain attaching and detaching.
- [PATCH 09/13] iommu/sva: Refactoring iommu_sva_bind/unbind_device()
- Remove the refcount of iommu_sva::users.
- Add iommu_sva::domain.
- [PATCH 11/13] iommu: Prepare IOMMU domain for IOPF
- Remove unnecessary check of IS_ERR_OR_NULL(mm).
- [Overall]
- Rebase to v6.0-rc1.
- Remove previous Test-by's as some APIs are changed.
- Polishing of various codes and comments.

v10:
- https://lore.kernel.org/linux-iommu/[email protected]/
- Rebase on next branch of iommu tree.
- Split attach/detach_device_pasid interfaces and SVA domain extensions
to different patches.
- Handle the return error of xa_cmpxchg() gracefully.
- Directly pass mm in as the SVA fault data.
- Rename iopf_handle_group() to iopf_handler().
- Some commit message and code comment refinement.
- Add Tested-by's from Zhangfei and Tony.

v9:
- https://lore.kernel.org/linux-iommu/[email protected]/
- Some minor changes on comments and function names.
- Simplify dev_iommu_get_max_pasids().

v8:
- https://lore.kernel.org/linux-iommu/[email protected]/
- Add support for calculating the max pasids that a device could
consume.
- Replace container_of_safe() with container_of.
- Remove iommu_ops->sva_domain_ops and make sva support through the generic domain_alloc/free() interfaces.
- [Robin] It would be logical to pass IOMMU_DOMAIN_SVA to the normal domain_alloc call, so that driver-internal stuff like context descriptors can be still be hung off the domain as usual (rather than all drivers having to implement some extra internal lookup mechanism
to handle all the SVA domain ops).
- [Robin] I'd just stick the mm pointer in struct iommu_domain, in a
union with the fault handler stuff those are mutually exclusive with SVA.
- https://lore.kernel.org/linux-iommu/[email protected]/

v7:
- https://lore.kernel.org/linux-iommu/[email protected]/
- Remove duplicate array for sva domain.
- Rename detach_dev_pasid to block_dev_pasid.
- Add raw device driver interfaces for iommufd.
- Other misc refinements and patch reorganization.
- Drop "dmaengine: idxd: Separate user and kernel pasid enabling" which has been picked for dmaengine tree.

v6:
- https://lore.kernel.org/linux-iommu/[email protected]/
- Refine the SVA basic data structures.

- Refine arm smmuv3 sva domain allocation.
- Fix a possible lock issue.

v5:
- https://lore.kernel.org/linux-iommu/[email protected]/
- Address review comments from Jean-Philippe Brucker. Very appreciated!
- Remove redundant pci aliases check in device_group_immutable_singleton().
- Treat all buses except PCI as static in immutable singleton check.
- As the sva_bind/unbind() have already guaranteed sva domain free only after iopf_queue_flush_dev(), remove the unnecessary domain refcount.
- Move domain get() out of the list iteration in iopf_handle_group().

v4:
- https://lore.kernel.org/linux-iommu/[email protected]/
- Solve the overlap with another series and make this series self-contained.
- No objection to the abstraction of data structure during v3 review. Hence remove the RFC subject prefix.
- Refine the immutable singleton group code according to Kevin's
comments.

v3:
- https://lore.kernel.org/linux-iommu/[email protected]/
- Rework iommu_group_singleton_lockdown() by adding a flag to the group that positively indicates the group can never have more than one member, even after hot plug.
- Abstract the data structs used for iommu sva in a separated patches to make it easier for review.
- I still keep the RFC prefix in this series as above two significant changes need at least another round review to be finalized.
- Several misc refinements.

v2:
- https://lore.kernel.org/linux-iommu/[email protected]/
- Add sva domain life cycle management to avoid race between unbind and page fault handling.
- Use a single domain for each mm.
- Return a single sva handler for the same binding.
- Add a new helper to meet singleton group requirement.
- Rework the SVA domain allocation for arm smmu v3 driver and move the pasid_bit initialization to device probe.
- Drop the patch "iommu: Handle IO page faults directly".
- Add mmget_not_zero(mm) in SVA page fault handler.

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

Lu Baolu (17):
iommu: Add max_pasids field in struct iommu_device
iommu: Add max_pasids field in struct dev_iommu
iommu: Remove SVM_FLAG_SUPERVISOR_MODE support
PCI: Enable PASID only when ACS RR & UF enabled on upstream path
iommu: Add attach/detach_dev_pasid iommu interface
iommu: Add IOMMU SVA domain support
iommu: Try to allocate blocking domain when probing device
iommu: Make free of iommu_domain_ops optional iommu/vt-d: Add blocking domain support
iommu/vt-d: Add SVA domain support
arm-smmu-v3: Add blocking domain support arm-smmu-v3/sva: Add SVA domain support
iommu/sva: Refactoring iommu_sva_bind/unbind_device()
iommu: Remove SVA related callbacks from iommu ops
iommu: Prepare IOMMU domain for IOPF
iommu: Per-domain I/O page fault handling
iommu: Rename iommu-sva-lib.{c,h}

 include/linux/intel-svm.h                     |  13 -
 include/linux/iommu.h                         | 112 +++++--
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h   |  22 +-
 drivers/iommu/intel/iommu.h                   |  17 +-
 .../iommu/{iommu-sva-lib.h => iommu-sva.h}    |  14 +-
 drivers/dma/idxd/cdev.c                       |   3 +-
 drivers/dma/idxd/init.c                       |  25 +-
 .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c   | 104 ++++---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c   |  45 ++-
 drivers/iommu/intel/dmar.c                    |   7 +
 drivers/iommu/intel/iommu.c                   |  48 ++-
 drivers/iommu/intel/svm.c                     | 147 ++++-----
 drivers/iommu/io-pgfault.c                    |  77 +----
 drivers/iommu/iommu-sva-lib.c                 |  71 -----
 drivers/iommu/iommu-sva.c                     | 239 ++++++++++++++
 drivers/iommu/iommu.c                         | 294 +++++++++++-------
 drivers/misc/uacce/uacce.c                    |   2 +-
 drivers/pci/ats.c                             |   3 +
 drivers/iommu/Makefile                        |   2 +-
 19 files changed, 769 insertions(+), 476 deletions(-)
 rename drivers/iommu/{iommu-sva-lib.h => iommu-sva.h} (83%)
 delete mode 100644 drivers/iommu/iommu-sva-lib.c
 create mode 100644 drivers/iommu/iommu-sva.c
Signature · 1 line
2.25.1
author maintainer
#1 lore raw

PATCH v12 1/17 iommu: Add max_pasids field in struct iommu_device

Use this field to keep the number of supported PASIDs that an IOMMU hardware is able to support. This is a generic attribute of an IOMMU and lifting it into the per-IOMMU device structure makes it possible to allocate a PASID for device without calls into the IOMMU drivers. Any iommu driver that supports PASID related features should set this field before enabling them on the devices.

In the Intel IOMMU driver, intel_iommu_sm is moved to CONFIG_INTEL_IOMMU enclave so that the pasid_supported() helper could be used in dmar.c without compilation errors.

---
 include/linux/iommu.h                       | 2 ++
 drivers/iommu/intel/iommu.h                 | 4 ++--
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 1 +
 drivers/iommu/intel/dmar.c                  | 7 +++++++
 4 files changed, 12 insertions(+), 2 deletions(-)
include/linux/iommu.h +2 -0
index ea30f00dc145..ed172cbdabf2 100644
@@ -322,12 +322,14 @@ struct iommu_domain_ops {
  * @list: Used by the iommu-core to keep a list of registered iommus
  * @ops: iommu-ops for talking to this iommu
  * @dev: struct device for sysfs handling
+ * @max_pasids: number of supported PASIDs
  */
 struct iommu_device {
 	struct list_head list;
 	const struct iommu_ops *ops;
 	struct fwnode_handle *fwnode;
 	struct device *dev;
+	u32 max_pasids;
 };
 
 /**
drivers/iommu/intel/iommu.h +2 -2
index 74b0e19e23ee..c4831d2bee93 100644
@@ -478,8 +478,6 @@ enum {
 #define VTD_FLAG_IRQ_REMAP_PRE_ENABLED	(1 << 1)
 #define VTD_FLAG_SVM_CAPABLE		(1 << 2)
 
-extern int intel_iommu_sm;
-
 #define sm_supported(iommu)	(intel_iommu_sm && ecap_smts((iommu)->ecap))
 #define pasid_supported(iommu)	(sm_supported(iommu) &&			\
 				 ecap_pasid((iommu)->ecap))
@@ -794,6 +792,7 @@ struct context_entry *iommu_context_addr(struct intel_iommu *iommu, u8 bus,
 extern const struct iommu_ops intel_iommu_ops;
 
 #ifdef CONFIG_INTEL_IOMMU
+extern int intel_iommu_sm;
 extern int iommu_calculate_agaw(struct intel_iommu *iommu);
 extern int iommu_calculate_max_sagaw(struct intel_iommu *iommu);
 extern int dmar_disabled;
@@ -809,6 +808,7 @@ static inline int iommu_calculate_max_sagaw(struct intel_iommu *iommu)
 }
 #define dmar_disabled	(1)
 #define intel_iommu_enabled (0)
+#define intel_iommu_sm (0)
 #endif
 
 static inline const char *decode_prq_descriptor(char *str, size_t size,
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +1 -0
index d32b02336411..f88541be8213 100644
@@ -3521,6 +3521,7 @@ static int arm_smmu_device_hw_probe(struct arm_smmu_device *smmu)
 	/* SID/SSID sizes */
 	smmu->ssid_bits = FIELD_GET(IDR1_SSIDSIZE, reg);
 	smmu->sid_bits = FIELD_GET(IDR1_SIDSIZE, reg);
+	smmu->iommu.max_pasids = 1UL << smmu->ssid_bits;
 
 	/*
 	 * If the SMMU supports fewer bits than would fill a single L2 stream
drivers/iommu/intel/dmar.c +7 -0
index 5a8f780e7ffd..3528058d253e 100644
@@ -1104,6 +1104,13 @@ static int alloc_iommu(struct dmar_drhd_unit *drhd)
 
 	raw_spin_lock_init(&iommu->register_lock);
 
+	/*
+	 * A value of N in PSS field of eCap register indicates hardware
+	 * supports PASID field of N+1 bits.
+	 */
+	if (pasid_supported(iommu))
+		iommu->iommu.max_pasids = 2UL << ecap_pss(iommu->ecap);
+
 	/*
 	 * This is only for hotplug; at boot time intel_iommu_enabled won't
 	 * be set yet. When intel_iommu_init() runs, it registers the units
Signature · 1 line
2.25.1
author maintainer
#2 lore raw

PATCH v12 2/17 iommu: Add max_pasids field in struct dev_iommu

In reply to Lu Baolu

Use this field to save the number of PASIDs that a device is able to consume. It is a generic attribute of a device and lifting it into the per-device dev_iommu struct could help to avoid the boilerplate code in various IOMMU drivers.

---
 include/linux/iommu.h |  2 ++
 drivers/iommu/iommu.c | 20 ++++++++++++++++++++
 2 files changed, 22 insertions(+)
include/linux/iommu.h +2 -0
index ed172cbdabf2..0d9ce209a501 100644
@@ -368,6 +368,7 @@ struct iommu_fault_param {
  * @fwspec:	 IOMMU fwspec data
  * @iommu_dev:	 IOMMU device this device is linked to
  * @priv:	 IOMMU Driver private data
+ * @max_pasids:  number of PASIDs this device can consume
  *
  * TODO: migrate other per device data pointers under iommu_dev_data, e.g.
  *	struct iommu_group	*iommu_group;
@@ -379,6 +380,7 @@ struct dev_iommu {
 	struct iommu_fwspec		*fwspec;
 	struct iommu_device		*iommu_dev;
 	void				*priv;
+	u32				max_pasids;
 };
 
 int iommu_device_register(struct iommu_device *iommu,
drivers/iommu/iommu.c +20 -0
index 780fb7071577..e9f6a8d33b58 100644
@@ -20,6 +20,7 @@
 #include <linux/idr.h>
 #include <linux/err.h>
 #include <linux/pci.h>
+#include <linux/pci-ats.h>
 #include <linux/bitops.h>
 #include <linux/property.h>
 #include <linux/fsl/mc.h>
@@ -218,6 +219,24 @@ static void dev_iommu_free(struct device *dev)
 	kfree(param);
 }
 
+static u32 dev_iommu_get_max_pasids(struct device *dev)
+{
+	u32 max_pasids = 0, bits = 0;
+	int ret;
+
+	if (dev_is_pci(dev)) {
+		ret = pci_max_pasids(to_pci_dev(dev));
+		if (ret > 0)
+			max_pasids = ret;
+	} else {
+		ret = device_property_read_u32(dev, "pasid-num-bits", &bits);
+		if (!ret)
+			max_pasids = 1UL << bits;
+	}
+
+	return min_t(u32, max_pasids, dev->iommu->iommu_dev->max_pasids);
+}
+
 static int __iommu_probe_device(struct device *dev, struct list_head *group_list)
 {
 	const struct iommu_ops *ops = dev->bus->iommu_ops;
@@ -243,6 +262,7 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list
 	}
 
 	dev->iommu->iommu_dev = iommu_dev;
+	dev->iommu->max_pasids = dev_iommu_get_max_pasids(dev);
 
 	group = iommu_group_get_for_dev(dev);
 	if (IS_ERR(group)) {
Signature · 1 line
2.25.1
author maintainer
#3 lore raw

PATCH v12 3/17 iommu: Remove SVM_FLAG_SUPERVISOR_MODE support

In reply to Lu Baolu

The current kernel DMA with PASID support is based on the SVA with a flag SVM_FLAG_SUPERVISOR_MODE. The IOMMU driver binds the kernel memory address space to a PASID of the device. The device driver programs the device with kernel virtual address (KVA) for DMA access. There have been security and functional issues with this approach:

- The lack of IOTLB synchronization upon kernel page table updates. (vmalloc, module/BPF loading, CONFIG_DEBUG_PAGEALLOC etc.)
- Other than slight more protection, using kernel virtual address (KVA) has little advantage over physical address. There are also no use cases yet where DMA engines need kernel virtual addresses for in-kernel DMA.

This removes SVM_FLAG_SUPERVISOR_MODE support from the IOMMU interface. The device drivers are suggested to handle kernel DMA with PASID through the kernel DMA APIs.

The drvdata parameter in iommu_sva_bind_device() and all callbacks is not needed anymore. Cleanup them as well.

---
 include/linux/intel-svm.h                     | 13 -----
 include/linux/iommu.h                         |  8 +--
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h   |  5 +-
 drivers/iommu/intel/iommu.h                   |  3 +-
 drivers/dma/idxd/cdev.c                       |  3 +-
 drivers/dma/idxd/init.c                       | 25 +-------
 .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c   |  3 +-
 drivers/iommu/intel/svm.c                     | 57 +++++--------------
 drivers/iommu/iommu.c                         |  5 +-
 drivers/misc/uacce/uacce.c                    |  2 +-
 10 files changed, 26 insertions(+), 98 deletions(-)
include/linux/intel-svm.h +0 -13
index 207ef06ba3e1..f9a0d44f6fdb 100644
@@ -13,17 +13,4 @@
 #define PRQ_RING_MASK	((0x1000 << PRQ_ORDER) - 0x20)
 #define PRQ_DEPTH	((0x1000 << PRQ_ORDER) >> 5)
 
-/*
- * The SVM_FLAG_SUPERVISOR_MODE flag requests a PASID which can be used only
- * for access to kernel addresses. No IOTLB flushes are automatically done
- * for kernel mappings; it is valid only for access to the kernel's static
- * 1:1 mapping of physical memory — not to vmalloc or even module mappings.
- * A future API addition may permit the use of such ranges, by means of an
- * explicit IOTLB flush call (akin to the DMA API's unmap method).
- *
- * It is unlikely that we will ever hook into flush_tlb_kernel_range() to
- * do such IOTLB flushes automatically.
- */
-#define SVM_FLAG_SUPERVISOR_MODE	BIT(0)
-
 #endif /* __INTEL_SVM_H__ */
include/linux/iommu.h +3 -5
index 0d9ce209a501..2f237c3cd680 100644
@@ -247,8 +247,7 @@ struct iommu_ops {
 	int (*dev_enable_feat)(struct device *dev, enum iommu_dev_features f);
 	int (*dev_disable_feat)(struct device *dev, enum iommu_dev_features f);
 
-	struct iommu_sva *(*sva_bind)(struct device *dev, struct mm_struct *mm,
-				      void *drvdata);
+	struct iommu_sva *(*sva_bind)(struct device *dev, struct mm_struct *mm);
 	void (*sva_unbind)(struct iommu_sva *handle);
 	u32 (*sva_get_pasid)(struct iommu_sva *handle);
 
@@ -670,8 +669,7 @@ int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features f);
 int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features f);
 
 struct iommu_sva *iommu_sva_bind_device(struct device *dev,
-					struct mm_struct *mm,
-					void *drvdata);
+					struct mm_struct *mm);
 void iommu_sva_unbind_device(struct iommu_sva *handle);
 u32 iommu_sva_get_pasid(struct iommu_sva *handle);
 
@@ -1007,7 +1005,7 @@ iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
 }
 
 static inline struct iommu_sva *
-iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata)
+iommu_sva_bind_device(struct device *dev, struct mm_struct *mm)
 {
 	return NULL;
 }
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +2 -3
index cd48590ada30..d2ba86470c42 100644
@@ -754,8 +754,7 @@ bool arm_smmu_master_sva_enabled(struct arm_smmu_master *master);
 int arm_smmu_master_enable_sva(struct arm_smmu_master *master);
 int arm_smmu_master_disable_sva(struct arm_smmu_master *master);
 bool arm_smmu_master_iopf_supported(struct arm_smmu_master *master);
-struct iommu_sva *arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm,
-				    void *drvdata);
+struct iommu_sva *arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm);
 void arm_smmu_sva_unbind(struct iommu_sva *handle);
 u32 arm_smmu_sva_get_pasid(struct iommu_sva *handle);
 void arm_smmu_sva_notifier_synchronize(void);
@@ -791,7 +790,7 @@ static inline bool arm_smmu_master_iopf_supported(struct arm_smmu_master *master
 }
 
 static inline struct iommu_sva *
-arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm, void *drvdata)
+arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm)
 {
 	return ERR_PTR(-ENODEV);
 }
drivers/iommu/intel/iommu.h +1 -2
index c4831d2bee93..91312aabdd53 100644
@@ -746,8 +746,7 @@ struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn);
 extern void intel_svm_check(struct intel_iommu *iommu);
 extern int intel_svm_enable_prq(struct intel_iommu *iommu);
 extern int intel_svm_finish_prq(struct intel_iommu *iommu);
-struct iommu_sva *intel_svm_bind(struct device *dev, struct mm_struct *mm,
-				 void *drvdata);
+struct iommu_sva *intel_svm_bind(struct device *dev, struct mm_struct *mm);
 void intel_svm_unbind(struct iommu_sva *handle);
 u32 intel_svm_get_pasid(struct iommu_sva *handle);
 int intel_svm_page_response(struct device *dev, struct iommu_fault_event *evt,
drivers/dma/idxd/cdev.c +1 -2
index c2808fd081d6..66720001ba1c 100644
@@ -6,7 +6,6 @@
 #include <linux/pci.h>
 #include <linux/device.h>
 #include <linux/sched/task.h>
-#include <linux/intel-svm.h>
 #include <linux/io-64-nonatomic-lo-hi.h>
 #include <linux/cdev.h>
 #include <linux/fs.h>
@@ -100,7 +99,7 @@ static int idxd_cdev_open(struct inode *inode, struct file *filp)
 	filp->private_data = ctx;
 
 	if (device_user_pasid_enabled(idxd)) {
-		sva = iommu_sva_bind_device(dev, current->mm, NULL);
+		sva = iommu_sva_bind_device(dev, current->mm);
 		if (IS_ERR(sva)) {
 			rc = PTR_ERR(sva);
 			dev_err(dev, "pasid allocation failed: %d\n", rc);
drivers/dma/idxd/init.c +1 -24
index aa3478257ddb..25ba73243c6f 100644
@@ -14,7 +14,6 @@
 #include <linux/io-64-nonatomic-lo-hi.h>
 #include <linux/device.h>
 #include <linux/idr.h>
-#include <linux/intel-svm.h>
 #include <linux/iommu.h>
 #include <uapi/linux/idxd.h>
 #include <linux/dmaengine.h>
@@ -466,29 +465,7 @@ static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_d
 
 static int idxd_enable_system_pasid(struct idxd_device *idxd)
 {
-	int flags;
-	unsigned int pasid;
-	struct iommu_sva *sva;
-
-	flags = SVM_FLAG_SUPERVISOR_MODE;
-
-	sva = iommu_sva_bind_device(&idxd->pdev->dev, NULL, &flags);
-	if (IS_ERR(sva)) {
-		dev_warn(&idxd->pdev->dev,
-			 "iommu sva bind failed: %ld\n", PTR_ERR(sva));
-		return PTR_ERR(sva);
-	}
-
-	pasid = iommu_sva_get_pasid(sva);
-	if (pasid == IOMMU_PASID_INVALID) {
-		iommu_sva_unbind_device(sva);
-		return -ENODEV;
-	}
-
-	idxd->sva = sva;
-	idxd->pasid = pasid;
-	dev_dbg(&idxd->pdev->dev, "system pasid: %u\n", pasid);
-	return 0;
+	return -EOPNOTSUPP;
 }
 
 static void idxd_disable_system_pasid(struct idxd_device *idxd)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +1 -2
index 1ef7bbb4acf3..f155d406c5d5 100644
@@ -367,8 +367,7 @@ __arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm)
 	return ERR_PTR(ret);
 }
 
-struct iommu_sva *
-arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm, void *drvdata)
+struct iommu_sva *arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm)
 {
 	struct iommu_sva *handle;
 	struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
drivers/iommu/intel/svm.c +14 -43

116 lines not on this page · raw message

drivers/iommu/iommu.c +2 -3
index e9f6a8d33b58..1d28a74a0511 100644
@@ -2775,7 +2775,6 @@ EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
  * iommu_sva_bind_device() - Bind a process address space to a device
  * @dev: the device
  * @mm: the mm to bind, caller must hold a reference to it
- * @drvdata: opaque data pointer to pass to bind callback
  *
  * Create a bond between device and address space, allowing the device to access
  * the mm using the returned PASID. If a bond already exists between @device and
@@ -2788,7 +2787,7 @@ EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
  * On error, returns an ERR_PTR value.
  */
 struct iommu_sva *
-iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata)
+iommu_sva_bind_device(struct device *dev, struct mm_struct *mm)
 {
 	struct iommu_group *group;
 	struct iommu_sva *handle = ERR_PTR(-EINVAL);
@@ -2813,7 +2812,7 @@ iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata)
 	if (iommu_group_device_count(group) != 1)
 		goto out_unlock;
 
-	handle = ops->sva_bind(dev, mm, drvdata);
+	handle = ops->sva_bind(dev, mm);
 
 out_unlock:
 	mutex_unlock(&group->mutex);
drivers/misc/uacce/uacce.c +1 -1
index b70a013139c7..905eff1f840e 100644
@@ -108,7 +108,7 @@ static int uacce_bind_queue(struct uacce_device *uacce, struct uacce_queue *q)
 	if (!(uacce->flags & UACCE_DEV_SVA))
 		return 0;
 
-	handle = iommu_sva_bind_device(uacce->parent, current->mm, NULL);
+	handle = iommu_sva_bind_device(uacce->parent, current->mm);
 	if (IS_ERR(handle))
 		return PTR_ERR(handle);
 
Signature · 1 line
2.25.1
author maintainer
#4 lore raw

PATCH v12 4/17 PCI: Enable PASID only when ACS RR & UF enabled on upstream path

In reply to Lu Baolu

The Requester ID/Process Address Space ID (PASID) combination identifies an address space distinct from the PCI bus address space, e.g., an address space defined by an IOMMU.

But the PCIe fabric routes Memory Requests based on the TLP address, ignoring any PASID (PCIe r6.0, sec 2.2.10.4), so a TLP with PASID that SHOULD go upstream to the IOMMU may instead be routed as a P2P Request if its address falls in a bridge window.

To ensure that all Memory Requests with PASID are routed upstream, only enable PASID if ACS P2P Request Redirect and Upstream Forwarding are enabled for the path leading to the device.

---
 drivers/pci/ats.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/pci/ats.c +3 -0
index c967ad6e2626..f9cc2e10b676 100644
@@ -382,6 +382,9 @@ int pci_enable_pasid(struct pci_dev *pdev, int features)
 	if (!pasid)
 		return -EINVAL;
 
+	if (!pci_acs_path_enabled(pdev, NULL, PCI_ACS_RR | PCI_ACS_UF))
+		return -EINVAL;
+
 	pci_read_config_word(pdev, pasid + PCI_PASID_CAP, &supported);
 	supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV;
 
Signature · 1 line
2.25.1
Reviewed-by on v12 4/17
#5 lore raw

Re: [PATCH v12 04/17] PCI: Enable PASID only when ACS RR & UF enabled on upstream path

Quoted 13 lines · Lu Baolu

The Requester ID/Process Address Space ID (PASID) combination identifies an address space distinct from the PCI bus address space, e.g., an address space defined by an IOMMU.

But the PCIe fabric routes Memory Requests based on the TLP address, ignoring any PASID (PCIe r6.0, sec 2.2.10.4), so a TLP with PASID that SHOULD go upstream to the IOMMU may instead be routed as a P2P Request if its address falls in a bridge window.

To ensure that all Memory Requests with PASID are routed upstream, only enable PASID if ACS P2P Request Redirect and Upstream Forwarding are enabled for the path leading to the device.

Quoted 8 lines
---
 drivers/pci/ats.c | 3 +++
 1 file changed, 3 insertions(+)

Jason

author maintainer
#6 lore raw

PATCH v12 5/17 iommu: Add attach/detach_dev_pasid iommu interface

In reply to Lu Baolu

Attaching an IOMMU domain to a PASID of a device is a generic operation for modern IOMMU drivers which support PASID-granular DMA address translation. Currently visible usage scenarios include (but not limited):

- SVA (Shared Virtual Address)
- kernel DMA with PASID
- hardware-assist mediated device

This adds set_dev_pasid domain ops for this purpose and also adds some interfaces for device drivers to attach/detach/retrieve a domain for a PASID of a device.

If multiple devices share a single group, it's fine as long the fabric always routes every TLP marked with a PASID to the host bridge and only the host bridge. For example, ACS achieves this universally and has been checked when pci_enable_pasid() is called. As we can't reliably tell the source apart in a group, all the devices in a group have to be considered as the same source, and mapped to the same PASID table.

---
 include/linux/iommu.h |  28 ++++++++++
 drivers/iommu/iommu.c | 126 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 154 insertions(+)
include/linux/iommu.h +28 -0
index 2f237c3cd680..5e9553a7de0d 100644
@@ -266,6 +266,7 @@ struct iommu_ops {
  * struct iommu_domain_ops - domain specific operations
  * @attach_dev: attach an iommu domain to a device
  * @detach_dev: detach an iommu domain from a device
+ * @set_dev_pasid: set an iommu domain to a pasid of device
  * @map: map a physically contiguous memory region to an iommu domain
  * @map_pages: map a physically contiguous set of pages of the same size to
  *             an iommu domain.
@@ -286,6 +287,8 @@ struct iommu_ops {
 struct iommu_domain_ops {
 	int (*attach_dev)(struct iommu_domain *domain, struct device *dev);
 	void (*detach_dev)(struct iommu_domain *domain, struct device *dev);
+	int (*set_dev_pasid)(struct iommu_domain *domain, struct device *dev,
+			     ioasid_t pasid);
 
 	int (*map)(struct iommu_domain *domain, unsigned long iova,
 		   phys_addr_t paddr, size_t size, int prot, gfp_t gfp);
@@ -680,6 +683,13 @@ int iommu_group_claim_dma_owner(struct iommu_group *group, void *owner);
 void iommu_group_release_dma_owner(struct iommu_group *group);
 bool iommu_group_dma_owner_claimed(struct iommu_group *group);
 
+int iommu_attach_device_pasid(struct iommu_domain *domain,
+			      struct device *dev, ioasid_t pasid);
+void iommu_detach_device_pasid(struct iommu_domain *domain,
+			       struct device *dev, ioasid_t pasid);
+struct iommu_domain *
+iommu_get_domain_for_dev_pasid(struct device *dev, ioasid_t pasid,
+			       unsigned int type);
 #else /* CONFIG_IOMMU_API */
 
 struct iommu_ops {};
@@ -1047,6 +1057,24 @@ static inline bool iommu_group_dma_owner_claimed(struct iommu_group *group)
 {
 	return false;
 }
+
+static inline int iommu_attach_device_pasid(struct iommu_domain *domain,
+					    struct device *dev, ioasid_t pasid)
+{
+	return -ENODEV;
+}
+
+static inline void iommu_detach_device_pasid(struct iommu_domain *domain,
+					     struct device *dev, ioasid_t pasid)
+{
+}
+
+static inline struct iommu_domain *
+iommu_get_domain_for_dev_pasid(struct device *dev, ioasid_t pasid,
+			       unsigned int type)
+{
+	return NULL;
+}
 #endif /* CONFIG_IOMMU_API */
 
 /**
drivers/iommu/iommu.c +126 -0

141 lines not on this page · raw message

Signature · 1 line
2.25.1
#7 lore raw

Re: [PATCH v12 05/17] iommu: Add attach/detach_dev_pasid iommu interface

Quoted 8 lines · Lu Baolu

+ * iommu_get_domain_for_dev_pasid() - Retrieve domain for @pasid of @dev
+ * @dev: the queried device
+ * @pasid: the pasid of the device
+ * @type: matched domain type, 0 for any match
+ *
+ * This is a variant of iommu_get_domain_for_dev(). It returns the existing
+ * domain attached to pasid of a device. It's only for internal use of the
+ * IOMMU subsystem.

If it is only for external use then why is it exported?

I would add something like:

Callers must hold a lock around this function, and both iommu_attach/detach_dev_pasid() whenever a domain of type is being manipulated. This API does not internally resolve races with attach/detach.

Quoted 8 lines

+ * detaching from the device PASID.
+ *
+ * Return: attached domain on success, NULL otherwise.
+ */

drivers/iommu/iommu.c +4 -0
+struct iommu_domain *iommu_get_domain_for_dev_pasid(struct device *dev,
+						    ioasid_t pasid,
+						    unsigned int type)
+{

drivers/iommu/iommu.c bool iommu_group_dma_owner_claimed(struct iommu_group *group) · line 3376

Quoted 10 lines
drivers/iommu/iommu.c +10 -0
+	struct iommu_domain *domain;
+	struct iommu_group *group;
+
+	group = iommu_group_get(dev);
+	if (!group)
+		return NULL;
+	/*
+	 * The xarray protects its internal state with RCU. Hence the domain
+	 * obtained is either NULL or fully formed.
+	 */

This has nothing to do with RCU

xa_lock() is used to ensure that the domain pointer remains valid while we check the type since it blocks concurrent xa_erase().

Jason

author maintainer
#8 lore raw

Re: [PATCH v12 05/17] iommu: Add attach/detach_dev_pasid iommu interface

Quoted 11 lines · Jason Gunthorpe
Quoted 8 lines · Lu Baolu

+ * iommu_get_domain_for_dev_pasid() - Retrieve domain for @pasid of @dev
+ * @dev: the queried device
+ * @pasid: the pasid of the device
+ * @type: matched domain type, 0 for any match
+ *
+ * This is a variant of iommu_get_domain_for_dev(). It returns the existing
+ * domain attached to pasid of a device. It's only for internal use of the
+ * IOMMU subsystem.

Quoted 8 lines

If it is only for external use then why is it exported?

I would add something like:

Callers must hold a lock around this function, and both iommu_attach/detach_dev_pasid() whenever a domain of type is being manipulated. This API does not internally resolve races with attach/detach.

Yes. Updated. This is what this API expected.

Quoted 22 lines
Quoted 9 lines

+ * detaching from the device PASID.
+ *
+ * Return: attached domain on success, NULL otherwise.
+ */

drivers/iommu/iommu.c +4 -0
+struct iommu_domain *iommu_get_domain_for_dev_pasid(struct device *dev,
+						    ioasid_t pasid,
+						    unsigned int type)
+{

drivers/iommu/iommu.c bool iommu_group_dma_owner_claimed(struct iommu_group *group) · line 3376

Quoted 10 lines
drivers/iommu/iommu.c +10 -0
+	struct iommu_domain *domain;
+	struct iommu_group *group;
+
+	group = iommu_group_get(dev);
+	if (!group)
+		return NULL;
+	/*
+	 * The xarray protects its internal state with RCU. Hence the domain
+	 * obtained is either NULL or fully formed.
+	 */

This has nothing to do with RCU

Quoted 2 lines

xa_lock() is used to ensure that the domain pointer remains valid while we check the type since it blocks concurrent xa_erase().

With xa_lock() added, this comment is not needed anymore.

Best regards,
baolu

author maintainer
#9 lore raw

PATCH v12 6/17 iommu: Add IOMMU SVA domain support

In reply to Lu Baolu

The sva iommu_domain represents a hardware pagetable that the IOMMU hardware could use for SVA translation. This adds some infrastructure to support SVA domain in the iommu common layer. It includes:

- Extend the iommu_domain to support a new IOMMU_DOMAIN_SVA domain type. The IOMMU drivers that support allocation of the SVA domain should provide its own sva domain specific iommu_domain_ops.
- Add a helper to allocate an SVA domain. The iommu_domain_free() is still used to free an SVA domain.

The report_iommu_fault() should be replaced by the new iommu_report_device_fault(). Leave the existing fault handler with the existing users and the newly added SVA members excludes it.

---
 include/linux/iommu.h | 25 +++++++++++++++++++++++--
 drivers/iommu/iommu.c | 20 ++++++++++++++++++++
 2 files changed, 43 insertions(+), 2 deletions(-)
include/linux/iommu.h +23 -2
index 5e9553a7de0d..836aa480961c 100644
@@ -64,6 +64,8 @@ struct iommu_domain_geometry {
 #define __IOMMU_DOMAIN_PT	(1U << 2)  /* Domain is identity mapped   */
 #define __IOMMU_DOMAIN_DMA_FQ	(1U << 3)  /* DMA-API uses flush queue    */
 
+#define __IOMMU_DOMAIN_SVA	(1U << 4)  /* Shared process address space */
+
 /*
  * This are the possible domain-types
  *
@@ -77,6 +79,8 @@ struct iommu_domain_geometry {
  *				  certain optimizations for these domains
  *	IOMMU_DOMAIN_DMA_FQ	- As above, but definitely using batched TLB
  *				  invalidation.
+ *	IOMMU_DOMAIN_SVA	- DMA addresses are shared process addresses
+ *				  represented by mm_struct's.
  */
 #define IOMMU_DOMAIN_BLOCKED	(0U)
 #define IOMMU_DOMAIN_IDENTITY	(__IOMMU_DOMAIN_PT)
@@ -86,15 +90,24 @@ struct iommu_domain_geometry {
 #define IOMMU_DOMAIN_DMA_FQ	(__IOMMU_DOMAIN_PAGING |	\
 				 __IOMMU_DOMAIN_DMA_API |	\
 				 __IOMMU_DOMAIN_DMA_FQ)
+#define IOMMU_DOMAIN_SVA	(__IOMMU_DOMAIN_SVA)
 
 struct iommu_domain {
 	unsigned type;
 	const struct iommu_domain_ops *ops;
 	unsigned long pgsize_bitmap;	/* Bitmap of page sizes in use */
-	iommu_fault_handler_t handler;
-	void *handler_token;
 	struct iommu_domain_geometry geometry;
 	struct iommu_dma_cookie *iova_cookie;
+	union {
+		struct {
+			iommu_fault_handler_t handler;
+			void *handler_token;
+		};
+		struct {	/* IOMMU_DOMAIN_SVA */
+			struct mm_struct *mm;
+			int users;
+		};
+	};
 };
 
 static inline bool iommu_is_dma_domain(struct iommu_domain *domain)
@@ -683,6 +696,8 @@ int iommu_group_claim_dma_owner(struct iommu_group *group, void *owner);
 void iommu_group_release_dma_owner(struct iommu_group *group);
 bool iommu_group_dma_owner_claimed(struct iommu_group *group);
 
+struct iommu_domain *iommu_sva_domain_alloc(struct device *dev,
+					    struct mm_struct *mm);
 int iommu_attach_device_pasid(struct iommu_domain *domain,
 			      struct device *dev, ioasid_t pasid);
 void iommu_detach_device_pasid(struct iommu_domain *domain,
@@ -1058,6 +1073,12 @@ static inline bool iommu_group_dma_owner_claimed(struct iommu_group *group)
 	return false;
 }
 
+static inline struct iommu_domain *
+iommu_sva_domain_alloc(struct device *dev, struct mm_struct *mm)
+{
+	return NULL;
+}
+
 static inline int iommu_attach_device_pasid(struct iommu_domain *domain,
 					    struct device *dev, ioasid_t pasid)
 {
drivers/iommu/iommu.c +20 -0
index 26aca46193c3..2c9488d966ab 100644
@@ -27,6 +27,7 @@
 #include <linux/module.h>
 #include <linux/cc_platform.h>
 #include <trace/events/iommu.h>
+#include <linux/sched/mm.h>
 
 static struct kset *iommu_group_kset;
 static DEFINE_IDA(iommu_group_ida);
@@ -1960,6 +1961,8 @@ EXPORT_SYMBOL_GPL(iommu_domain_alloc);
 
 void iommu_domain_free(struct iommu_domain *domain)
 {
+	if (domain->type == IOMMU_DOMAIN_SVA)
+		mmdrop(domain->mm);
 	iommu_put_dma_cookie(domain);
 	domain->ops->free(domain);
 }
@@ -3384,3 +3387,20 @@ struct iommu_domain *iommu_get_domain_for_dev_pasid(struct device *dev,
 	return domain;
 }
 EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev_pasid);
+
+struct iommu_domain *iommu_sva_domain_alloc(struct device *dev,
+					    struct mm_struct *mm)
+{
+	const struct iommu_ops *ops = dev_iommu_ops(dev);
+	struct iommu_domain *domain;
+
+	domain = ops->domain_alloc(IOMMU_DOMAIN_SVA);
+	if (!domain)
+		return NULL;
+
+	domain->type = IOMMU_DOMAIN_SVA;
+	mmgrab(mm);
+	domain->mm = mm;
+
+	return domain;
+}
Signature · 1 line
2.25.1
author maintainer
#10 lore raw

PATCH v12 7/17 iommu: Try to allocate blocking domain when probing device

In reply to Lu Baolu

Allocate the blocking domain when probing devices if the driver supports blocking domain allocation. Otherwise, revert to the previous behavior, that is, use UNMANAGED domain instead when the blocking domain is needed.

---
 drivers/iommu/iommu.c | 29 +++++++++++++++++------------
 1 file changed, 17 insertions(+), 12 deletions(-)
drivers/iommu/iommu.c +17 -12
index 2c9488d966ab..e985f4d5895f 100644
@@ -318,6 +318,10 @@ int iommu_probe_device(struct device *dev)
 	mutex_lock(&group->mutex);
 	iommu_alloc_default_domain(group, dev);
 
+	/* Try to allocate a blocking domain */
+	group->blocking_domain = __iommu_domain_alloc(dev->bus,
+						      IOMMU_DOMAIN_BLOCKED);
+
 	/*
 	 * If device joined an existing group which has been claimed, don't
 	 * attach the default domain.
@@ -1778,6 +1782,10 @@ int bus_iommu_probe(struct bus_type *bus)
 		/* Try to allocate default domain */
 		probe_alloc_default_domain(bus, group);
 
+		/* Try to allocate blocking domain */
+		group->blocking_domain =
+				__iommu_domain_alloc(bus, IOMMU_DOMAIN_BLOCKED);
+
 		if (!group->default_domain) {
 			mutex_unlock(&group->mutex);
 			continue;
@@ -3165,18 +3173,15 @@ static int __iommu_group_alloc_blocking_domain(struct iommu_group *group)
 	if (group->blocking_domain)
 		return 0;
 
-	group->blocking_domain =
-		__iommu_domain_alloc(dev->dev->bus, IOMMU_DOMAIN_BLOCKED);
-	if (!group->blocking_domain) {
-		/*
-		 * For drivers that do not yet understand IOMMU_DOMAIN_BLOCKED
-		 * create an empty domain instead.
-		 */
-		group->blocking_domain = __iommu_domain_alloc(
-			dev->dev->bus, IOMMU_DOMAIN_UNMANAGED);
-		if (!group->blocking_domain)
-			return -EINVAL;
-	}
+	/*
+	 * For drivers that do not yet understand IOMMU_DOMAIN_BLOCKED
+	 * create an empty domain instead.
+	 */
+	group->blocking_domain = __iommu_domain_alloc(dev->dev->bus,
+						      IOMMU_DOMAIN_UNMANAGED);
+	if (!group->blocking_domain)
+		return -EINVAL;
+
 	return 0;
 }
 
Signature · 1 line
2.25.1
#11 lore raw

Re: [PATCH v12 07/17] iommu: Try to allocate blocking domain when probing device

Quoted 10 lines · Lu Baolu

Allocate the blocking domain when probing devices if the driver supports blocking domain allocation. Otherwise, revert to the previous behavior, that is, use UNMANAGED domain instead when the blocking domain is needed.

---
 drivers/iommu/iommu.c | 29 +++++++++++++++++------------
 1 file changed, 17 insertions(+), 12 deletions(-)

This seems like a lot of overhead to allocate these things for every group?

Why not add a simple refcount on the blocking domain instead and allocate the domain on the pasid attach like we do for ownership?

Jason

author maintainer
#12 lore raw

Re: [PATCH v12 07/17] iommu: Try to allocate blocking domain when probing device

Quoted 11 lines · Jason Gunthorpe
Quoted 10 lines · Lu Baolu

Allocate the blocking domain when probing devices if the driver supports blocking domain allocation. Otherwise, revert to the previous behavior, that is, use UNMANAGED domain instead when the blocking domain is needed.

---
  drivers/iommu/iommu.c | 29 +++++++++++++++++------------
  1 file changed, 17 insertions(+), 12 deletions(-)
Quoted 5 lines

This seems like a lot of overhead to allocate these things for every group?

Why not add a simple refcount on the blocking domain instead and allocate the domain on the pasid attach like we do for ownership?

I am working towards implementing static instance of blocking domain for each IOMMU driver, and then, there's no much overhead to allocate it in the probing device path.

Best regards,
baolu

#13 lore raw

Re: [PATCH v12 07/17] iommu: Try to allocate blocking domain when probing device

Quoted 15 lines · Baolu Lu
Quoted 14 lines · Jason Gunthorpe
Quoted 10 lines · Lu Baolu

Allocate the blocking domain when probing devices if the driver supports blocking domain allocation. Otherwise, revert to the previous behavior, that is, use UNMANAGED domain instead when the blocking domain is needed.

---
  drivers/iommu/iommu.c | 29 +++++++++++++++++------------
  1 file changed, 17 insertions(+), 12 deletions(-)

This seems like a lot of overhead to allocate these things for every group?

Quoted 6 lines
Quoted 2 lines

Why not add a simple refcount on the blocking domain instead and allocate the domain on the pasid attach like we do for ownership?

I am working towards implementing static instance of blocking domain for each IOMMU driver, and then, there's no much overhead to allocate it in the probing device path.

Well, I thought about that and I don't think we can get there in a short order. Would rather you progress this series without getting entangled in such a big adventure

Jason

author maintainer
#14 lore raw

Re: [PATCH v12 07/17] iommu: Try to allocate blocking domain when probing device

Quoted 19 lines · Jason Gunthorpe
Quoted 18 lines · Baolu Lu
Quoted 16 lines · Jason Gunthorpe
Quoted 10 lines · Lu Baolu

Allocate the blocking domain when probing devices if the driver supports blocking domain allocation. Otherwise, revert to the previous behavior, that is, use UNMANAGED domain instead when the blocking domain is needed.

---
   drivers/iommu/iommu.c | 29 +++++++++++++++++------------
   1 file changed, 17 insertions(+), 12 deletions(-)

This seems like a lot of overhead to allocate these things for every group?

Why not add a simple refcount on the blocking domain instead and allocate the domain on the pasid attach like we do for ownership?

Quoted 6 lines
Quoted 3 lines

I am working towards implementing static instance of blocking domain for each IOMMU driver, and then, there's no much overhead to allocate it in the probing device path.

Well, I thought about that and I don't think we can get there in a short order.

Yes. Fair enough.

Quoted 2 lines

Would rather you progress this series without
getting entangled in such a big adventure

Agreed. I will drop this patch and add below code in the iommu interface:

drivers/iommu/iommu.c +20 -0
@@ -3219,6 +3219,26 @@ int iommu_attach_device_pasid(struct iommu_domain *domain,
                return -ENODEV;
 
        mutex_lock(&group->mutex);
+
+       /*
+        * The underlying IOMMU driver needs to support blocking domain
+        * allocation and the callback to block DMA transactions with a
+        * specific PASID.
+        */
+       if (!group->blocking_domain) {
+               group->blocking_domain = __iommu_domain_alloc(dev->bus,
+                               IOMMU_DOMAIN_BLOCKED);
+               if (!group->blocking_domain) {
+                       ret = -ENODEV;
+                       goto out_unlock;
+               }
+       }
+
+       if (!group->blocking_domain->ops->set_dev_pasid) {
+               ret = -EOPNOTSUPP;
+               goto out_unlock;
+       }
+
        curr = xa_cmpxchg(&group->pasid_array, pasid, NULL, domain, 
GFP_KERNEL);
        if (curr) {
                ret = xa_err(curr) ? : -EBUSY;

Currently both ARM SMMUv3 and VT-d drivers use static blocking domain. Hence I didn't use a refcount for blocking domain release here.

Best regards,
baolu

#15 lore raw

Re: [PATCH v12 07/17] iommu: Try to allocate blocking domain when probing device

Quoted 67 lines · Baolu Lu
Quoted 19 lines · Jason Gunthorpe
Quoted 18 lines · Baolu Lu
Quoted 16 lines · Jason Gunthorpe
Quoted 10 lines · Lu Baolu

Allocate the blocking domain when probing devices if the driver supports blocking domain allocation. Otherwise, revert to the previous behavior, that is, use UNMANAGED domain instead when the blocking domain is needed.

---
   drivers/iommu/iommu.c | 29 +++++++++++++++++------------
   1 file changed, 17 insertions(+), 12 deletions(-)

This seems like a lot of overhead to allocate these things for every group?

Why not add a simple refcount on the blocking domain instead and allocate the domain on the pasid attach like we do for ownership?

Quoted 6 lines
Quoted 3 lines

I am working towards implementing static instance of blocking domain for each IOMMU driver, and then, there's no much overhead to allocate it in the probing device path.

Well, I thought about that and I don't think we can get there in a short order.

Yes. Fair enough.

Quoted 2 lines

Would rather you progress this series without
getting entangled in such a big adventure

Agreed. I will drop this patch and add below code in the iommu interface:

drivers/iommu/iommu.c +20 -0
@@ -3219,6 +3219,26 @@ int iommu_attach_device_pasid(struct iommu_domain
 *domain,
                return -ENODEV;
 
        mutex_lock(&group->mutex);
+
+       /*
+        * The underlying IOMMU driver needs to support blocking domain
+        * allocation and the callback to block DMA transactions with a
+        * specific PASID.
+        */
+       if (!group->blocking_domain) {
+               group->blocking_domain = __iommu_domain_alloc(dev->bus,
+                               IOMMU_DOMAIN_BLOCKED);
+               if (!group->blocking_domain) {
+                       ret = -ENODEV;
+                       goto out_unlock;
+               }
+       }
+
+       if (!group->blocking_domain->ops->set_dev_pasid) {
+               ret = -EOPNOTSUPP;
+               goto out_unlock;
+       }
+
        curr = xa_cmpxchg(&group->pasid_array, pasid, NULL, domain,
 GFP_KERNEL);
        if (curr) {
                ret = xa_err(curr) ? : -EBUSY;
Quoted 2 lines

Currently both ARM SMMUv3 and VT-d drivers use static blocking domain. Hence I didn't use a refcount for blocking domain release here.

I don't think that works in the general case, you can't just destroy what is in group->blocking_domain..

Maybe all of this is just the good reason to go to a simple device->ops->remove_dev_pasid() callback and forget about blocking domain here.

Jason

author maintainer
#16 lore raw

Re: [PATCH v12 07/17] iommu: Try to allocate blocking domain when probing device

Quoted 68 lines · Jason Gunthorpe
Quoted 67 lines · Baolu Lu
Quoted 19 lines · Jason Gunthorpe
Quoted 18 lines · Baolu Lu
Quoted 16 lines · Jason Gunthorpe
Quoted 10 lines · Lu Baolu

Allocate the blocking domain when probing devices if the driver supports blocking domain allocation. Otherwise, revert to the previous behavior, that is, use UNMANAGED domain instead when the blocking domain is needed.

---
    drivers/iommu/iommu.c | 29 +++++++++++++++++------------
    1 file changed, 17 insertions(+), 12 deletions(-)

This seems like a lot of overhead to allocate these things for every group?

Why not add a simple refcount on the blocking domain instead and allocate the domain on the pasid attach like we do for ownership?

Quoted 6 lines
Quoted 3 lines

I am working towards implementing static instance of blocking domain for each IOMMU driver, and then, there's no much overhead to allocate it in the probing device path.

Well, I thought about that and I don't think we can get there in a short order.

Yes. Fair enough.

Quoted 2 lines

Would rather you progress this series without
getting entangled in such a big adventure

Agreed. I will drop this patch and add below code in the iommu interface:

drivers/iommu/iommu.c +20 -0
@@ -3219,6 +3219,26 @@ int iommu_attach_device_pasid(struct iommu_domain
 *domain,
                 return -ENODEV;
 
         mutex_lock(&group->mutex);
+
+       /*
+        * The underlying IOMMU driver needs to support blocking domain
+        * allocation and the callback to block DMA transactions with a
+        * specific PASID.
+        */
+       if (!group->blocking_domain) {
+               group->blocking_domain = __iommu_domain_alloc(dev->bus,
+                               IOMMU_DOMAIN_BLOCKED);
+               if (!group->blocking_domain) {
+                       ret = -ENODEV;
+                       goto out_unlock;
+               }
+       }
+
+       if (!group->blocking_domain->ops->set_dev_pasid) {
+               ret = -EOPNOTSUPP;
+               goto out_unlock;
+       }
+
         curr = xa_cmpxchg(&group->pasid_array, pasid, NULL, domain,
 GFP_KERNEL);
         if (curr) {
                 ret = xa_err(curr) ? : -EBUSY;
Quoted 5 lines
Quoted 2 lines

Currently both ARM SMMUv3 and VT-d drivers use static blocking domain. Hence I didn't use a refcount for blocking domain release here.

I don't think that works in the general case, you can't just destroy what is in group->blocking_domain..

If I understand you correctly, we can't just free the blocking domain and forget about whether this domain is still set on any device?

Quoted 4 lines

Maybe all of this is just the good reason to go to a simple device->ops->remove_dev_pasid() callback and forget about blocking domain here.

Do you mean rolling back to what we did in v10?

include/linux/iommu.h +2 -0
@@ -262,6 +262,8 @@ struct iommu_ops {
  * struct iommu_domain_ops - domain specific operations
  * @attach_dev: attach an iommu domain to a device
  * @detach_dev: detach an iommu domain from a device
+ * @set_dev_pasid: set an iommu domain to a pasid of device
+ * @block_dev_pasid: block pasid of device from using iommu domain
  * @map: map a physically contiguous memory region to an iommu domain
  * @map_pages: map a physically contiguous set of pages of the same 

size to
* an iommu domain.
@@ -282,6 +284,10 @@ struct iommu_ops {
struct iommu_domain_ops {
int (*attach_dev)(struct iommu_domain *domain, struct device *dev); void (*detach_dev)(struct iommu_domain *domain, struct device
*dev);
+ int (*set_dev_pasid)(struct iommu_domain *domain, struct device *dev,
+ ioasid_t pasid);
+ void (*block_dev_pasid)(struct iommu_domain *domain, struct device *dev,
+ ioasid_t pasid);

Best regards,
baolu

#17 lore raw

Re: [PATCH v12 07/17] iommu: Try to allocate blocking domain when probing device

Quoted 5 lines · Baolu Lu
Quoted 3 lines

Maybe all of this is just the good reason to go to a simple device->ops->remove_dev_pasid() callback and forget about blocking domain here.

Do you mean rolling back to what we did in v10?

Yeah, but it shouldn't be a domain_op, removing a pasid is a device op

Just

remove_dev_pasid(struct device *dev, ioasid_t pasid)

Jason

author maintainer
#18 lore raw

Re: [PATCH v12 07/17] iommu: Try to allocate blocking domain when probing device

Quoted 5 lines · Jason Gunthorpe
Quoted 4 lines · Baolu Lu
Quoted 3 lines

Maybe all of this is just the good reason to go to a simple device->ops->remove_dev_pasid() callback and forget about blocking domain here.

Quoted 7 lines
Quoted 1 line

Do you mean rolling back to what we did in v10?

Yeah, but it shouldn't be a domain_op, removing a pasid is a device op

Just

remove_dev_pasid(struct device *dev, ioasid_t pasid)

It's clear now. Thanks!

How about below iommu_attach/detach_device_pasid() code?

By the way, how about naming it "block_dev_pasid(dev, pasid)"?

+static int __iommu_set_group_pasid(struct iommu_domain *domain,
+ struct iommu_group *group, ioasid_t pasid)
+{
+ struct group_device *device;
+ int ret = 0;
+
+ list_for_each_entry(device, &group->devices, list) {
+ ret = domain->ops->set_dev_pasid(domain, device->dev, pasid);
+ if (ret)
+ break;
+ }
+
+ return ret;
+}
+
+static void __iommu_remove_group_pasid(struct iommu_group *group,
+ ioasid_t pasid)
+{
+ struct group_device *device;
+ const struct iommu_ops *ops;
+
+ list_for_each_entry(device, &group->devices, list) {
+ ops = dev_iommu_ops(device->dev);
+ ops->remove_dev_pasid(device->dev, pasid);
+ }
+}
+
+/*
+ * iommu_attach_device_pasid() - Attach a domain to pasid of device
+ * @domain: the iommu domain.
+ * @dev: the attached device.
+ * @pasid: the pasid of the device.
+ *
+ * Return: 0 on success, or an error.
+ */
+int iommu_attach_device_pasid(struct iommu_domain *domain,
+ struct device *dev, ioasid_t pasid)
+{
+ struct iommu_group *group;
+ void *curr;
+ int ret;
+
+ if (!domain->ops->set_dev_pasid)
+ return -EOPNOTSUPP;
+
+ group = iommu_group_get(dev);
+ if (!group)
+ return -ENODEV;
+
+ mutex_lock(&group->mutex);
+ curr = xa_cmpxchg(&group->pasid_array, pasid, NULL, domain, GFP_KERNEL);
+ if (curr) {
+ ret = xa_err(curr) ? : -EBUSY;
+ goto out_unlock;
+ }
+
+ ret = __iommu_set_group_pasid(domain, group, pasid);
+ if (ret) {
+ __iommu_remove_group_pasid(group, pasid);
+ xa_erase(&group->pasid_array, pasid);
+ }
+out_unlock:
+ mutex_unlock(&group->mutex);
+ iommu_group_put(group);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(iommu_attach_device_pasid);
+
+/*
+ * iommu_detach_device_pasid() - Detach the domain from pasid of device
+ * @domain: the iommu domain.
+ * @dev: the attached device.
+ * @pasid: the pasid of the device.
+ *
+ * The @domain must have been attached to @pasid of the @dev with
+ * iommu_attach_device_pasid().
+ */
+void iommu_detach_device_pasid(struct iommu_domain *domain, struct device *dev,
+ ioasid_t pasid)
+{
+ struct iommu_group *group = iommu_group_get(dev);
+
+ mutex_lock(&group->mutex);
+ __iommu_remove_group_pasid(group, pasid);
+ WARN_ON(xa_erase(&group->pasid_array, pasid) != domain);
+ mutex_unlock(&group->mutex);
+
+ iommu_group_put(group);
+}
+EXPORT_SYMBOL_GPL(iommu_detach_device_pasid);

+ * @remove_dev_pasid: Remove any translation configurations of a specific
+ * pasid, so that any DMA transactions with this pasid
+ * will be blocked by the hardware.
* @pgsize_bitmap: bitmap of all possible supported page sizes
* @owner: Driver module providing these ops
*/
@@ -256,6 +259,7 @@ struct iommu_ops {
struct iommu_page_response *msg);

int (*def_domain_type)(struct device *dev);
+ void (*remove_dev_pasid)(struct device *dev, ioasid_t pasid);

Best regards,
baolu

#19 lore raw

Re: [PATCH v12 07/17] iommu: Try to allocate blocking domain when probing device

Quoted 10 lines · Baolu Lu
Quoted 9 lines · Jason Gunthorpe
Quoted 5 lines · Baolu Lu
Quoted 3 lines

Maybe all of this is just the good reason to go to a simple device->ops->remove_dev_pasid() callback and forget about blocking domain here.

Do you mean rolling back to what we did in v10?

Yeah, but it shouldn't be a domain_op, removing a pasid is a device op

Quoted 7 lines
Quoted 3 lines

Just

remove_dev_pasid(struct device *dev, ioasid_t pasid)

It's clear now. Thanks!

How about below iommu_attach/detach_device_pasid() code?

I think this is probably the right thing

Quoted 1 line

By the way, how about naming it "block_dev_pasid(dev, pasid)"?

set/remove is a better pairing that set/block

Jason

author maintainer
#20 lore raw

PATCH v12 8/17 iommu: Make free of iommu_domain_ops optional

In reply to Lu Baolu

So that the drivers are able to implement static singleton instance for some special domains. The blocking domain is an example. The driver has no need to allocate different memory for each blocking domain allocation request. Hence, no need for a free callback.

---
 drivers/iommu/iommu.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iommu/iommu.c +2 -1
index e985f4d5895f..630e1b64ed89 100644
@@ -1972,7 +1972,8 @@ void iommu_domain_free(struct iommu_domain *domain)
 	if (domain->type == IOMMU_DOMAIN_SVA)
 		mmdrop(domain->mm);
 	iommu_put_dma_cookie(domain);
-	domain->ops->free(domain);
+	if (domain->ops->free)
+		domain->ops->free(domain);
 }
 EXPORT_SYMBOL_GPL(iommu_domain_free);
 
Signature · 1 line
2.25.1
Reviewed-by on v12 8/17
#21 lore raw

Re: [PATCH v12 08/17] iommu: Make free of iommu_domain_ops optional

Quoted 17 lines · Lu Baolu

So that the drivers are able to implement static singleton instance for some special domains. The blocking domain is an example. The driver has no need to allocate different memory for each blocking domain allocation request. Hence, no need for a free callback.

---
 drivers/iommu/iommu.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iommu/iommu.c +0 -0
index e985f4d5895f..630e1b64ed89 100644

drivers/iommu/iommu.c void iommu_domain_free(struct iommu_domain *domain) · line 1978

Quoted 8 lines
drivers/iommu/iommu.c +2 -1
@@ -1972,7 +1972,8 @@ void iommu_domain_free(struct iommu_domain *domain)
 	if (domain->type == IOMMU_DOMAIN_SVA)
 		mmdrop(domain->mm);
 	iommu_put_dma_cookie(domain);
-	domain->ops->free(domain);
+	if (domain->ops->free)
+		domain->ops->free(domain);
 }
 EXPORT_SYMBOL_GPL(iommu_domain_free);

This patch is in the wrong order, it needs to be before the ARM patch implementing the blocking domain

But looks OK

Jason

#22 lore raw

Re: [PATCH v12 08/17] iommu: Make free of iommu_domain_ops optional

Quoted 13 lines · Jason Gunthorpe
Quoted 12 lines · Lu Baolu

So that the drivers are able to implement static singleton instance for some special domains. The blocking domain is an example. The driver has no need to allocate different memory for each blocking domain allocation request. Hence, no need for a free callback.

---
 drivers/iommu/iommu.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 16 lines

drivers/iommu/iommu.c void iommu_domain_free(struct iommu_domain *domain) · line 1978

Quoted 13 lines
drivers/iommu/iommu.c +2 -1
index e985f4d5895f..630e1b64ed89 100644
@@ -1972,7 +1972,8 @@ void iommu_domain_free(struct iommu_domain *domain)
 	if (domain->type == IOMMU_DOMAIN_SVA)
 		mmdrop(domain->mm);
 	iommu_put_dma_cookie(domain);
-	domain->ops->free(domain);
+	if (domain->ops->free)
+		domain->ops->free(domain);
 }
 EXPORT_SYMBOL_GPL(iommu_domain_free);

This patch is in the wrong order, it needs to be before the ARM patch implementing the blocking domain

Oh, nevermind, I am reading them in the wrong order :)

Jason

author maintainer
#23 lore raw

PATCH v12 9/17 iommu/vt-d: Add blocking domain support

In reply to Lu Baolu

The Intel IOMMU hardwares support blocking DMA transactions by clearing the translation table entries. This implements a real blocking domain to avoid using an empty UNMANAGED domain. The detach_dev callback of the domain ops is not used in any path. Remove it to avoid dead code as well.

---
 drivers/iommu/intel/iommu.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)
drivers/iommu/intel/iommu.c +18 -7
index 64d30895a4c8..ffd1ff69bf25 100644
@@ -4127,12 +4127,30 @@ static int md_domain_init(struct dmar_domain *domain, int guest_width)
 	return 0;
 }
 
+/*
+ * Intel IOMMU blocking domain support:
+ */
+static int blocking_domain_attach_dev(struct iommu_domain *domain,
+				      struct device *dev)
+{
+	dmar_remove_one_dev_info(dev);
+	return 0;
+}
+
+static struct iommu_domain blocking_domain = {
+	.ops = &(const struct iommu_domain_ops) {
+		.attach_dev	= blocking_domain_attach_dev
+	}
+};
+
 static struct iommu_domain *intel_iommu_domain_alloc(unsigned type)
 {
 	struct dmar_domain *dmar_domain;
 	struct iommu_domain *domain;
 
 	switch (type) {
+	case IOMMU_DOMAIN_BLOCKED:
+		return &blocking_domain;
 	case IOMMU_DOMAIN_DMA:
 	case IOMMU_DOMAIN_DMA_FQ:
 	case IOMMU_DOMAIN_UNMANAGED:
@@ -4239,12 +4257,6 @@ static int intel_iommu_attach_device(struct iommu_domain *domain,
 	return domain_add_dev_info(to_dmar_domain(domain), dev);
 }
 
-static void intel_iommu_detach_device(struct iommu_domain *domain,
-				      struct device *dev)
-{
-	dmar_remove_one_dev_info(dev);
-}
-
 static int intel_iommu_map(struct iommu_domain *domain,
 			   unsigned long iova, phys_addr_t hpa,
 			   size_t size, int iommu_prot, gfp_t gfp)
@@ -4767,7 +4779,6 @@ const struct iommu_ops intel_iommu_ops = {
 #endif
 	.default_domain_ops = &(const struct iommu_domain_ops) {
 		.attach_dev		= intel_iommu_attach_device,
-		.detach_dev		= intel_iommu_detach_device,
 		.map_pages		= intel_iommu_map_pages,
 		.unmap_pages		= intel_iommu_unmap_pages,
 		.iotlb_sync_map		= intel_iommu_iotlb_sync_map,
Signature · 1 line
2.25.1
#24 lore raw

Re: [PATCH v12 09/17] iommu/vt-d: Add blocking domain support

Quoted 10 lines · Lu Baolu

The Intel IOMMU hardwares support blocking DMA transactions by clearing the translation table entries. This implements a real blocking domain to avoid using an empty UNMANAGED domain. The detach_dev callback of the domain ops is not used in any path. Remove it to avoid dead code as well.

---
 drivers/iommu/intel/iommu.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

At this point I'd suggest to make a little series that does all the blocking domain stuff in one shot

The removal of detach_dev callbacks is pretty nice for clarity.

Jason

author maintainer
#25 lore raw

Re: [PATCH v12 09/17] iommu/vt-d: Add blocking domain support

Quoted 12 lines · Jason Gunthorpe
Quoted 10 lines · Lu Baolu

The Intel IOMMU hardwares support blocking DMA transactions by clearing the translation table entries. This implements a real blocking domain to avoid using an empty UNMANAGED domain. The detach_dev callback of the domain ops is not used in any path. Remove it to avoid dead code as well.

---
  drivers/iommu/intel/iommu.c | 25 ++++++++++++++++++-------
  1 file changed, 18 insertions(+), 7 deletions(-)
Quoted 4 lines

At this point I'd suggest to make a little series that does all the blocking domain stuff in one shot

The removal of detach_dev callbacks is pretty nice for clarity.

Yes. As you saw, I have a separated series for above purpose.

Best regards,
baolu

author maintainer
#26 lore raw

PATCH v12 10/17 iommu/vt-d: Add SVA domain support

In reply to Lu Baolu

Add support for SVA domain allocation and provide an SVA-specific iommu_domain_ops. This implementation is based on the existing SVA code. Possible cleanup and refactoring are left for incremental changes later.

---
 drivers/iommu/intel/iommu.h |  9 +++++++
 drivers/iommu/intel/iommu.c | 20 +++++++++++++++-
 drivers/iommu/intel/svm.c   | 47 +++++++++++++++++++++++++++++++++++++
 3 files changed, 75 insertions(+), 1 deletion(-)
drivers/iommu/intel/iommu.h +9 -0
index 91312aabdd53..4ab535ec6191 100644
@@ -751,6 +751,8 @@ void intel_svm_unbind(struct iommu_sva *handle);
 u32 intel_svm_get_pasid(struct iommu_sva *handle);
 int intel_svm_page_response(struct device *dev, struct iommu_fault_event *evt,
 			    struct iommu_page_response *msg);
+struct iommu_domain *intel_svm_domain_alloc(void);
+void intel_svm_block_dev_pasid(struct device *dev, ioasid_t pasid);
 
 struct intel_svm_dev {
 	struct list_head list;
@@ -776,6 +778,13 @@ struct intel_svm {
 };
 #else
 static inline void intel_svm_check(struct intel_iommu *iommu) {}
+static inline struct iommu_domain *intel_svm_domain_alloc(void)
+{
+	return NULL;
+}
+static inline void intel_svm_block_dev_pasid(struct device *dev, ioasid_t pasid)
+{
+}
 #endif
 
 #ifdef CONFIG_INTEL_IOMMU_DEBUGFS
drivers/iommu/intel/iommu.c +19 -1
index ffd1ff69bf25..d242911f0a88 100644
@@ -4137,9 +4137,25 @@ static int blocking_domain_attach_dev(struct iommu_domain *domain,
 	return 0;
 }
 
+static int blocking_domain_set_dev_pasid(struct iommu_domain *_domain,
+					 struct device *dev, ioasid_t pasid)
+{
+	struct iommu_domain *domain;
+
+	domain = iommu_get_domain_for_dev_pasid(dev, pasid, IOMMU_DOMAIN_SVA);
+	if (IS_ERR(domain))
+		return PTR_ERR(domain);
+
+	if (domain)
+		intel_svm_block_dev_pasid(dev, pasid);
+
+	return 0;
+}
+
 static struct iommu_domain blocking_domain = {
 	.ops = &(const struct iommu_domain_ops) {
-		.attach_dev	= blocking_domain_attach_dev
+		.attach_dev	= blocking_domain_attach_dev,
+		.set_dev_pasid	= blocking_domain_set_dev_pasid,
 	}
 };
 
@@ -4174,6 +4190,8 @@ static struct iommu_domain *intel_iommu_domain_alloc(unsigned type)
 		return domain;
 	case IOMMU_DOMAIN_IDENTITY:
 		return &si_domain->domain;
+	case IOMMU_DOMAIN_SVA:
+		return intel_svm_domain_alloc();
 	default:
 		return NULL;
 	}
drivers/iommu/intel/svm.c +47 -0

50 lines not on this page · raw message

Signature · 1 line
2.25.1
author maintainer
#27 lore raw

PATCH v12 11/17 arm-smmu-v3: Add blocking domain support

In reply to Lu Baolu

The ARM SMMUv3 hardwares support blocking DMA transactions by clearing the translation table entries. This implements a real blocking domain to avoid using an empty UNMANAGED domain.

---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +20 -0
index f88541be8213..5520a9607758 100644
@@ -88,6 +88,8 @@ static struct arm_smmu_option_prop arm_smmu_options[] = {
 	{ 0, NULL},
 };
 
+static void arm_smmu_detach_dev(struct arm_smmu_master *master);
+
 static void parse_driver_options(struct arm_smmu_device *smmu)
 {
 	int i = 0;
@@ -2004,10 +2006,28 @@ static bool arm_smmu_capable(enum iommu_cap cap)
 	}
 }
 
+static int blocking_domain_attach_dev(struct iommu_domain *domain,
+				      struct device *dev)
+{
+	struct arm_smmu_master *master = dev_iommu_priv_get(dev);
+
+	arm_smmu_detach_dev(master);
+	return 0;
+}
+
+static struct iommu_domain blocking_domain = {
+	.ops = &(const struct iommu_domain_ops) {
+		.attach_dev	= blocking_domain_attach_dev
+	}
+};
+
 static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
 {
 	struct arm_smmu_domain *smmu_domain;
 
+	if (type == IOMMU_DOMAIN_BLOCKED)
+		return &blocking_domain;
+
 	if (type != IOMMU_DOMAIN_UNMANAGED &&
 	    type != IOMMU_DOMAIN_DMA &&
 	    type != IOMMU_DOMAIN_DMA_FQ &&
Signature · 1 line
2.25.1
Reviewed-by on v12 11/17
#28 lore raw

Re: [PATCH v12 11/17] arm-smmu-v3: Add blocking domain support

Quoted 9 lines · Lu Baolu

The ARM SMMUv3 hardwares support blocking DMA transactions by clearing the translation table entries. This implements a real blocking domain to avoid using an empty UNMANAGED domain.

---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

IMHO I'd send this separately since it optimizes VFIO today

Looks OK though

Jason

author maintainer
#29 lore raw

PATCH v12 12/17 arm-smmu-v3/sva: Add SVA domain support

In reply to Lu Baolu

Add support for SVA domain allocation and provide an SVA-specific iommu_domain_ops. This implementation is based on the existing SVA code. Possible cleanup and refactoring are left for incremental changes later.

---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h   | 13 ++++
 .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c   | 61 +++++++++++++++++++
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c   | 21 ++++++-
 3 files changed, 94 insertions(+), 1 deletion(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +13 -0
index d2ba86470c42..c9544b656756 100644
@@ -758,6 +758,9 @@ struct iommu_sva *arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm);
 void arm_smmu_sva_unbind(struct iommu_sva *handle);
 u32 arm_smmu_sva_get_pasid(struct iommu_sva *handle);
 void arm_smmu_sva_notifier_synchronize(void);
+struct iommu_domain *arm_smmu_sva_domain_alloc(void);
+void arm_smmu_sva_block_dev_pasid(struct iommu_domain *domain,
+				  struct device *dev, ioasid_t id);
 #else /* CONFIG_ARM_SMMU_V3_SVA */
 static inline bool arm_smmu_sva_supported(struct arm_smmu_device *smmu)
 {
@@ -803,5 +806,15 @@ static inline u32 arm_smmu_sva_get_pasid(struct iommu_sva *handle)
 }
 
 static inline void arm_smmu_sva_notifier_synchronize(void) {}
+
+static inline struct iommu_domain *arm_smmu_sva_domain_alloc(void)
+{
+	return NULL;
+}
+
+static inline void arm_smmu_sva_block_dev_pasid(struct iommu_domain *domain,
+						struct device *dev, ioasid_t id)
+{
+}
 #endif /* CONFIG_ARM_SMMU_V3_SVA */
 #endif /* _ARM_SMMU_V3_H */
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +61 -0

64 lines not on this page · raw message

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +20 -1
index 5520a9607758..a27ce693cc65 100644
@@ -2015,9 +2015,25 @@ static int blocking_domain_attach_dev(struct iommu_domain *domain,
 	return 0;
 }
 
+static int blocking_domain_set_dev_pasid(struct iommu_domain *_domain,
+					 struct device *dev, ioasid_t pasid)
+{
+	struct iommu_domain *domain;
+
+	domain = iommu_get_domain_for_dev_pasid(dev, pasid, IOMMU_DOMAIN_SVA);
+	if (IS_ERR(domain))
+		return PTR_ERR(domain);
+
+	if (domain)
+		arm_smmu_sva_block_dev_pasid(domain, dev, pasid);
+
+	return 0;
+}
+
 static struct iommu_domain blocking_domain = {
 	.ops = &(const struct iommu_domain_ops) {
-		.attach_dev	= blocking_domain_attach_dev
+		.attach_dev	= blocking_domain_attach_dev,
+		.set_dev_pasid	= blocking_domain_set_dev_pasid
 	}
 };
 
@@ -2028,6 +2044,9 @@ static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
 	if (type == IOMMU_DOMAIN_BLOCKED)
 		return &blocking_domain;
 
+	if (type == IOMMU_DOMAIN_SVA)
+		return arm_smmu_sva_domain_alloc();
+
 	if (type != IOMMU_DOMAIN_UNMANAGED &&
 	    type != IOMMU_DOMAIN_DMA &&
 	    type != IOMMU_DOMAIN_DMA_FQ &&
Signature · 1 line
2.25.1
#30 lore raw

Re: [PATCH v12 12/17] arm-smmu-v3/sva: Add SVA domain support

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c void arm_smmu_sva_notifier_synchronize(void) · line 598

Quoted 2 lines · Lu Baolu
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +2 -0
+static const struct iommu_domain_ops arm_smmu_sva_domain_ops = {
+	.set_dev_pasid		= arm_smmu_sva_set_dev_pasid,

Do we want to permit drivers to not allow a SVA domain to be set on a RID?

It seems like a weird restriction to me

Jason

author maintainer
#31 lore raw

Re: [PATCH v12 12/17] arm-smmu-v3/sva: Add SVA domain support

Quoted 8 lines · Jason Gunthorpe

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c void arm_smmu_sva_notifier_synchronize(void) · line 598

Quoted 2 lines · Lu Baolu
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +2 -0
+static const struct iommu_domain_ops arm_smmu_sva_domain_ops = {
+	.set_dev_pasid		= arm_smmu_sva_set_dev_pasid,

Do we want to permit drivers to not allow a SVA domain to be set on a RID?

It seems like a weird restriction to me

Conceptually as long as the page table is compatible and user pages are pinned (or I/O page fault is supported), the device drivers are valid to set SVA domain to a RID. But I don't see a real use case as far as I can see.

A reasonable use case is sharing EPT between KVM and IOMMU. That demands a new type of domain and implements its own .set_dev for page table attachment.

Best regards,
baolu

#32 lore raw

Re: [PATCH v12 12/17] arm-smmu-v3/sva: Add SVA domain support

Quoted 8 lines · Baolu Lu
Quoted 7 lines · Jason Gunthorpe

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c void arm_smmu_sva_notifier_synchronize(void) · line 598

Quoted 2 lines · Lu Baolu
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +2 -0
+static const struct iommu_domain_ops arm_smmu_sva_domain_ops = {
+	.set_dev_pasid		= arm_smmu_sva_set_dev_pasid,

Do we want to permit drivers to not allow a SVA domain to be set on a RID?

Quoted 6 lines
Quoted 1 line

It seems like a weird restriction to me

Conceptually as long as the page table is compatible and user pages are pinned (or I/O page fault is supported), the device drivers are valid to set SVA domain to a RID. But I don't see a real use case as far as I can see.

It may be interesting for something like DPDK type applications where having the entire process address space mapped SVA to the device could be quite nice.

You, currently, give up interrupts, but perhaps that is solvable in some way.

So, IDK.. I wouldn't dismiss it entirely but I wouldn't do a bunch of work to support it either.

Quoted 3 lines

A reasonable use case is sharing EPT between KVM and IOMMU. That demands a new type of domain and implements its own .set_dev for page table attachment.

Not everything is virtualization :)

Jason

author maintainer
#33 lore raw

Re: [PATCH v12 12/17] arm-smmu-v3/sva: Add SVA domain support

Quoted 18 lines · Jason Gunthorpe
Quoted 8 lines · Baolu Lu
Quoted 7 lines · Jason Gunthorpe

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c void arm_smmu_sva_notifier_synchronize(void) · line 598

Quoted 2 lines · Lu Baolu
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +2 -0
+static const struct iommu_domain_ops arm_smmu_sva_domain_ops = {
+	.set_dev_pasid		= arm_smmu_sva_set_dev_pasid,

Do we want to permit drivers to not allow a SVA domain to be set on a RID?

Quoted 5 lines
Quoted 1 line

It seems like a weird restriction to me

Conceptually as long as the page table is compatible and user pages are pinned (or I/O page fault is supported), the device drivers are valid to set SVA domain to a RID. But I don't see a real use case as far as I can see.

It may be interesting for something like DPDK type applications where having the entire process address space mapped SVA to the device could be quite nice.

Quoted 5 lines

You, currently, give up interrupts, but perhaps that is solvable in some way.

So, IDK.. I wouldn't dismiss it entirely but I wouldn't do a bunch of work to support it either.

Then we can do this through the set_dev callback, as it's the right callback to set a domain to the RID, right? Not sure whether it worth a new type of domain. The current implementation doesn't prevent us from achieving this in the future anyway.

Quoted 5 lines
Quoted 4 lines

A reasonable use case is sharing EPT between KVM and IOMMU. That demands a new type of domain and implements its own .set_dev for page table attachment.

Not everything is virtualization:)

Yes. Fair enough. :-)

Best regards,
baolu

author maintainer
#34 lore raw

PATCH v12 13/17 iommu/sva: Refactoring iommu_sva_bind/unbind_device()

In reply to Lu Baolu

The existing iommu SVA interfaces are implemented by calling the SVA specific iommu ops provided by the IOMMU drivers. There's no need for any SVA specific ops in iommu_ops vector anymore as we can achieve this through the generic attach/detach_dev_pasid domain ops.

This refactors the IOMMU SVA interfaces implementation by using the iommu_detach/detach_device_pasid interfaces and align them with the concept of the SVA iommu domain. Put the new SVA code in the SVA related file in order to make it self-contained.

---
 include/linux/iommu.h         |  43 ++++++-------
 drivers/iommu/iommu-sva-lib.c | 110 ++++++++++++++++++++++++++++++++++
 drivers/iommu/iommu.c         |  91 ----------------------------
 3 files changed, 133 insertions(+), 111 deletions(-)
include/linux/iommu.h +23 -20
index 836aa480961c..5b1e0f81eebb 100644
@@ -643,6 +643,7 @@ struct iommu_fwspec {
  */
 struct iommu_sva {
 	struct device			*dev;
+	struct iommu_domain		*domain;
 };
 
 int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
@@ -684,11 +685,6 @@ void iommu_release_device(struct device *dev);
 int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features f);
 int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features f);
 
-struct iommu_sva *iommu_sva_bind_device(struct device *dev,
-					struct mm_struct *mm);
-void iommu_sva_unbind_device(struct iommu_sva *handle);
-u32 iommu_sva_get_pasid(struct iommu_sva *handle);
-
 int iommu_device_use_default_domain(struct device *dev);
 void iommu_device_unuse_default_domain(struct device *dev);
 
@@ -1029,21 +1025,6 @@ iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
 	return -ENODEV;
 }
 
-static inline struct iommu_sva *
-iommu_sva_bind_device(struct device *dev, struct mm_struct *mm)
-{
-	return NULL;
-}
-
-static inline void iommu_sva_unbind_device(struct iommu_sva *handle)
-{
-}
-
-static inline u32 iommu_sva_get_pasid(struct iommu_sva *handle)
-{
-	return IOMMU_PASID_INVALID;
-}
-
 static inline struct iommu_fwspec *dev_iommu_fwspec_get(struct device *dev)
 {
 	return NULL;
@@ -1121,4 +1102,26 @@ void iommu_debugfs_setup(void);
 static inline void iommu_debugfs_setup(void) {}
 #endif
 
+#ifdef CONFIG_IOMMU_SVA
+struct iommu_sva *iommu_sva_bind_device(struct device *dev,
+					struct mm_struct *mm);
+void iommu_sva_unbind_device(struct iommu_sva *handle);
+u32 iommu_sva_get_pasid(struct iommu_sva *handle);
+#else
+static inline struct iommu_sva *
+iommu_sva_bind_device(struct device *dev, struct mm_struct *mm)
+{
+	return NULL;
+}
+
+static inline void iommu_sva_unbind_device(struct iommu_sva *handle)
+{
+}
+
+static inline u32 iommu_sva_get_pasid(struct iommu_sva *handle)
+{
+	return IOMMU_PASID_INVALID;
+}
+#endif /* CONFIG_IOMMU_SVA */
+
 #endif /* __LINUX_IOMMU_H */
drivers/iommu/iommu-sva-lib.c +110 -0

119 lines not on this page · raw message

drivers/iommu/iommu.c +0 -91

97 lines not on this page · raw message

Signature · 1 line
2.25.1
#35 lore raw

Re: [PATCH v12 13/17] iommu/sva: Refactoring iommu_sva_bind/unbind_device()

Quoted 7 lines · Lu Baolu

The existing iommu SVA interfaces are implemented by calling the SVA specific iommu ops provided by the IOMMU drivers. There's no need for any SVA specific ops in iommu_ops vector anymore as we can achieve this through the generic attach/detach_dev_pasid domain ops.

This refactors the IOMMU SVA interfaces implementation by using the iommu_detach/detach_device_pasid interfaces and align them with the

Did you mean using the iommu_attach/detach_device_pasid interfaces here?

Thanks,

Yuan Can

Quoted 314 lines

concept of the SVA iommu domain. Put the new SVA code in the SVA related file in order to make it self-contained.

---
  include/linux/iommu.h         |  43 ++++++-------
  drivers/iommu/iommu-sva-lib.c | 110 ++++++++++++++++++++++++++++++++++
  drivers/iommu/iommu.c         |  91 ----------------------------
  3 files changed, 133 insertions(+), 111 deletions(-)
include/linux/iommu.h +23 -20
index 836aa480961c..5b1e0f81eebb 100644
@@ -643,6 +643,7 @@ struct iommu_fwspec {
   */
  struct iommu_sva {
  	struct device			*dev;
+	struct iommu_domain		*domain;
  };
 
  int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
@@ -684,11 +685,6 @@ void iommu_release_device(struct device *dev);
  int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features f);
  int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features f);
 
-struct iommu_sva *iommu_sva_bind_device(struct device *dev,
-					struct mm_struct *mm);
-void iommu_sva_unbind_device(struct iommu_sva *handle);
-u32 iommu_sva_get_pasid(struct iommu_sva *handle);
-
  int iommu_device_use_default_domain(struct device *dev);
  void iommu_device_unuse_default_domain(struct device *dev);
 
@@ -1029,21 +1025,6 @@ iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
  	return -ENODEV;
  }
 
-static inline struct iommu_sva *
-iommu_sva_bind_device(struct device *dev, struct mm_struct *mm)
-{
-	return NULL;
-}
-
-static inline void iommu_sva_unbind_device(struct iommu_sva *handle)
-{
-}
-
-static inline u32 iommu_sva_get_pasid(struct iommu_sva *handle)
-{
-	return IOMMU_PASID_INVALID;
-}
-
  static inline struct iommu_fwspec *dev_iommu_fwspec_get(struct device *dev)
  {
  	return NULL;
@@ -1121,4 +1102,26 @@ void iommu_debugfs_setup(void);
  static inline void iommu_debugfs_setup(void) {}
  #endif
 
+#ifdef CONFIG_IOMMU_SVA
+struct iommu_sva *iommu_sva_bind_device(struct device *dev,
+					struct mm_struct *mm);
+void iommu_sva_unbind_device(struct iommu_sva *handle);
+u32 iommu_sva_get_pasid(struct iommu_sva *handle);
+#else
+static inline struct iommu_sva *
+iommu_sva_bind_device(struct device *dev, struct mm_struct *mm)
+{
+	return NULL;
+}
+
+static inline void iommu_sva_unbind_device(struct iommu_sva *handle)
+{
+}
+
+static inline u32 iommu_sva_get_pasid(struct iommu_sva *handle)
+{
+	return IOMMU_PASID_INVALID;
+}
+#endif /* CONFIG_IOMMU_SVA */
+
  #endif /* __LINUX_IOMMU_H */
drivers/iommu/iommu-sva-lib.c +110 -0
index 106506143896..197f68602439 100644
@@ -4,6 +4,7 @@
   */
  #include <linux/mutex.h>
  #include <linux/sched/mm.h>
+#include <linux/iommu.h>
 
  #include "iommu-sva-lib.h"
 
@@ -69,3 +70,112 @@ struct mm_struct *iommu_sva_find(ioasid_t pasid)
  	return ioasid_find(&iommu_sva_pasid, pasid, __mmget_not_zero);
  }
  EXPORT_SYMBOL_GPL(iommu_sva_find);
+
+/**
+ * iommu_sva_bind_device() - Bind a process address space to a device
+ * @dev: the device
+ * @mm: the mm to bind, caller must hold a reference to mm_users
+ *
+ * Create a bond between device and address space, allowing the device to access
+ * the mm using the returned PASID. If a bond already exists between @device and
+ * @mm, it is returned and an additional reference is taken. Caller must call
+ * iommu_sva_unbind_device() to release each reference.
+ *
+ * iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA) must be called first, to
+ * initialize the required SVA features.
+ *
+ * On error, returns an ERR_PTR value.
+ */
+struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm)
+{
+	struct iommu_domain *domain;
+	struct iommu_sva *handle;
+	ioasid_t max_pasids;
+	int ret;
+
+	max_pasids = dev->iommu->max_pasids;
+	if (!max_pasids)
+		return ERR_PTR(-EOPNOTSUPP);
+
+	/* Allocate mm->pasid if necessary. */
+	ret = iommu_sva_alloc_pasid(mm, 1, max_pasids - 1);
+	if (ret)
+		return ERR_PTR(ret);
+
+	handle = kzalloc(sizeof(*handle), GFP_KERNEL);
+	if (!handle)
+		return ERR_PTR(-ENOMEM);
+
+	mutex_lock(&iommu_sva_lock);
+	/* Search for an existing domain. */
+	domain = iommu_get_domain_for_dev_pasid(dev, mm->pasid,
+						IOMMU_DOMAIN_SVA);
+	if (IS_ERR(domain)) {
+		ret = PTR_ERR(domain);
+		goto out_unlock;
+	}
+
+	if (domain) {
+		domain->users++;
+		goto out;
+	}
+
+	/* Allocate a new domain and set it on device pasid. */
+	domain = iommu_sva_domain_alloc(dev, mm);
+	if (!domain) {
+		ret = -ENOMEM;
+		goto out_unlock;
+	}
+
+	ret = iommu_attach_device_pasid(domain, dev, mm->pasid);
+	if (ret)
+		goto out_free_domain;
+	domain->users = 1;
+out:
+	mutex_unlock(&iommu_sva_lock);
+	handle->dev = dev;
+	handle->domain = domain;
+
+	return handle;
+
+out_free_domain:
+	iommu_domain_free(domain);
+out_unlock:
+	mutex_unlock(&iommu_sva_lock);
+	kfree(handle);
+
+	return ERR_PTR(ret);
+}
+EXPORT_SYMBOL_GPL(iommu_sva_bind_device);
+
+/**
+ * iommu_sva_unbind_device() - Remove a bond created with iommu_sva_bind_device
+ * @handle: the handle returned by iommu_sva_bind_device()
+ *
+ * Put reference to a bond between device and address space. The device should
+ * not be issuing any more transaction for this PASID. All outstanding page
+ * requests for this PASID must have been flushed to the IOMMU.
+ */
+void iommu_sva_unbind_device(struct iommu_sva *handle)
+{
+	struct iommu_domain *domain = handle->domain;
+	ioasid_t pasid = domain->mm->pasid;
+	struct device *dev = handle->dev;
+
+	mutex_lock(&iommu_sva_lock);
+	if (--domain->users == 0) {
+		iommu_detach_device_pasid(domain, dev, pasid);
+		iommu_domain_free(domain);
+	}
+	mutex_unlock(&iommu_sva_lock);
+	kfree(handle);
+}
+EXPORT_SYMBOL_GPL(iommu_sva_unbind_device);
+
+u32 iommu_sva_get_pasid(struct iommu_sva *handle)
+{
+	struct iommu_domain *domain = handle->domain;
+
+	return domain->mm->pasid;
+}
+EXPORT_SYMBOL_GPL(iommu_sva_get_pasid);
drivers/iommu/iommu.c +0 -91
index 630e1b64ed89..1119fe39e842 100644
@@ -2785,97 +2785,6 @@ int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
  }
  EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
 
-/**
- * iommu_sva_bind_device() - Bind a process address space to a device
- * @dev: the device
- * @mm: the mm to bind, caller must hold a reference to it
- *
- * Create a bond between device and address space, allowing the device to access
- * the mm using the returned PASID. If a bond already exists between @device and
- * @mm, it is returned and an additional reference is taken. Caller must call
- * iommu_sva_unbind_device() to release each reference.
- *
- * iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA) must be called first, to
- * initialize the required SVA features.
- *
- * On error, returns an ERR_PTR value.
- */
-struct iommu_sva *
-iommu_sva_bind_device(struct device *dev, struct mm_struct *mm)
-{
-	struct iommu_group *group;
-	struct iommu_sva *handle = ERR_PTR(-EINVAL);
-	const struct iommu_ops *ops = dev_iommu_ops(dev);
-
-	if (!ops->sva_bind)
-		return ERR_PTR(-ENODEV);
-
-	group = iommu_group_get(dev);
-	if (!group)
-		return ERR_PTR(-ENODEV);
-
-	/* Ensure device count and domain don't change while we're binding */
-	mutex_lock(&group->mutex);
-
-	/*
-	 * To keep things simple, SVA currently doesn't support IOMMU groups
-	 * with more than one device. Existing SVA-capable systems are not
-	 * affected by the problems that required IOMMU groups (lack of ACS
-	 * isolation, device ID aliasing and other hardware issues).
-	 */
-	if (iommu_group_device_count(group) != 1)
-		goto out_unlock;
-
-	handle = ops->sva_bind(dev, mm);
-
-out_unlock:
-	mutex_unlock(&group->mutex);
-	iommu_group_put(group);
-
-	return handle;
-}
-EXPORT_SYMBOL_GPL(iommu_sva_bind_device);
-
-/**
- * iommu_sva_unbind_device() - Remove a bond created with iommu_sva_bind_device
- * @handle: the handle returned by iommu_sva_bind_device()
- *
- * Put reference to a bond between device and address space. The device should
- * not be issuing any more transaction for this PASID. All outstanding page
- * requests for this PASID must have been flushed to the IOMMU.
- */
-void iommu_sva_unbind_device(struct iommu_sva *handle)
-{
-	struct iommu_group *group;
-	struct device *dev = handle->dev;
-	const struct iommu_ops *ops = dev_iommu_ops(dev);
-
-	if (!ops->sva_unbind)
-		return;
-
-	group = iommu_group_get(dev);
-	if (!group)
-		return;
-
-	mutex_lock(&group->mutex);
-	ops->sva_unbind(handle);
-	mutex_unlock(&group->mutex);
-
-	iommu_group_put(group);
-}
-EXPORT_SYMBOL_GPL(iommu_sva_unbind_device);
-
-u32 iommu_sva_get_pasid(struct iommu_sva *handle)
-{
-	const struct iommu_ops *ops = dev_iommu_ops(handle->dev);
-
-	if (!ops->sva_get_pasid)
-		return IOMMU_PASID_INVALID;
-
-	return ops->sva_get_pasid(handle);
-}
-EXPORT_SYMBOL_GPL(iommu_sva_get_pasid);
-
  /*
   * Changes the default domain of an iommu group that has *only* one device
   *
author maintainer
#36 lore raw

Re: [PATCH v12 13/17] iommu/sva: Refactoring iommu_sva_bind/unbind_device()

Quoted 11 lines · Yuan Can
Quoted 7 lines · Lu Baolu

The existing iommu SVA interfaces are implemented by calling the SVA specific iommu ops provided by the IOMMU drivers. There's no need for any SVA specific ops in iommu_ops vector anymore as we can achieve this through the generic attach/detach_dev_pasid domain ops.

This refactors the IOMMU SVA interfaces implementation by using the iommu_detach/detach_device_pasid interfaces and align them with the

Did you mean using the iommu_attach/detach_device_pasid interfaces here?

The device driver oriented SVA interfaces keep consistent as before. Here we only refactor the IOMMU internal implementation.

Best regards,
baolu

author maintainer
#37 lore raw

Re: [PATCH v12 13/17] iommu/sva: Refactoring iommu_sva_bind/unbind_device()

Quoted 8 lines · Baolu Lu
Quoted 7 lines · Yuan Can
Quoted 5 lines · Lu Baolu

The existing iommu SVA interfaces are implemented by calling the SVA specific iommu ops provided by the IOMMU drivers. There's no need for any SVA specific ops in iommu_ops vector anymore as we can achieve this through the generic attach/detach_dev_pasid domain ops.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

This refactors the IOMMU SVA interfaces implementation by using the iommu_detach/detach_device_pasid interfaces and align them with the

Did you mean using the iommu_attach/detach_device_pasid interfaces here?

The device driver oriented SVA interfaces keep consistent as before. Here we only refactor the IOMMU internal implementation.

Oh! A typo in the commit message, right? Thanks and I will fix it.

Best regards,
baolu

author maintainer
#38 lore raw

PATCH v12 14/17 iommu: Remove SVA related callbacks from iommu ops

In reply to Lu Baolu

These ops'es have been deprecated. There's no need for them anymore. Remove them to avoid dead code.

---
 include/linux/iommu.h                         |  7 ---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h   | 16 ------
 drivers/iommu/intel/iommu.h                   |  3 --
 .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c   | 40 ---------------
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c   |  3 --
 drivers/iommu/intel/iommu.c                   |  3 --
 drivers/iommu/intel/svm.c                     | 49 -------------------
 7 files changed, 121 deletions(-)
include/linux/iommu.h +0 -7
index 5b1e0f81eebb..85023f94e146 100644
@@ -227,9 +227,6 @@ struct iommu_iotlb_gather {
  *                      driver init to device driver init (default no)
  * @dev_has/enable/disable_feat: per device entries to check/enable/disable
  *                               iommu specific features.
- * @sva_bind: Bind process address space to device
- * @sva_unbind: Unbind process address space from device
- * @sva_get_pasid: Get PASID associated to a SVA handle
  * @page_response: handle page request response
  * @def_domain_type: device default domain type, return value:
  *		- IOMMU_DOMAIN_IDENTITY: must use an identity domain
@@ -260,10 +257,6 @@ struct iommu_ops {
 	int (*dev_enable_feat)(struct device *dev, enum iommu_dev_features f);
 	int (*dev_disable_feat)(struct device *dev, enum iommu_dev_features f);
 
-	struct iommu_sva *(*sva_bind)(struct device *dev, struct mm_struct *mm);
-	void (*sva_unbind)(struct iommu_sva *handle);
-	u32 (*sva_get_pasid)(struct iommu_sva *handle);
-
 	int (*page_response)(struct device *dev,
 			     struct iommu_fault_event *evt,
 			     struct iommu_page_response *msg);
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +0 -16
index c9544b656756..3e8f4e8ba1df 100644
@@ -754,9 +754,6 @@ bool arm_smmu_master_sva_enabled(struct arm_smmu_master *master);
 int arm_smmu_master_enable_sva(struct arm_smmu_master *master);
 int arm_smmu_master_disable_sva(struct arm_smmu_master *master);
 bool arm_smmu_master_iopf_supported(struct arm_smmu_master *master);
-struct iommu_sva *arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm);
-void arm_smmu_sva_unbind(struct iommu_sva *handle);
-u32 arm_smmu_sva_get_pasid(struct iommu_sva *handle);
 void arm_smmu_sva_notifier_synchronize(void);
 struct iommu_domain *arm_smmu_sva_domain_alloc(void);
 void arm_smmu_sva_block_dev_pasid(struct iommu_domain *domain,
@@ -792,19 +789,6 @@ static inline bool arm_smmu_master_iopf_supported(struct arm_smmu_master *master
 	return false;
 }
 
-static inline struct iommu_sva *
-arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm)
-{
-	return ERR_PTR(-ENODEV);
-}
-
-static inline void arm_smmu_sva_unbind(struct iommu_sva *handle) {}
-
-static inline u32 arm_smmu_sva_get_pasid(struct iommu_sva *handle)
-{
-	return IOMMU_PASID_INVALID;
-}
-
 static inline void arm_smmu_sva_notifier_synchronize(void) {}
 
 static inline struct iommu_domain *arm_smmu_sva_domain_alloc(void)
drivers/iommu/intel/iommu.h +0 -3
index 4ab535ec6191..6535808c2d9a 100644
@@ -746,9 +746,6 @@ struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn);
 extern void intel_svm_check(struct intel_iommu *iommu);
 extern int intel_svm_enable_prq(struct intel_iommu *iommu);
 extern int intel_svm_finish_prq(struct intel_iommu *iommu);
-struct iommu_sva *intel_svm_bind(struct device *dev, struct mm_struct *mm);
-void intel_svm_unbind(struct iommu_sva *handle);
-u32 intel_svm_get_pasid(struct iommu_sva *handle);
 int intel_svm_page_response(struct device *dev, struct iommu_fault_event *evt,
 			    struct iommu_page_response *msg);
 struct iommu_domain *intel_svm_domain_alloc(void);
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +0 -40
index 953ba19a1af8..e7473e879b3c 100644
@@ -344,11 +344,6 @@ __arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm)
 	if (!bond)
 		return ERR_PTR(-ENOMEM);
 
-	/* Allocate a PASID for this mm if necessary */
-	ret = iommu_sva_alloc_pasid(mm, 1, (1U << master->ssid_bits) - 1);
-	if (ret)
-		goto err_free_bond;
-
 	bond->mm = mm;
 	bond->sva.dev = dev;
 	refcount_set(&bond->refs, 1);
@@ -367,41 +362,6 @@ __arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm)
 	return ERR_PTR(ret);
 }
 
-struct iommu_sva *arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm)
-{
-	struct iommu_sva *handle;
-	struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
-	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
-
-	if (smmu_domain->stage != ARM_SMMU_DOMAIN_S1)
-		return ERR_PTR(-EINVAL);
-
-	mutex_lock(&sva_lock);
-	handle = __arm_smmu_sva_bind(dev, mm);
-	mutex_unlock(&sva_lock);
-	return handle;
-}
-
-void arm_smmu_sva_unbind(struct iommu_sva *handle)
-{
-	struct arm_smmu_bond *bond = sva_to_bond(handle);
-
-	mutex_lock(&sva_lock);
-	if (refcount_dec_and_test(&bond->refs)) {
-		list_del(&bond->list);
-		arm_smmu_mmu_notifier_put(bond->smmu_mn);
-		kfree(bond);
-	}
-	mutex_unlock(&sva_lock);
-}
-
-u32 arm_smmu_sva_get_pasid(struct iommu_sva *handle)
-{
-	struct arm_smmu_bond *bond = sva_to_bond(handle);
-
-	return bond->mm->pasid;
-}
-
 bool arm_smmu_sva_supported(struct arm_smmu_device *smmu)
 {
 	unsigned long reg, fld;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -3
index a27ce693cc65..f6e145973bc9 100644
@@ -2866,9 +2866,6 @@ static struct iommu_ops arm_smmu_ops = {
 	.get_resv_regions	= arm_smmu_get_resv_regions,
 	.dev_enable_feat	= arm_smmu_dev_enable_feature,
 	.dev_disable_feat	= arm_smmu_dev_disable_feature,
-	.sva_bind		= arm_smmu_sva_bind,
-	.sva_unbind		= arm_smmu_sva_unbind,
-	.sva_get_pasid		= arm_smmu_sva_get_pasid,
 	.page_response		= arm_smmu_page_response,
 	.pgsize_bitmap		= -1UL, /* Restricted during device attach */
 	.owner			= THIS_MODULE,
drivers/iommu/intel/iommu.c +0 -3
index d242911f0a88..b8c0afc7f0ac 100644
@@ -4790,9 +4790,6 @@ const struct iommu_ops intel_iommu_ops = {
 	.def_domain_type	= device_def_domain_type,
 	.pgsize_bitmap		= SZ_4K,
 #ifdef CONFIG_INTEL_IOMMU_SVM
-	.sva_bind		= intel_svm_bind,
-	.sva_unbind		= intel_svm_unbind,
-	.sva_get_pasid		= intel_svm_get_pasid,
 	.page_response		= intel_svm_page_response,
 #endif
 	.default_domain_ops = &(const struct iommu_domain_ops) {
drivers/iommu/intel/svm.c +0 -49

61 lines not on this page · raw message

Signature · 1 line
2.25.1
author maintainer
#39 lore raw

PATCH v12 15/17 iommu: Prepare IOMMU domain for IOPF

In reply to Lu Baolu

This adds some mechanisms around the iommu_domain so that the I/O page fault handling framework could route a page fault to the domain and call the fault handler from it.

Add pointers to the page fault handler and its private data in struct iommu_domain. The fault handler will be called with the private data as a parameter once a page fault is routed to the domain. Any kernel component which owns an iommu domain could install handler and its private parameter so that the page fault could be further routed and handled.

This also prepares the SVA implementation to be the first consumer of the per-domain page fault handling model. The I/O page fault handler for SVA is copied to the SVA file with mmget_not_zero() added before mmap_read_lock().

---
 include/linux/iommu.h         |  3 ++
 drivers/iommu/iommu-sva-lib.h |  8 +++++
 drivers/iommu/io-pgfault.c    |  7 +++++
 drivers/iommu/iommu-sva-lib.c | 58 +++++++++++++++++++++++++++++++++++
 drivers/iommu/iommu.c         |  4 +++
 5 files changed, 80 insertions(+)
include/linux/iommu.h +3 -0
index 85023f94e146..266ef281cbd5 100644
@@ -98,6 +98,9 @@ struct iommu_domain {
 	unsigned long pgsize_bitmap;	/* Bitmap of page sizes in use */
 	struct iommu_domain_geometry geometry;
 	struct iommu_dma_cookie *iova_cookie;
+	enum iommu_page_response_code (*iopf_handler)(struct iommu_fault *fault,
+						      void *data);
+	void *fault_data;
 	union {
 		struct {
 			iommu_fault_handler_t handler;
drivers/iommu/iommu-sva-lib.h +8 -0
index 8909ea1094e3..1b3ace4b5863 100644
@@ -26,6 +26,8 @@ int iopf_queue_flush_dev(struct device *dev);
 struct iopf_queue *iopf_queue_alloc(const char *name);
 void iopf_queue_free(struct iopf_queue *queue);
 int iopf_queue_discard_partial(struct iopf_queue *queue);
+enum iommu_page_response_code
+iommu_sva_handle_iopf(struct iommu_fault *fault, void *data);
 
 #else /* CONFIG_IOMMU_SVA */
 static inline int iommu_queue_iopf(struct iommu_fault *fault, void *cookie)
@@ -63,5 +65,11 @@ static inline int iopf_queue_discard_partial(struct iopf_queue *queue)
 {
 	return -ENODEV;
 }
+
+static inline enum iommu_page_response_code
+iommu_sva_handle_iopf(struct iommu_fault *fault, void *data)
+{
+	return IOMMU_PAGE_RESP_INVALID;
+}
 #endif /* CONFIG_IOMMU_SVA */
 #endif /* _IOMMU_SVA_LIB_H */
drivers/iommu/io-pgfault.c +7 -0
index 1df8c1dcae77..aee9e033012f 100644
@@ -181,6 +181,13 @@ static void iopf_handle_group(struct work_struct *work)
  * request completes, outstanding faults will have been dealt with by the time
  * the PASID is freed.
  *
+ * Any valid page fault will be eventually routed to an iommu domain and the
+ * page fault handler installed there will get called. The users of this
+ * handling framework should guarantee that the iommu domain could only be
+ * freed after the device has stopped generating page faults (or the iommu
+ * hardware has been set to block the page faults) and the pending page faults
+ * have been flushed.
+ *
  * Return: 0 on success and <0 on error.
  */
 int iommu_queue_iopf(struct iommu_fault *fault, void *cookie)
drivers/iommu/iommu-sva-lib.c +58 -0

61 lines not on this page · raw message

drivers/iommu/iommu.c +4 -0
index 1119fe39e842..4dbf9d5436de 100644
@@ -29,6 +29,8 @@
 #include <trace/events/iommu.h>
 #include <linux/sched/mm.h>
 
+#include "iommu-sva-lib.h"
+
 static struct kset *iommu_group_kset;
 static DEFINE_IDA(iommu_group_ida);
 
@@ -3316,6 +3318,8 @@ struct iommu_domain *iommu_sva_domain_alloc(struct device *dev,
 	domain->type = IOMMU_DOMAIN_SVA;
 	mmgrab(mm);
 	domain->mm = mm;
+	domain->iopf_handler = iommu_sva_handle_iopf;
+	domain->fault_data = mm;
 
 	return domain;
 }
Signature · 1 line
2.25.1
author maintainer
#40 lore raw

PATCH v12 16/17 iommu: Per-domain I/O page fault handling

In reply to Lu Baolu

Tweak the I/O page fault handling framework to route the page faults to the domain and call the page fault handler retrieved from the domain. This makes the I/O page fault handling framework possible to serve more usage scenarios as long as they have an IOMMU domain and install a page fault handler in it. Some unused functions are also removed to avoid dead code.

The iommu_get_domain_for_dev_pasid() which retrieves attached domain for a {device, PASID} pair is used. It will be used by the page fault handling framework which knows {device, PASID} reported from the iommu driver. We have a guarantee that the SVA domain doesn't go away during IOPF handling, because unbind() won't free the domain until all the pending page requests have been flushed from the pipeline. The drivers either call iopf_queue_flush_dev() explicitly, or in stall case, the device driver is required to flush all DMAs including stalled transactions before calling unbind().

This also renames iopf_handle_group() to iopf_handler() to avoid confusing.

---
 drivers/iommu/io-pgfault.c | 68 +++++---------------------------------
 1 file changed, 9 insertions(+), 59 deletions(-)
drivers/iommu/io-pgfault.c +9 -59
index aee9e033012f..d046d89cec55 100644
@@ -69,69 +69,18 @@ static int iopf_complete_group(struct device *dev, struct iopf_fault *iopf,
 	return iommu_page_response(dev, &resp);
 }
 
-static enum iommu_page_response_code
-iopf_handle_single(struct iopf_fault *iopf)
-{
-	vm_fault_t ret;
-	struct mm_struct *mm;
-	struct vm_area_struct *vma;
-	unsigned int access_flags = 0;
-	unsigned int fault_flags = FAULT_FLAG_REMOTE;
-	struct iommu_fault_page_request *prm = &iopf->fault.prm;
-	enum iommu_page_response_code status = IOMMU_PAGE_RESP_INVALID;
-
-	if (!(prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID))
-		return status;
-
-	mm = iommu_sva_find(prm->pasid);
-	if (IS_ERR_OR_NULL(mm))
-		return status;
-
-	mmap_read_lock(mm);
-
-	vma = find_extend_vma(mm, prm->addr);
-	if (!vma)
-		/* Unmapped area */
-		goto out_put_mm;
-
-	if (prm->perm & IOMMU_FAULT_PERM_READ)
-		access_flags |= VM_READ;
-
-	if (prm->perm & IOMMU_FAULT_PERM_WRITE) {
-		access_flags |= VM_WRITE;
-		fault_flags |= FAULT_FLAG_WRITE;
-	}
-
-	if (prm->perm & IOMMU_FAULT_PERM_EXEC) {
-		access_flags |= VM_EXEC;
-		fault_flags |= FAULT_FLAG_INSTRUCTION;
-	}
-
-	if (!(prm->perm & IOMMU_FAULT_PERM_PRIV))
-		fault_flags |= FAULT_FLAG_USER;
-
-	if (access_flags & ~vma->vm_flags)
-		/* Access fault */
-		goto out_put_mm;
-
-	ret = handle_mm_fault(vma, prm->addr, fault_flags, NULL);
-	status = ret & VM_FAULT_ERROR ? IOMMU_PAGE_RESP_INVALID :
-		IOMMU_PAGE_RESP_SUCCESS;
-
-out_put_mm:
-	mmap_read_unlock(mm);
-	mmput(mm);
-
-	return status;
-}
-
-static void iopf_handle_group(struct work_struct *work)
+static void iopf_handler(struct work_struct *work)
 {
 	struct iopf_group *group;
+	struct iommu_domain *domain;
 	struct iopf_fault *iopf, *next;
 	enum iommu_page_response_code status = IOMMU_PAGE_RESP_SUCCESS;
 
 	group = container_of(work, struct iopf_group, work);
+	domain = iommu_get_domain_for_dev_pasid(group->dev,
+				group->last_fault.fault.prm.pasid, 0);
+	if (!domain || !domain->iopf_handler)
+		status = IOMMU_PAGE_RESP_INVALID;
 
 	list_for_each_entry_safe(iopf, next, &group->faults, list) {
 		/*
@@ -139,7 +88,8 @@ static void iopf_handle_group(struct work_struct *work)
 		 * faults in the group if there is an error.
 		 */
 		if (status == IOMMU_PAGE_RESP_SUCCESS)
-			status = iopf_handle_single(iopf);
+			status = domain->iopf_handler(&iopf->fault,
+						      domain->fault_data);
 
 		if (!(iopf->fault.prm.flags &
 		      IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE))
@@ -242,7 +192,7 @@ int iommu_queue_iopf(struct iommu_fault *fault, void *cookie)
 	group->last_fault.fault = *fault;
 	INIT_LIST_HEAD(&group->faults);
 	list_add(&group->last_fault.list, &group->faults);
-	INIT_WORK(&group->work, iopf_handle_group);
+	INIT_WORK(&group->work, iopf_handler);
 
 	/* See if we have partial faults for this group */
 	list_for_each_entry_safe(iopf, next, &iopf_param->partial, list) {
Signature · 1 line
2.25.1
author maintainer
#41 lore raw

PATCH v12 17/17 iommu: Rename iommu-sva-lib.{c,h}

In reply to Lu Baolu

Rename iommu-sva-lib.c[h] to iommu-sva.c[h] as it contains all code for SVA implementation in iommu core.

---
 drivers/iommu/{iommu-sva-lib.h => iommu-sva.h}  | 6 +++---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 2 +-
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c     | 2 +-
 drivers/iommu/intel/iommu.c                     | 2 +-
 drivers/iommu/intel/svm.c                       | 2 +-
 drivers/iommu/io-pgfault.c                      | 2 +-
 drivers/iommu/{iommu-sva-lib.c => iommu-sva.c}  | 2 +-
 drivers/iommu/iommu.c                           | 2 +-
 drivers/iommu/Makefile                          | 2 +-
 9 files changed, 11 insertions(+), 11 deletions(-)
 rename drivers/iommu/{iommu-sva-lib.h => iommu-sva.h} (95%)
 rename drivers/iommu/{iommu-sva-lib.c => iommu-sva.c} (99%)
drivers/iommu/iommu-sva.h renamed from drivers/iommu/iommu-sva-lib.h +3 -3
similarity index 95%
rename from drivers/iommu/iommu-sva-lib.h
rename to drivers/iommu/iommu-sva.h
index 1b3ace4b5863..7215a761b962 100644
@@ -2,8 +2,8 @@
 /*
  * SVA library for IOMMU drivers
  */
-#ifndef _IOMMU_SVA_LIB_H
-#define _IOMMU_SVA_LIB_H
+#ifndef _IOMMU_SVA_H
+#define _IOMMU_SVA_H
 
 #include <linux/ioasid.h>
 #include <linux/mm_types.h>
@@ -72,4 +72,4 @@ iommu_sva_handle_iopf(struct iommu_fault *fault, void *data)
 	return IOMMU_PAGE_RESP_INVALID;
 }
 #endif /* CONFIG_IOMMU_SVA */
-#endif /* _IOMMU_SVA_LIB_H */
+#endif /* _IOMMU_SVA_H */
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +1 -1
index e7473e879b3c..c5471fa0299c 100644
@@ -10,7 +10,7 @@
 #include <linux/slab.h>
 
 #include "arm-smmu-v3.h"
-#include "../../iommu-sva-lib.h"
+#include "../../iommu-sva.h"
 #include "../../io-pgtable-arm.h"
 
 struct arm_smmu_mmu_notifier {
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +1 -1
index f6e145973bc9..a3c5b1a1203b 100644
@@ -31,7 +31,7 @@
 #include <linux/amba/bus.h>
 
 #include "arm-smmu-v3.h"
-#include "../../iommu-sva-lib.h"
+#include "../../iommu-sva.h"
 
 static bool disable_bypass = true;
 module_param(disable_bypass, bool, 0444);
drivers/iommu/intel/iommu.c +1 -1
index b8c0afc7f0ac..f5ae4f7f916d 100644
@@ -27,7 +27,7 @@
 
 #include "iommu.h"
 #include "../irq_remapping.h"
-#include "../iommu-sva-lib.h"
+#include "../iommu-sva.h"
 #include "pasid.h"
 #include "cap_audit.h"
 
drivers/iommu/intel/svm.c +1 -1
index 3be41b0291e5..028ad54732c5 100644
@@ -24,7 +24,7 @@
 #include "iommu.h"
 #include "pasid.h"
 #include "perf.h"
-#include "../iommu-sva-lib.h"
+#include "../iommu-sva.h"
 #include "trace.h"
 
 static irqreturn_t prq_event_thread(int irq, void *d);
drivers/iommu/io-pgfault.c +1 -1
index d046d89cec55..e5b8b9110c13 100644
@@ -11,7 +11,7 @@
 #include <linux/slab.h>
 #include <linux/workqueue.h>
 
-#include "iommu-sva-lib.h"
+#include "iommu-sva.h"
 
 /**
  * struct iopf_queue - IO Page Fault queue
drivers/iommu/iommu-sva.c renamed from drivers/iommu/iommu-sva-lib.c +1 -1
similarity index 99%
rename from drivers/iommu/iommu-sva-lib.c
rename to drivers/iommu/iommu-sva.c
index 520ec0b4ed85..54b4a66583d4 100644
@@ -6,7 +6,7 @@
 #include <linux/sched/mm.h>
 #include <linux/iommu.h>
 
-#include "iommu-sva-lib.h"
+#include "iommu-sva.h"
 
 static DEFINE_MUTEX(iommu_sva_lock);
 static DECLARE_IOASID_SET(iommu_sva_pasid);
drivers/iommu/iommu.c +1 -1
index 4dbf9d5436de..9e84f58440dd 100644
@@ -29,7 +29,7 @@
 #include <trace/events/iommu.h>
 #include <linux/sched/mm.h>
 
-#include "iommu-sva-lib.h"
+#include "iommu-sva.h"
 
 static struct kset *iommu_group_kset;
 static DEFINE_IDA(iommu_group_ida);
drivers/iommu/Makefile +1 -1
index 44475a9b3eea..c1763476162b 100644
@@ -27,6 +27,6 @@ obj-$(CONFIG_FSL_PAMU) += fsl_pamu.o fsl_pamu_domain.o
 obj-$(CONFIG_S390_IOMMU) += s390-iommu.o
 obj-$(CONFIG_HYPERV_IOMMU) += hyperv-iommu.o
 obj-$(CONFIG_VIRTIO_IOMMU) += virtio-iommu.o
-obj-$(CONFIG_IOMMU_SVA) += iommu-sva-lib.o io-pgfault.o
+obj-$(CONFIG_IOMMU_SVA) += iommu-sva.o io-pgfault.o
 obj-$(CONFIG_SPRD_IOMMU) += sprd-iommu.o
 obj-$(CONFIG_APPLE_DART) += apple-dart.o
Signature · 1 line
2.25.1
#42 lore raw
In reply to Lu Baolu
Quoted 25 lines · Lu Baolu

Hi folks,

The former part of this series introduces the IOMMU interfaces to attach or detach an iommu domain to/from a pasid of a device, and refactors the exsiting IOMMU SVA implementation by assigning an SVA type of iommu domain to a shared virtual address and replacing sva_bind/unbind iommu ops with a set_dev_pasid domain ops.

The latter part changes the existing I/O page fault handling framework from only serving SVA to a generic one. Any driver or component could handle the I/O page faults for its domain in its own way by installing an I/O page fault handler.

This series has been functionally tested by Tony Zhu on Intel hardware and Zhangfei Gao on arm64 (Kunpeng920) hardware. Thanks a lot for the efforts.

This series is also available on github:
https://github.com/LuBaolu/intel-iommu/commits/iommu-sva-refactoring-v12

Please review and suggest.

Best regards,
baolu

Quoted 9 lines

Change log:
v12:
- Add blocking domain support in both vt-d and smmuv3 drivers and make the set blocking domain through its own domain ops.
- Add a type parameter in iommu_get_domain_for_dev_pasid() to matach the interested domain type.
- Only enforce ACS RR & UF in pci_enable_pasid() and refine the commit messages according to Bjorn's suggestions.
- Misc code and comment refinement.

I think this is looking pretty good, aside from some minor remarks

Jason