Skip to contentSkip to conversation

iommu: SVA and IOPF refactoring

PATCH Superseded Lu Baolu 4 participants 40 messages May 10, 2022 – May 16, 2022

Subsystems (6)

v6 of 14 Superseded Latest is v14 →

lore
Share & tools
lore

Lu Baolu

author maintainer
#0 lore raw

v6 cover iommu: SVA and IOPF refactoring

Hi folks,

The former part of this series refactors the IOMMU SVA code by assigning an SVA type of iommu_domain to a shared virtual address and replacing sva_bind/unbind iommu ops with attach/detach_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 on an x86 machine and compile tested for other architectures.

This series is also available on github:
[2] https://github.com/LuBaolu/intel-iommu/commits/iommu-sva-refactoring-v6

Please review and suggest.

Best regards,
baolu

Change log:
v6:
- 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 exept 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.

Dave Jiang (1):
dmaengine: idxd: Separate user and kernel pasid enabling

Lu Baolu (11):
iommu: Add pasid_bits field in struct dev_iommu
iommu: Add attach/detach_dev_pasid domain ops iommu/sva: Basic data structures for SVA iommu/vt-d: Remove SVM_FLAG_SUPERVISOR_MODE support iommu/vt-d: Add SVA domain support arm-smmu-v3/sva: Add SVA domain support iommu/sva: Use attach/detach_pasid_dev in SVA interfaces
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-iommu.h                   |   9 +-
 include/linux/iommu.h                         |  99 +++++--
 drivers/dma/idxd/idxd.h                       |   6 +
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h   |  19 +-
 .../iommu/{iommu-sva-lib.h => iommu-sva.h}    |   3 -
 drivers/dma/idxd/cdev.c                       |   4 +-
 drivers/dma/idxd/init.c                       |  30 +-
 .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c   | 111 +++++---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c   |  10 +-
 drivers/iommu/intel/iommu.c                   |  12 +-
 drivers/iommu/intel/svm.c                     | 146 ++++------
 drivers/iommu/io-pgfault.c                    |  73 +----
 drivers/iommu/iommu-sva-lib.c                 |  71 -----
 drivers/iommu/iommu-sva.c                     | 261 ++++++++++++++++++
 drivers/iommu/iommu.c                         | 193 +++++++------
 drivers/iommu/Makefile                        |   2 +-
 16 files changed, 623 insertions(+), 426 deletions(-)
 rename drivers/iommu/{iommu-sva-lib.h => iommu-sva.h} (91%)
 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 v6 1/12 dmaengine: idxd: Separate user and kernel pasid enabling

From: Dave Jiang <[email protected]>

The idxd driver always gated the pasid enabling under a single knob and this assumption is incorrect. The pasid used for kernel operation can be independently toggled and has no dependency on the user pasid (and vice versa). Split the two so they are independent "enabled" flags.

---
 drivers/dma/idxd/idxd.h |  6 ++++++
 drivers/dma/idxd/cdev.c |  4 ++--
 drivers/dma/idxd/init.c | 30 ++++++++++++++++++------------
 3 files changed, 26 insertions(+), 14 deletions(-)
drivers/dma/idxd/idxd.h +6 -0
index da72eb15f610..ccbefd0be617 100644
@@ -239,6 +239,7 @@ enum idxd_device_flag {
 	IDXD_FLAG_CONFIGURABLE = 0,
 	IDXD_FLAG_CMD_RUNNING,
 	IDXD_FLAG_PASID_ENABLED,
+	IDXD_FLAG_USER_PASID_ENABLED,
 };
 
 struct idxd_dma_dev {
@@ -469,6 +470,11 @@ static inline bool device_pasid_enabled(struct idxd_device *idxd)
 	return test_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags);
 }
 
+static inline bool device_user_pasid_enabled(struct idxd_device *idxd)
+{
+	return test_bit(IDXD_FLAG_USER_PASID_ENABLED, &idxd->flags);
+}
+
 static inline bool device_swq_supported(struct idxd_device *idxd)
 {
 	return (support_enqcmd && device_pasid_enabled(idxd));
drivers/dma/idxd/cdev.c +2 -2
index b9b2b4a4124e..7df996deffbe 100644
@@ -99,7 +99,7 @@ static int idxd_cdev_open(struct inode *inode, struct file *filp)
 	ctx->wq = wq;
 	filp->private_data = ctx;
 
-	if (device_pasid_enabled(idxd)) {
+	if (device_user_pasid_enabled(idxd)) {
 		sva = iommu_sva_bind_device(dev, current->mm, NULL);
 		if (IS_ERR(sva)) {
 			rc = PTR_ERR(sva);
@@ -152,7 +152,7 @@ static int idxd_cdev_release(struct inode *node, struct file *filep)
 	if (wq_shared(wq)) {
 		idxd_device_drain_pasid(idxd, ctx->pasid);
 	} else {
-		if (device_pasid_enabled(idxd)) {
+		if (device_user_pasid_enabled(idxd)) {
 			/* The wq disable in the disable pasid function will drain the wq */
 			rc = idxd_wq_disable_pasid(wq);
 			if (rc < 0)
drivers/dma/idxd/init.c +18 -12
index 993a5dcca24f..e1b5d1e4a949 100644
@@ -513,16 +513,19 @@ static int idxd_probe(struct idxd_device *idxd)
 
 	if (IS_ENABLED(CONFIG_INTEL_IDXD_SVM) && sva) {
 		rc = iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA);
-		if (rc == 0) {
-			rc = idxd_enable_system_pasid(idxd);
-			if (rc < 0) {
-				iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
-				dev_warn(dev, "Failed to enable PASID. No SVA support: %d\n", rc);
-			} else {
-				set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags);
-			}
-		} else {
+		if (rc) {
+			/*
+			 * Do not bail here since legacy DMA is still
+			 * supported, both user and in-kernel DMA with
+			 * PASID rely on SVA feature.
+			 */
 			dev_warn(dev, "Unable to turn on SVA feature.\n");
+		} else {
+			set_bit(IDXD_FLAG_USER_PASID_ENABLED, &idxd->flags);
+			if (idxd_enable_system_pasid(idxd))
+				dev_warn(dev, "No in-kernel DMA with PASID.\n");
+			else
+				set_bit(IDXD_FLAG_PASID_ENABLED, &idxd->flags);
 		}
 	} else if (!sva) {
 		dev_warn(dev, "User forced SVA off via module param.\n");
@@ -561,7 +564,8 @@ static int idxd_probe(struct idxd_device *idxd)
  err:
 	if (device_pasid_enabled(idxd))
 		idxd_disable_system_pasid(idxd);
-	iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
+	if (device_user_pasid_enabled(idxd) || device_pasid_enabled(idxd))
+		iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
 	return rc;
 }
 
@@ -574,7 +578,8 @@ static void idxd_cleanup(struct idxd_device *idxd)
 	idxd_cleanup_internals(idxd);
 	if (device_pasid_enabled(idxd))
 		idxd_disable_system_pasid(idxd);
-	iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
+	if (device_user_pasid_enabled(idxd) || device_pasid_enabled(idxd))
+		iommu_dev_disable_feature(dev, IOMMU_DEV_FEAT_SVA);
 }
 
 static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
@@ -691,7 +696,8 @@ static void idxd_remove(struct pci_dev *pdev)
 	free_irq(irq_entry->vector, irq_entry);
 	pci_free_irq_vectors(pdev);
 	pci_iounmap(pdev, idxd->reg_base);
-	iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA);
+	if (device_user_pasid_enabled(idxd) || device_pasid_enabled(idxd))
+		iommu_dev_disable_feature(&pdev->dev, IOMMU_DEV_FEAT_SVA);
 	pci_disable_device(pdev);
 	destroy_workqueue(idxd->wq);
 	perfmon_pmu_remove(idxd);
Signature · 1 line
2.25.1
author maintainer
#2 lore raw

PATCH v6 2/12 iommu: Add pasid_bits field in struct dev_iommu

In reply to Lu Baolu

Use this field to save the pasid/ssid bits that a device is able to support with its IOMMU hardware. It is a generic attribute of a device and lifting it into the per-device dev_iommu struct makes it possible to allocate a PASID for device without calls into the IOMMU drivers. Any iommu driver which suports PASID related features should set this field before features are enabled on the devices.

For initialization of this field in the VT-d driver, the info->pasid_supported is only set for PCI devices. So the status is that non-PCI SVA hasn't been supported yet. Setting this field only for PCI devices has no functional change.

---
 include/linux/iommu.h                       | 1 +
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 2 ++
 drivers/iommu/intel/iommu.c                 | 5 ++++-
 3 files changed, 7 insertions(+), 1 deletion(-)
include/linux/iommu.h +1 -0
index 5e1afe169549..b8ffaf2cb1d0 100644
@@ -373,6 +373,7 @@ struct dev_iommu {
 	struct iommu_fwspec		*fwspec;
 	struct iommu_device		*iommu_dev;
 	void				*priv;
+	unsigned int			pasid_bits;
 };
 
 int iommu_device_register(struct iommu_device *iommu,
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2 -0
index 627a3ed5ee8f..afc63fce6107 100644
@@ -2681,6 +2681,8 @@ static struct iommu_device *arm_smmu_probe_device(struct device *dev)
 	    smmu->features & ARM_SMMU_FEAT_STALL_FORCE)
 		master->stall_enabled = true;
 
+	dev->iommu->pasid_bits = master->ssid_bits;
+
 	return &smmu->iommu;
 
 err_free_master:
drivers/iommu/intel/iommu.c +4 -1
index 2990f80c5e08..99643f897f26 100644
@@ -4624,8 +4624,11 @@ static struct iommu_device *intel_iommu_probe_device(struct device *dev)
 			if (pasid_supported(iommu)) {
 				int features = pci_pasid_features(pdev);
 
-				if (features >= 0)
+				if (features >= 0) {
 					info->pasid_supported = features | 1;
+					dev->iommu->pasid_bits =
+						fls(pci_max_pasids(pdev)) - 1;
+				}
 			}
 
 			if (info->ats_supported && ecap_prs(iommu->ecap) &&
Signature · 6 lines
2.25.1

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

Re: [PATCH v6 02/12] iommu: Add pasid_bits field in struct dev_iommu

Quoted 16 lines · Lu Baolu

int iommu_device_register(struct iommu_device *iommu,

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2 -0
index 627a3ed5ee8f..afc63fce6107 100644
+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
@@ -2681,6 +2681,8 @@ static struct iommu_device *arm_smmu_probe_device(struct device *dev)
 	    smmu->features & ARM_SMMU_FEAT_STALL_FORCE)
 		master->stall_enabled = true;
 
+	dev->iommu->pasid_bits = master->ssid_bits;
 	return &smmu->iommu;
 
 err_free_master:
drivers/iommu/intel/iommu.c +1 -0
index 2990f80c5e08..99643f897f26 100644
+++ b/drivers/iommu/intel/iommu.c

drivers/iommu/intel/iommu.c static struct iommu_device *intel_iommu_probe_device(struct device *dev) · line 4631

Quoted 9 lines
drivers/iommu/intel/iommu.c +4 -1
@@ -4624,8 +4624,11 @@ static struct iommu_device *intel_iommu_probe_device(struct device *dev)
 			if (pasid_supported(iommu)) {
 				int features = pci_pasid_features(pdev);
 
-				if (features >= 0)
+				if (features >= 0) {
 					info->pasid_supported = features | 1;
+					dev->iommu->pasid_bits =
+						fls(pci_max_pasids(pdev)) - 1;
+				}

It is not very nice that both the iommu drivers have to duplicate the code to read the pasid capability out of the PCI device.

IMHO it would make more sense for the iommu layer to report the capability of its own HW block only, and for the core code to figure out the master's limitation using a bus-specific approach.

It is also unfortunate that the enable/disable pasid is inside the iommu driver as well - ideally the PCI driver itself would do this when it knows it wants to use PASIDs.

The ordering interaction with ATS makes this look quite annoying though. :(

I'm also not convinced individual IOMMU drivers should be forcing ATS on, there are performance and functional implications here. Using ATS or not is possibly best left as an administrator policy controlled by the core code. Again we seem to have some mess.

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

author maintainer
#4 lore raw

Re: [PATCH v6 02/12] iommu: Add pasid_bits field in struct dev_iommu

Quoted 31 lines · Jason Gunthorpe
Quoted 16 lines · Lu Baolu

int iommu_device_register(struct iommu_device *iommu,

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2 -0
index 627a3ed5ee8f..afc63fce6107 100644
+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
@@ -2681,6 +2681,8 @@ static struct iommu_device *arm_smmu_probe_device(struct device *dev)
  	    smmu->features & ARM_SMMU_FEAT_STALL_FORCE)
  		master->stall_enabled = true;
 
+	dev->iommu->pasid_bits = master->ssid_bits;
  	return &smmu->iommu;
 
  err_free_master:
drivers/iommu/intel/iommu.c +1 -0
index 2990f80c5e08..99643f897f26 100644
+++ b/drivers/iommu/intel/iommu.c

drivers/iommu/intel/iommu.c static struct iommu_device *intel_iommu_probe_device(struct device *dev) · line 4631

Quoted 9 lines
drivers/iommu/intel/iommu.c +4 -1
@@ -4624,8 +4624,11 @@ static struct iommu_device *intel_iommu_probe_device(struct device *dev)
  			if (pasid_supported(iommu)) {
  				int features = pci_pasid_features(pdev);
 
-				if (features >= 0)
+				if (features >= 0) {
  					info->pasid_supported = features | 1;
+					dev->iommu->pasid_bits =
+						fls(pci_max_pasids(pdev)) - 1;
+				}

It is not very nice that both the iommu drivers have to duplicate the code to read the pasid capability out of the PCI device.

Quoted 3 lines

IMHO it would make more sense for the iommu layer to report the capability of its own HW block only, and for the core code to figure out the master's limitation using a bus-specific approach.

Fair enough. The iommu hardware capability could be reported in

/**
* struct iommu_device - IOMMU core representation of one IOMMU hardware
* instance
* @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
*/

struct iommu_device {
        struct list_head list;
        const struct iommu_ops *ops;
        struct fwnode_handle *fwnode;
        struct device *dev;
};

I haven't checked ARM code yet, but it works for x86 as far as I can see.

Quoted 5 lines

It is also unfortunate that the enable/disable pasid is inside the iommu driver as well - ideally the PCI driver itself would do this when it knows it wants to use PASIDs.

Quoted 7 lines

The ordering interaction with ATS makes this look quite annoying though. :(

I'm also not convinced individual IOMMU drivers should be forcing ATS on, there are performance and functional implications here. Using ATS or not is possibly best left as an administrator policy controlled by the core code. Again we seem to have some mess.

Agreed with you. This has already been in my task list. I will start to solve it after the iommufd tasks.

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

#5 lore raw

Re: [PATCH v6 02/12] iommu: Add pasid_bits field in struct dev_iommu

Quoted 49 lines · Baolu Lu
Quoted 28 lines · Jason Gunthorpe
Quoted 14 lines · Lu Baolu

int iommu_device_register(struct iommu_device *iommu,

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +2 -0
index 627a3ed5ee8f..afc63fce6107 100644
+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
@@ -2681,6 +2681,8 @@ static struct iommu_device *arm_smmu_probe_device(struct device *dev)
  	    smmu->features & ARM_SMMU_FEAT_STALL_FORCE)
  		master->stall_enabled = true;
+	dev->iommu->pasid_bits = master->ssid_bits;
  	return &smmu->iommu;
  err_free_master:
drivers/iommu/intel/iommu.c +1 -0
index 2990f80c5e08..99643f897f26 100644
+++ b/drivers/iommu/intel/iommu.c

drivers/iommu/intel/iommu.c static struct iommu_device *intel_iommu_probe_device(struct device *dev) · line 4630

Quoted 8 lines
drivers/iommu/intel/iommu.c +4 -1
@@ -4624,8 +4624,11 @@ static struct iommu_device *intel_iommu_probe_device(struct device *dev)
  			if (pasid_supported(iommu)) {
  				int features = pci_pasid_features(pdev);
-				if (features >= 0)
+				if (features >= 0) {
  					info->pasid_supported = features | 1;
+					dev->iommu->pasid_bits =
+						fls(pci_max_pasids(pdev)) - 1;
+				}

It is not very nice that both the iommu drivers have to duplicate the code to read the pasid capability out of the PCI device.

Quoted 3 lines

IMHO it would make more sense for the iommu layer to report the capability of its own HW block only, and for the core code to figure out the master's limitation using a bus-specific approach.

Fair enough. The iommu hardware capability could be reported in

/**
* struct iommu_device - IOMMU core representation of one IOMMU hardware
* instance
* @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
*/

struct iommu_device {
        struct list_head list;
        const struct iommu_ops *ops;
        struct fwnode_handle *fwnode;
        struct device *dev;
};
Quoted 2 lines

I haven't checked ARM code yet, but it works for x86 as far as I can see.

Arm also supports non-PCI PASID by reading a firmware property:

        device_property_read_u32(dev, "pasid-num-bits", &master->ssid_bits);

should be the only difference

Thanks,
Jean

Quoted 14 lines
Quoted 6 lines

It is also unfortunate that the enable/disable pasid is inside the iommu driver as well - ideally the PCI driver itself would do this when it knows it wants to use PASIDs.

Quoted 7 lines

The ordering interaction with ATS makes this look quite annoying though. :(

I'm also not convinced individual IOMMU drivers should be forcing ATS on, there are performance and functional implications here. Using ATS or not is possibly best left as an administrator policy controlled by the core code. Again we seem to have some mess.

Quoted 5 lines

Agreed with you. This has already been in my task list. I will start to solve it after the iommufd tasks.

Best regards,
baolu

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

#6 lore raw

Re: [PATCH v6 02/12] iommu: Add pasid_bits field in struct dev_iommu

Quoted 14 lines · Jean-Philippe Brucker
Quoted 14 lines

/**
* struct iommu_device - IOMMU core representation of one IOMMU hardware
* instance
* @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
*/

struct iommu_device {
        struct list_head list;
        const struct iommu_ops *ops;
        struct fwnode_handle *fwnode;
        struct device *dev;
};
Quoted 8 lines
Quoted 2 lines

I haven't checked ARM code yet, but it works for x86 as far as I can see.

Arm also supports non-PCI PASID by reading a firmware property:

        device_property_read_u32(dev, "pasid-num-bits", &master->ssid_bits);

should be the only difference

That is not "ARM" that is generic DT/ACPI for platform devices and should be handled by the core code in the same place it does PCI discovery.

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

author maintainer
#7 lore raw

PATCH v6 3/12 iommu: Add attach/detach_dev_pasid domain ops

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 a pair of common domain ops for this purpose and adds helpers to attach/detach a domain to/from a {device, PASID}. Some buses, like PCI, route packets without considering the PASID value. Thus a DMA target address with PASID might be treated as P2P if the address falls into the MMIO BAR of other devices in the group. To make things simple, these interfaces only apply to devices belonging to the singleton groups, and the singleton is immutable in fabric i.e. not affected by hotplug.

---
 include/linux/iommu.h | 21 +++++++++++++
 drivers/iommu/iommu.c | 71 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 92 insertions(+)
include/linux/iommu.h +21 -0
index b8ffaf2cb1d0..ab36244d4e94 100644
@@ -263,6 +263,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
+ * @attach_dev_pasid: attach an iommu domain to a pasid of device
+ * @detach_dev_pasid: detach an iommu domain from 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.
@@ -283,6 +285,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 (*attach_dev_pasid)(struct iommu_domain *domain,
+				struct device *dev, ioasid_t pasid);
+	void (*detach_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);
@@ -678,6 +684,10 @@ 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);
 #else /* CONFIG_IOMMU_API */
 
 struct iommu_ops {};
@@ -1051,6 +1061,17 @@ 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)
+{
+}
 #endif /* CONFIG_IOMMU_API */
 
 /**
drivers/iommu/iommu.c +71 -0

86 lines not on this page · raw message

Signature · 6 lines
2.25.1

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

Re: [PATCH v6 03/12] iommu: Add attach/detach_dev_pasid domain ops

Quoted 2 lines · Lu Baolu

This adds a pair of common domain ops for this purpose and adds helpers to attach/detach a domain to/from a {device, PASID}.

I wonder if this should not have a detach op - after discussing with Robin we can see that detach_dev is not used in updated drivers. Instead attach_dev acts as 'set_domain'

So, it would be more symmetrical if attaching a blocking_domain to the PASID was the way to 'detach'.

This could be made straightforward by following the sketch I showed to have a static, global blocing_domain and providing a pointer to it in struct iommu_ops

Then 'detach pasid' is:

iommu_ops->blocking_domain->ops->attach_dev_pasid(domain, dev, pasid);

And we move away from the notion of 'detach' and in the direction that everything continuously has a domain set. PASID would logically default to blocking_domain, though we wouldn't track this anywhere.

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

author maintainer
#9 lore raw

Re: [PATCH v6 03/12] iommu: Add attach/detach_dev_pasid domain ops

Quoted 16 lines · Jason Gunthorpe
Quoted 2 lines · Lu Baolu

This adds a pair of common domain ops for this purpose and adds helpers to attach/detach a domain to/from a {device, PASID}.

I wonder if this should not have a detach op - after discussing with Robin we can see that detach_dev is not used in updated drivers. Instead attach_dev acts as 'set_domain'

So, it would be more symmetrical if attaching a blocking_domain to the PASID was the way to 'detach'.

This could be made straightforward by following the sketch I showed to have a static, global blocing_domain and providing a pointer to it in struct iommu_ops

Quoted 7 lines

Then 'detach pasid' is:

iommu_ops->blocking_domain->ops->attach_dev_pasid(domain, dev, pasid);

And we move away from the notion of 'detach' and in the direction that everything continuously has a domain set. PASID would logically default to blocking_domain, though we wouldn't track this anywhere.

I am not sure whether we still need to keep the blocking domain concept when we are entering the new PASID world. Please allow me to wait and listen to more opinions.

Best regards,
baolu

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

#10 lore raw

RE: [PATCH v6 03/12] iommu: Add attach/detach_dev_pasid domain ops

Quoted 26 lines

From: Baolu Lu <[email protected]>
Sent: Wednesday, May 11, 2022 10:32 AM

Quoted 3 lines · Jason Gunthorpe
Quoted 1 line · Lu Baolu

This adds a pair of common domain ops for this purpose and adds

helpers

Quoted 9 lines
Quoted 1 line

to attach/detach a domain to/from a {device, PASID}.

I wonder if this should not have a detach op - after discussing with Robin we can see that detach_dev is not used in updated drivers. Instead attach_dev acts as 'set_domain'

So, it would be more symmetrical if attaching a blocking_domain to the PASID was the way to 'detach'.

Quoted 7 lines

This could be made straightforward by following the sketch I showed to have a static, global blocing_domain and providing a pointer to it in struct iommu_ops

Then 'detach pasid' is:

iommu_ops->blocking_domain->ops->attach_dev_pasid(domain, dev,

pasid);

Quoted 1 line
Quoted 8 lines
Quoted 3 lines

And we move away from the notion of 'detach' and in the direction that everything continuously has a domain set. PASID would logically default to blocking_domain, though we wouldn't track this anywhere.

I am not sure whether we still need to keep the blocking domain concept when we are entering the new PASID world. Please allow me to wait and listen to more opinions.

I'm with Jason on this direction. In concept after a PASID is detached it's essentially blocked. Implementation-wise it doesn't prevent the iommu driver from marking the PASID entry as non-present as doing in this series instead of actually pointing to the empty page table of the block domain. But api-wise it does make the entire semantics more consistent.

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

#11 lore raw

Re: [PATCH v6 03/12] iommu: Add attach/detach_dev_pasid domain ops

Quoted 35 lines · Kevin
Quoted 26 lines

From: Baolu Lu <[email protected]>
Sent: Wednesday, May 11, 2022 10:32 AM

Quoted 3 lines · Jason Gunthorpe
Quoted 1 line · Lu Baolu

This adds a pair of common domain ops for this purpose and adds

helpers

Quoted 9 lines
Quoted 1 line

to attach/detach a domain to/from a {device, PASID}.

I wonder if this should not have a detach op - after discussing with Robin we can see that detach_dev is not used in updated drivers. Instead attach_dev acts as 'set_domain'

So, it would be more symmetrical if attaching a blocking_domain to the PASID was the way to 'detach'.

Quoted 7 lines

This could be made straightforward by following the sketch I showed to have a static, global blocing_domain and providing a pointer to it in struct iommu_ops

Then 'detach pasid' is:

iommu_ops->blocking_domain->ops->attach_dev_pasid(domain, dev,

pasid);

Quoted 1 line
Quoted 8 lines
Quoted 3 lines

And we move away from the notion of 'detach' and in the direction that everything continuously has a domain set. PASID would logically default to blocking_domain, though we wouldn't track this anywhere.

I am not sure whether we still need to keep the blocking domain concept when we are entering the new PASID world. Please allow me to wait and listen to more opinions.

Quoted 5 lines

I'm with Jason on this direction. In concept after a PASID is detached it's essentially blocked. Implementation-wise it doesn't prevent the iommu driver from marking the PASID entry as non-present as doing in this series instead of actually pointing to the empty page table of the block domain. But api-wise it does make the entire semantics more consistent.

This is all internal to IOMMU so I don't think we should be concerned about API consistency. I prefer a straighforward detach() operation because that way IOMMU drivers don't have to keep track of which domain is attached to which PASID. That code can be factored into the IOMMU core.

In addition to clearing contexts, detach() also needs to invalidate TLBs, and for that the SMMU driver needs to know the old ASID (!= PASID) that was used by the context descriptor. We can certainly work around a missing detach() to implement this, but it will be convoluted.

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

#12 lore raw

Re: [PATCH v6 03/12] iommu: Add attach/detach_dev_pasid domain ops

Quoted 20 lines · Jean-Philippe Brucker
Quoted 14 lines
Quoted 5 lines
Quoted 3 lines

Then 'detach pasid' is:

iommu_ops->blocking_domain->ops->attach_dev_pasid(domain, dev,

pasid);

Quoted 1 line
Quoted 8 lines
Quoted 3 lines

And we move away from the notion of 'detach' and in the direction that everything continuously has a domain set. PASID would logically default to blocking_domain, though we wouldn't track this anywhere.

I am not sure whether we still need to keep the blocking domain concept when we are entering the new PASID world. Please allow me to wait and listen to more opinions.

Quoted 5 lines

I'm with Jason on this direction. In concept after a PASID is detached it's essentially blocked. Implementation-wise it doesn't prevent the iommu driver from marking the PASID entry as non-present as doing in this series instead of actually pointing to the empty page table of the block domain. But api-wise it does make the entire semantics more consistent.

Quoted 4 lines

This is all internal to IOMMU so I don't think we should be concerned about API consistency. I prefer a straighforward detach() operation because that way IOMMU drivers don't have to keep track of which domain is attached to which PASID. That code can be factored into the IOMMU core.

Why would a driver need to keep additional tracking?

Quoted 4 lines

In addition to clearing contexts, detach() also needs to invalidate TLBs, and for that the SMMU driver needs to know the old ASID (!= PASID) that was used by the context descriptor. We can certainly work around a missing detach() to implement this, but it will be convoluted.

It is not "missing" it is just renamed to blocking_domain->ops->set_dev_pasid()

The implementation of that function would be identical to detach_dev_pasid.

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

#13 lore raw

Re: [PATCH v6 03/12] iommu: Add attach/detach_dev_pasid domain ops

Quoted 33 lines · Jason Gunthorpe
Quoted 20 lines · Jean-Philippe Brucker
Quoted 14 lines
Quoted 5 lines
Quoted 3 lines

Then 'detach pasid' is:

iommu_ops->blocking_domain->ops->attach_dev_pasid(domain, dev,

pasid);

Quoted 1 line
Quoted 8 lines
Quoted 3 lines

And we move away from the notion of 'detach' and in the direction that everything continuously has a domain set. PASID would logically default to blocking_domain, though we wouldn't track this anywhere.

I am not sure whether we still need to keep the blocking domain concept when we are entering the new PASID world. Please allow me to wait and listen to more opinions.

Quoted 5 lines

I'm with Jason on this direction. In concept after a PASID is detached it's essentially blocked. Implementation-wise it doesn't prevent the iommu driver from marking the PASID entry as non-present as doing in this series instead of actually pointing to the empty page table of the block domain. But api-wise it does make the entire semantics more consistent.

Quoted 4 lines

This is all internal to IOMMU so I don't think we should be concerned about API consistency. I prefer a straighforward detach() operation because that way IOMMU drivers don't have to keep track of which domain is attached to which PASID. That code can be factored into the IOMMU core.

Why would a driver need to keep additional tracking?

Quoted 4 lines

In addition to clearing contexts, detach() also needs to invalidate TLBs, and for that the SMMU driver needs to know the old ASID (!= PASID) that was used by the context descriptor. We can certainly work around a missing detach() to implement this, but it will be convoluted.

Quoted 4 lines

It is not "missing" it is just renamed to blocking_domain->ops->set_dev_pasid()

The implementation of that function would be identical to detach_dev_pasid.

attach(dev, pasid, sva_domain)
detach(dev, pasid, sva_domain)

versus

set_dev_pasid(dev, pasid, sva_domain)
set_dev_pasid(dev, pasid, blocking)

we loose the information of the domain previously attached, and the SMMU driver has to retrieve it to find the ASID corresponding to the mm.

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

#14 lore raw

Re: [PATCH v6 03/12] iommu: Add attach/detach_dev_pasid domain ops

Quoted 8 lines · Jean-Philippe Brucker
Quoted 4 lines

It is not "missing" it is just renamed to blocking_domain->ops->set_dev_pasid()

The implementation of that function would be identical to detach_dev_pasid.

attach(dev, pasid, sva_domain)
detach(dev, pasid, sva_domain)

Quoted 7 lines

versus

set_dev_pasid(dev, pasid, sva_domain)
set_dev_pasid(dev, pasid, blocking)

we loose the information of the domain previously attached, and the SMMU driver has to retrieve it to find the ASID corresponding to the mm.

It would be easy to have the old domain be an input as well - the core code knows it.

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

author maintainer
#15 lore raw

Re: [PATCH v6 03/12] iommu: Add attach/detach_dev_pasid domain ops

Quoted 12 lines · Jason Gunthorpe
Quoted 10 lines · Jean-Philippe Brucker
Quoted 4 lines

It is not "missing" it is just renamed to blocking_domain->ops->set_dev_pasid()

The implementation of that function would be identical to detach_dev_pasid.

attach(dev, pasid, sva_domain)
detach(dev, pasid, sva_domain)

versus

Quoted 8 lines
Quoted 5 lines

set_dev_pasid(dev, pasid, sva_domain)
set_dev_pasid(dev, pasid, blocking)

we loose the information of the domain previously attached, and the SMMU driver has to retrieve it to find the ASID corresponding to the mm.

It would be easy to have the old domain be an input as well - the core code knows it.

Thanks a lot for all suggestions. I have posted a follow-up series for this:

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

Let's discuss this there.

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

author maintainer
#16 lore raw

PATCH v6 4/12 iommu/sva: Basic data structures for SVA

In reply to Lu Baolu

Use below data structures for SVA implementation in the IOMMU core:

- struct iommu_domain (IOMMU_DOMAIN_SVA type)
Represent a hardware pagetable that the IOMMU hardware could use for SVA translation. Multiple iommu domains could be bound with an SVA mm and each grabs a mm_count of the mm in order to make sure mm could only be freed after all domains have been unbound. A new mm field is added to struct iommu_domain and a helper is added to retrieve mm from a domain pointer.

- struct iommu_sva (existing)
Represent a bond relationship between an SVA ioas and an iommu domain. If a bond already exists, it's reused and a reference is taken.

- struct dev_iommu::sva_bonds
A pasid-indexed xarray to track the bonds happened on the device.

---
 include/linux/iommu.h | 13 +++++++++++++
 drivers/iommu/iommu.c |  3 +++
 2 files changed, 16 insertions(+)
include/linux/iommu.h +13 -0
index ab36244d4e94..2921e634491e 100644
@@ -64,6 +64,9 @@ 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_SHARED	(1U << 4)  /* Page table shared from CPU  */
+#define __IOMMU_DOMAIN_HOST_VA	(1U << 5)  /* Host CPU virtual address */
+
 /*
  * This are the possible domain-types
  *
@@ -86,6 +89,8 @@ 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_SHARED |	\
+				 __IOMMU_DOMAIN_HOST_VA)
 
 struct iommu_domain {
 	unsigned type;
@@ -95,6 +100,9 @@ struct iommu_domain {
 	void *handler_token;
 	struct iommu_domain_geometry geometry;
 	struct iommu_dma_cookie *iova_cookie;
+#ifdef CONFIG_IOMMU_SVA
+	struct mm_struct *mm;
+#endif
 };
 
 static inline bool iommu_is_dma_domain(struct iommu_domain *domain)
@@ -380,6 +388,9 @@ struct dev_iommu {
 	struct iommu_device		*iommu_dev;
 	void				*priv;
 	unsigned int			pasid_bits;
+#ifdef CONFIG_IOMMU_SVA
+	struct xarray			sva_bonds;
+#endif
 };
 
 int iommu_device_register(struct iommu_device *iommu,
@@ -629,6 +640,8 @@ struct iommu_fwspec {
  */
 struct iommu_sva {
 	struct device			*dev;
+	struct iommu_domain		*domain;
+	refcount_t			users;
 };
 
 int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
drivers/iommu/iommu.c +3 -0
index 16e8db2d86fc..1abff5fc9554 100644
@@ -202,6 +202,9 @@ static struct dev_iommu *dev_iommu_get(struct device *dev)
 		return NULL;
 
 	mutex_init(&param->lock);
+#ifdef CONFIG_IOMMU_SVA
+	xa_init(&param->sva_bonds);
+#endif
 	dev->iommu = param;
 	return param;
 }
Signature · 6 lines
2.25.1

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

PATCH v6 5/12 iommu/vt-d: 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 in the Intel IOMMU driver. The device driver is suggested to handle kernel DMA with PASID through the kernel DMA APIs.

---
 drivers/iommu/intel/svm.c | 53 +++++++++------------------------------
 1 file changed, 12 insertions(+), 41 deletions(-)
drivers/iommu/intel/svm.c +12 -41
index 7ee37d996e15..574ddddaa33a 100644
@@ -313,8 +313,7 @@ static int pasid_to_svm_sdev(struct device *dev, unsigned int pasid,
 	return 0;
 }
 
-static int intel_svm_alloc_pasid(struct device *dev, struct mm_struct *mm,
-				 unsigned int flags)
+static int intel_svm_alloc_pasid(struct device *dev, struct mm_struct *mm)
 {
 	ioasid_t max_pasid = dev_is_pci(dev) ?
 			pci_max_pasids(to_pci_dev(dev)) : intel_pasid_max_id;
@@ -324,8 +323,7 @@ static int intel_svm_alloc_pasid(struct device *dev, struct mm_struct *mm,
 
 static struct iommu_sva *intel_svm_bind_mm(struct intel_iommu *iommu,
 					   struct device *dev,
-					   struct mm_struct *mm,
-					   unsigned int flags)
+					   struct mm_struct *mm)
 {
 	struct device_domain_info *info = dev_iommu_priv_get(dev);
 	unsigned long iflags, sflags;
@@ -341,22 +339,18 @@ static struct iommu_sva *intel_svm_bind_mm(struct intel_iommu *iommu,
 
 		svm->pasid = mm->pasid;
 		svm->mm = mm;
-		svm->flags = flags;
 		INIT_LIST_HEAD_RCU(&svm->devs);
 
-		if (!(flags & SVM_FLAG_SUPERVISOR_MODE)) {
-			svm->notifier.ops = &intel_mmuops;
-			ret = mmu_notifier_register(&svm->notifier, mm);
-			if (ret) {
-				kfree(svm);
-				return ERR_PTR(ret);
-			}
+		svm->notifier.ops = &intel_mmuops;
+		ret = mmu_notifier_register(&svm->notifier, mm);
+		if (ret) {
+			kfree(svm);
+			return ERR_PTR(ret);
 		}
 
 		ret = pasid_private_add(svm->pasid, svm);
 		if (ret) {
-			if (svm->notifier.ops)
-				mmu_notifier_unregister(&svm->notifier, mm);
+			mmu_notifier_unregister(&svm->notifier, mm);
 			kfree(svm);
 			return ERR_PTR(ret);
 		}
@@ -391,9 +385,7 @@ static struct iommu_sva *intel_svm_bind_mm(struct intel_iommu *iommu,
 	}
 
 	/* Setup the pasid table: */
-	sflags = (flags & SVM_FLAG_SUPERVISOR_MODE) ?
-			PASID_FLAG_SUPERVISOR_MODE : 0;
-	sflags |= cpu_feature_enabled(X86_FEATURE_LA57) ? PASID_FLAG_FL5LP : 0;
+	sflags = cpu_feature_enabled(X86_FEATURE_LA57) ? PASID_FLAG_FL5LP : 0;
 	spin_lock_irqsave(&iommu->lock, iflags);
 	ret = intel_pasid_setup_first_level(iommu, dev, mm->pgd, mm->pasid,
 					    FLPT_DEFAULT_DID, sflags);
@@ -410,8 +402,7 @@ static struct iommu_sva *intel_svm_bind_mm(struct intel_iommu *iommu,
 	kfree(sdev);
 free_svm:
 	if (list_empty(&svm->devs)) {
-		if (svm->notifier.ops)
-			mmu_notifier_unregister(&svm->notifier, mm);
+		mmu_notifier_unregister(&svm->notifier, mm);
 		pasid_private_remove(mm->pasid);
 		kfree(svm);
 	}
@@ -821,37 +812,17 @@ static irqreturn_t prq_event_thread(int irq, void *d)
 struct iommu_sva *intel_svm_bind(struct device *dev, struct mm_struct *mm, void *drvdata)
 {
 	struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL);
-	unsigned int flags = 0;
 	struct iommu_sva *sva;
 	int ret;
 
-	if (drvdata)
-		flags = *(unsigned int *)drvdata;
-
-	if (flags & SVM_FLAG_SUPERVISOR_MODE) {
-		if (!ecap_srs(iommu->ecap)) {
-			dev_err(dev, "%s: Supervisor PASID not supported\n",
-				iommu->name);
-			return ERR_PTR(-EOPNOTSUPP);
-		}
-
-		if (mm) {
-			dev_err(dev, "%s: Supervisor PASID with user provided mm\n",
-				iommu->name);
-			return ERR_PTR(-EINVAL);
-		}
-
-		mm = &init_mm;
-	}
-
 	mutex_lock(&pasid_mutex);
-	ret = intel_svm_alloc_pasid(dev, mm, flags);
+	ret = intel_svm_alloc_pasid(dev, mm);
 	if (ret) {
 		mutex_unlock(&pasid_mutex);
 		return ERR_PTR(ret);
 	}
 
-	sva = intel_svm_bind_mm(iommu, dev, mm, flags);
+	sva = intel_svm_bind_mm(iommu, dev, mm);
 	mutex_unlock(&pasid_mutex);
 
 	return sva;
Signature · 6 lines
2.25.1

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Reviewed-by on v6 5/12
#18 lore raw

Re: [PATCH v6 05/12] iommu/vt-d: Remove SVM_FLAG_SUPERVISOR_MODE support

Quoted 17 lines · 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 in the Intel IOMMU driver. The device driver is suggested to handle kernel DMA with PASID through the kernel DMA APIs.

Quoted 6 lines
---
 drivers/iommu/intel/svm.c | 53 +++++++++------------------------------
 1 file changed, 12 insertions(+), 41 deletions(-)

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

author maintainer
#19 lore raw

PATCH v6 6/12 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.

---
 include/linux/intel-iommu.h |  5 ++++
 drivers/iommu/intel/iommu.c |  2 ++
 drivers/iommu/intel/svm.c   | 48 +++++++++++++++++++++++++++++++++++++
 3 files changed, 55 insertions(+)
include/linux/intel-iommu.h +5 -0
index 72e5d7900e71..2397c2007cda 100644
@@ -744,6 +744,7 @@ 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);
 
 struct intel_svm_dev {
 	struct list_head list;
@@ -769,6 +770,10 @@ 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;
+}
 #endif
 
 #ifdef CONFIG_INTEL_IOMMU_DEBUGFS
drivers/iommu/intel/iommu.c +2 -0
index 99643f897f26..10b1e9dcbd98 100644
@@ -4343,6 +4343,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 +48 -0

51 lines not on this page · raw message

Signature · 6 lines
2.25.1

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

PATCH v6 7/12 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.

---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h   |  6 ++
 .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c   | 68 +++++++++++++++++++
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c   |  3 +
 3 files changed, 77 insertions(+)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +6 -0
index cd48590ada30..e077f21e2528 100644
@@ -759,6 +759,7 @@ 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);
 #else /* CONFIG_ARM_SMMU_V3_SVA */
 static inline bool arm_smmu_sva_supported(struct arm_smmu_device *smmu)
 {
@@ -804,5 +805,10 @@ 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;
+}
 #endif /* CONFIG_ARM_SMMU_V3_SVA */
 #endif /* _ARM_SMMU_V3_H */
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +68 -0

71 lines not on this page · raw message

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -0
index afc63fce6107..9daf3de7e539 100644
@@ -1999,6 +1999,9 @@ static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
 {
 	struct arm_smmu_domain *smmu_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 · 6 lines
2.25.1

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

PATCH v6 8/12 iommu/sva: Use attach/detach_pasid_dev in SVA interfaces

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 attach/detach_pasid_dev ops and align them with the concept of the iommu domain. Put the new SVA code in the sva related file in order to make it self-contained.

---
 include/linux/iommu.h         |  44 ++++++-----
 drivers/iommu/iommu-sva-lib.c | 145 ++++++++++++++++++++++++++++++++++
 drivers/iommu/iommu.c         |  92 ---------------------
 3 files changed, 168 insertions(+), 113 deletions(-)
include/linux/iommu.h +23 -21
index 2921e634491e..5a3ef4d58b1f 100644
@@ -684,12 +684,6 @@ 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);
 bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features f);
 
-struct iommu_sva *iommu_sva_bind_device(struct device *dev,
-					struct mm_struct *mm,
-					void *drvdata);
-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);
 
@@ -1031,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, void *drvdata)
-{
-	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;
@@ -1087,6 +1066,29 @@ static inline void iommu_detach_device_pasid(struct iommu_domain *domain,
 }
 #endif /* CONFIG_IOMMU_API */
 
+#ifdef CONFIG_IOMMU_SVA
+struct iommu_sva *iommu_sva_bind_device(struct device *dev,
+					struct mm_struct *mm,
+					void *drvdata);
+void iommu_sva_unbind_device(struct iommu_sva *handle);
+u32 iommu_sva_get_pasid(struct iommu_sva *handle);
+#else /* CONFIG_IOMMU_SVA */
+static inline struct iommu_sva *
+iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata)
+{
+	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 */
+
 /**
  * iommu_map_sgtable - Map the given buffer to the IOMMU domain
  * @domain:	The IOMMU domain to perform the mapping
drivers/iommu/iommu-sva-lib.c +145 -0

154 lines not on this page · raw message

drivers/iommu/iommu.c +0 -92

98 lines not on this page · raw message

Signature · 6 lines
2.25.1

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

Re: [PATCH v6 08/12] iommu/sva: Use attach/detach_pasid_dev in SVA interfaces

Quoted 19 lines · Lu Baolu
drivers/iommu/iommu-sva-lib.c +19 -0
+/**
+ * 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
+ * @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
+ * @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, void *drvdata)
+{

drivers/iommu/iommu-sva-lib.c struct mm_struct *iommu_sva_find(ioasid_t pasid) · line 132

Quoted 10 lines
drivers/iommu/iommu-sva-lib.c +10 -0
+	int ret = -EINVAL;
+	struct iommu_sva *handle;
+	struct iommu_domain *domain;
+
+	/*
+	 * TODO: Remove the drvdata parameter after kernel PASID support is
+	 * enabled for the idxd driver.
+	 */
+	if (drvdata)
+		return ERR_PTR(-EOPNOTSUPP);

Why is this being left behind? Clean up the callers too please.

drivers/iommu/iommu-sva-lib.c struct mm_struct *iommu_sva_find(ioasid_t pasid) · line 145

Quoted 12 lines
drivers/iommu/iommu-sva-lib.c +12 -0
+	/* Allocate mm->pasid if necessary. */
+	ret = iommu_sva_alloc_pasid(mm, 1, (1U << dev->iommu->pasid_bits) - 1);
+	if (ret)
+		return ERR_PTR(ret);
+
+	mutex_lock(&iommu_sva_lock);
+	/* Search for an existing bond. */
+	handle = xa_load(&dev->iommu->sva_bonds, mm->pasid);
+	if (handle) {
+		refcount_inc(&handle->users);
+		goto out_success;
+	}

How can there be an existing bond?

dev->iommu is per-device

The device_group_immutable_singleton() insists on a single device group

Basically 'sva_bonds' is the same thing as the group->pasid_array.

Assuming we leave room for multi-device groups this logic should just be

	group = iommu_group_get(dev);
	if (!group)
		return -ENODEV;

	mutex_lock(&group->mutex);
	domain = xa_load(&group->pasid_array, mm->pasid);
	if (!domain || domain->type != IOMMU_DOMAIN_SVA || domain->mm != mm)
		domain = iommu_sva_alloc_domain(dev, mm);

?

And stick the refcount in the sva_domain

Also, given the current arrangement it might make sense to have a struct iommu_domain_sva given that no driver is wrappering this in something else.

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

author maintainer
#23 lore raw

Re: [PATCH v6 08/12] iommu/sva: Use attach/detach_pasid_dev in SVA interfaces

Quoted 25 lines · Jason Gunthorpe
Quoted 23 lines · Lu Baolu
drivers/iommu/iommu-sva-lib.c +23 -0
+/**
+ * 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
+ * @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
+ * @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, void *drvdata)
+{
+	int ret = -EINVAL;
+	struct iommu_sva *handle;
+	struct iommu_domain *domain;
+
Quoted 8 lines

drivers/iommu/iommu-sva-lib.c struct mm_struct *iommu_sva_find(ioasid_t pasid) · line 132

Quoted 6 lines
drivers/iommu/iommu-sva-lib.c +6 -0
+	/*
+	 * TODO: Remove the drvdata parameter after kernel PASID support is
+	 * enabled for the idxd driver.
+	 */
+	if (drvdata)
+		return ERR_PTR(-EOPNOTSUPP);

Why is this being left behind? Clean up the callers too please.

Okay, let me try to.

Quoted 16 lines

drivers/iommu/iommu-sva-lib.c struct mm_struct *iommu_sva_find(ioasid_t pasid) · line 145

Quoted 13 lines
drivers/iommu/iommu-sva-lib.c +12 -0
+	/* Allocate mm->pasid if necessary. */
+	ret = iommu_sva_alloc_pasid(mm, 1, (1U << dev->iommu->pasid_bits) - 1);
+	if (ret)
+		return ERR_PTR(ret);
+
+	mutex_lock(&iommu_sva_lock);
+	/* Search for an existing bond. */
+	handle = xa_load(&dev->iommu->sva_bonds, mm->pasid);
+	if (handle) {
+		refcount_inc(&handle->users);
+		goto out_success;
+	}

How can there be an existing bond?

Quoted 6 lines

dev->iommu is per-device

The device_group_immutable_singleton() insists on a single device group

Basically 'sva_bonds' is the same thing as the group->pasid_array.

Yes, really.

Quoted 13 lines

Assuming we leave room for multi-device groups this logic should just be

	group = iommu_group_get(dev);
	if (!group)
		return -ENODEV;

	mutex_lock(&group->mutex);
	domain = xa_load(&group->pasid_array, mm->pasid);
	if (!domain || domain->type != IOMMU_DOMAIN_SVA || domain->mm != mm)
		domain = iommu_sva_alloc_domain(dev, mm);
Quoted 1 line

?

Agreed. As a helper in iommu core, how about making it more generic like below?

+struct iommu_domain *iommu_get_domain_for_dev_pasid(struct device *dev,
+ iosid_t pasid,
+ unsigned int type)
+{
+ struct iommu_domain *domain;
+ struct iommu_group *group;
+
+ if (!pasid_valid(pasid))
+ return NULL;
+
+ group = iommu_group_get(dev);
+ if (!group)
+ return NULL;
+

+       mutex_lock(&group->mutex);
+       domain = xa_load(&group->pasid_array, pasid);
+       if (domain && domain->type != type)

+ domain = NULL;
+ mutex_unlock(&group->mutex);
+ iommu_group_put(group);
+
+ return domain;
+}

Quoted 6 lines

And stick the refcount in the sva_domain

Also, given the current arrangement it might make sense to have a struct iommu_domain_sva given that no driver is wrappering this in something else.

Fair enough. How about below wrapper?

+struct iommu_sva_domain {
+ /*

+        * Common iommu domain header, *must* be put at the top
+        * of the structure.
+        */
+       struct iommu_domain domain;
+       struct mm_struct *mm;
+       struct iommu_sva bond;

+}

The refcount is wrapped in bond.

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

#24 lore raw

Re: [PATCH v6 08/12] iommu/sva: Use attach/detach_pasid_dev in SVA interfaces

Quoted 75 lines · Baolu Lu
Quoted 25 lines · Jason Gunthorpe
Quoted 23 lines · Lu Baolu
drivers/iommu/iommu-sva-lib.c +23 -0
+/**
+ * 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
+ * @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
+ * @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, void *drvdata)
+{
+	int ret = -EINVAL;
+	struct iommu_sva *handle;
+	struct iommu_domain *domain;
+
Quoted 8 lines

drivers/iommu/iommu-sva-lib.c struct mm_struct *iommu_sva_find(ioasid_t pasid) · line 132

Quoted 6 lines
drivers/iommu/iommu-sva-lib.c +6 -0
+	/*
+	 * TODO: Remove the drvdata parameter after kernel PASID support is
+	 * enabled for the idxd driver.
+	 */
+	if (drvdata)
+		return ERR_PTR(-EOPNOTSUPP);

Why is this being left behind? Clean up the callers too please.

Okay, let me try to.

Quoted 16 lines

drivers/iommu/iommu-sva-lib.c struct mm_struct *iommu_sva_find(ioasid_t pasid) · line 145

Quoted 13 lines
drivers/iommu/iommu-sva-lib.c +12 -0
+	/* Allocate mm->pasid if necessary. */
+	ret = iommu_sva_alloc_pasid(mm, 1, (1U << dev->iommu->pasid_bits) - 1);
+	if (ret)
+		return ERR_PTR(ret);
+
+	mutex_lock(&iommu_sva_lock);
+	/* Search for an existing bond. */
+	handle = xa_load(&dev->iommu->sva_bonds, mm->pasid);
+	if (handle) {
+		refcount_inc(&handle->users);
+		goto out_success;
+	}

How can there be an existing bond?

Quoted 6 lines

dev->iommu is per-device

The device_group_immutable_singleton() insists on a single device group

Basically 'sva_bonds' is the same thing as the group->pasid_array.

Yes, really.

Quoted 13 lines

Assuming we leave room for multi-device groups this logic should just be

	group = iommu_group_get(dev);
	if (!group)
		return -ENODEV;

	mutex_lock(&group->mutex);
	domain = xa_load(&group->pasid_array, mm->pasid);
	if (!domain || domain->type != IOMMU_DOMAIN_SVA || domain->mm != mm)
		domain = iommu_sva_alloc_domain(dev, mm);
Quoted 4 lines
Quoted 1 line

?

Agreed. As a helper in iommu core, how about making it more generic like below?

IDK, is there more users of this? AFAIK SVA is the only place that will be auto-sharing?

Review

Quoted 8 lines
(quoted diff) +8 -0
+       mutex_lock(&group->mutex);
+       domain = xa_load(&group->pasid_array, pasid);
+       if (domain && domain->type != type)
+               domain = NULL;
+       mutex_unlock(&group->mutex);
+       iommu_group_put(group);
+
+       return domain;

This is bad locking, group->pasid_array values cannot be taken outside the lock.

Quoted 18 lines
Quoted 5 lines

And stick the refcount in the sva_domain

Also, given the current arrangement it might make sense to have a struct iommu_domain_sva given that no driver is wrappering this in something else.

Fair enough. How about below wrapper?

(quoted diff) +9 -0
+struct iommu_sva_domain {
+       /*
+        * Common iommu domain header, *must* be put at the top
+        * of the structure.
+        */
+       struct iommu_domain domain;
+       struct mm_struct *mm;
+       struct iommu_sva bond;
+}
Quoted 1 line

The refcount is wrapped in bond.

I'm still not sure that bond is necessary

But yes, something like that

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

author maintainer
#25 lore raw

Re: [PATCH v6 08/12] iommu/sva: Use attach/detach_pasid_dev in SVA interfaces

Quoted 12 lines · Jason Gunthorpe
Quoted 12 lines
Quoted 12 lines

Assuming we leave room for multi-device groups this logic should just be

	group = iommu_group_get(dev);
	if (!group)
		return -ENODEV;

	mutex_lock(&group->mutex);
	domain = xa_load(&group->pasid_array, mm->pasid);
	if (!domain || domain->type != IOMMU_DOMAIN_SVA || domain->mm != mm)
		domain = iommu_sva_alloc_domain(dev, mm);
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

?

Agreed. As a helper in iommu core, how about making it more generic like below?

IDK, is there more users of this? AFAIK SVA is the only place that will be auto-sharing?

The generic thing is that components, like SVA, want to fetch the attached domain from the iommu core.

Quoted 11 lines

Review

Quoted 9 lines
(quoted diff) +8 -0
+       mutex_lock(&group->mutex);
+       domain = xa_load(&group->pasid_array, pasid);
+       if (domain && domain->type != type)
+               domain = NULL;
+       mutex_unlock(&group->mutex);
+       iommu_group_put(group);
+
+       return domain;

This is bad locking, group->pasid_array values cannot be taken outside the lock.

It's not iommu core, but SVA (or other feature components) that manage the life cycle of a domain. The iommu core only provides a place to store the domain pointer. The feature components are free to fetch their domain pointers from iommu core as long as they are sure that the domain is alive during use.

Quoted 18 lines
Quoted 18 lines
Quoted 6 lines

And stick the refcount in the sva_domain

Also, given the current arrangement it might make sense to have a struct iommu_domain_sva given that no driver is wrappering this in something else.

Fair enough. How about below wrapper?

(quoted diff) +9 -0
+struct iommu_sva_domain {
+       /*
+        * Common iommu domain header,*must*  be put at the top
+        * of the structure.
+        */
+       struct iommu_domain domain;
+       struct mm_struct *mm;
+       struct iommu_sva bond;
+}
Quoted 2 lines
Quoted 1 line

The refcount is wrapped in bond.

I'm still not sure that bond is necessary

"bond" is the sva handle that the device drivers get through calling iommu_sva_bind().

Quoted 2 lines

But yes, something like that

Best regards,
baolu

#26 lore raw

RE: [PATCH v6 08/12] iommu/sva: Use attach/detach_pasid_dev in SVA interfaces

Quoted 19 lines

From: Baolu Lu <[email protected]>
Sent: Thursday, May 12, 2022 11:03 AM

Quoted 15 lines · Jason Gunthorpe
Quoted 15 lines
Quoted 3 lines

Also, given the current arrangement it might make sense to have a struct iommu_domain_sva given that no driver is wrappering this in something else.

Fair enough. How about below wrapper?

(quoted diff) +9 -0
+struct iommu_sva_domain {
+       /*
+        * Common iommu domain header,*must*  be put at the top
+        * of the structure.
+        */
+       struct iommu_domain domain;
+       struct mm_struct *mm;
+       struct iommu_sva bond;
+}
Quoted 6 lines
Quoted 2 lines
Quoted 1 line

The refcount is wrapped in bond.

I'm still not sure that bond is necessary

"bond" is the sva handle that the device drivers get through calling iommu_sva_bind().

'bond' was required before because we didn't have a domain to wrap the page table at that time.

Now we have a domain and it is 1:1 associated to bond. Probably make sense now by just returning the domain as the sva handle instead?
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

author maintainer
#27 lore raw

Re: [PATCH v6 08/12] iommu/sva: Use attach/detach_pasid_dev in SVA interfaces

Quoted 26 lines · Kevin
Quoted 19 lines

From: Baolu Lu <[email protected]>
Sent: Thursday, May 12, 2022 11:03 AM

Quoted 15 lines · Jason Gunthorpe
Quoted 15 lines
Quoted 3 lines

Also, given the current arrangement it might make sense to have a struct iommu_domain_sva given that no driver is wrappering this in something else.

Fair enough. How about below wrapper?

(quoted diff) +9 -0
+struct iommu_sva_domain {
+       /*
+        * Common iommu domain header,*must*  be put at the top
+        * of the structure.
+        */
+       struct iommu_domain domain;
+       struct mm_struct *mm;
+       struct iommu_sva bond;
+}
Quoted 6 lines
Quoted 2 lines
Quoted 1 line

The refcount is wrapped in bond.

I'm still not sure that bond is necessary

"bond" is the sva handle that the device drivers get through calling iommu_sva_bind().

Quoted 6 lines

'bond' was required before because we didn't have a domain to wrap the page table at that time.

Now we have a domain and it is 1:1 associated to bond. Probably make sense now by just returning the domain as the sva handle instead?

It also includes the device information that the domain has been attached. So the sva_unbind() looks like this:

/**
* 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)

It's fine to replace the iommu_sva with iommu_sva_domain for sva handle, if we can include the device in the unbind() interface.

Anyway, I'd expect to achieve all these in two steps:

- sva and iopf refactoring, only iommu internal changes;
- sva interface refactoring, only interface changes.

Does above work?

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

#28 lore raw

RE: [PATCH v6 08/12] iommu/sva: Use attach/detach_pasid_dev in SVA interfaces

Quoted 52 lines

From: Baolu Lu <[email protected]>
Sent: Thursday, May 12, 2022 1:17 PM

Quoted 26 lines · Kevin
Quoted 19 lines

From: Baolu Lu <[email protected]>
Sent: Thursday, May 12, 2022 11:03 AM

Quoted 15 lines · Jason Gunthorpe
Quoted 15 lines
Quoted 3 lines

Also, given the current arrangement it might make sense to have a struct iommu_domain_sva given that no driver is wrappering this in something else.

Fair enough. How about below wrapper?

(quoted diff) +9 -0
+struct iommu_sva_domain {
+       /*
+        * Common iommu domain header,*must*  be put at the top
+        * of the structure.
+        */
+       struct iommu_domain domain;
+       struct mm_struct *mm;
+       struct iommu_sva bond;
+}
Quoted 6 lines
Quoted 2 lines
Quoted 1 line

The refcount is wrapped in bond.

I'm still not sure that bond is necessary

"bond" is the sva handle that the device drivers get through calling iommu_sva_bind().

Quoted 6 lines

'bond' was required before because we didn't have a domain to wrap the page table at that time.

Now we have a domain and it is 1:1 associated to bond. Probably make sense now by just returning the domain as the sva handle instead?

It also includes the device information that the domain has been attached. So the sva_unbind() looks like this:

/**
* 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)

Quoted 2 lines

It's fine to replace the iommu_sva with iommu_sva_domain for sva handle, if we can include the device in the unbind() interface.

can we just have unbind(domain, device)?

Quoted 10 lines

Anyway, I'd expect to achieve all these in two steps:

- sva and iopf refactoring, only iommu internal changes;
- sva interface refactoring, only interface changes.

Does above work?

Best regards,
baolu

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

author maintainer
#29 lore raw

Re: [PATCH v6 08/12] iommu/sva: Use attach/detach_pasid_dev in SVA interfaces

Quoted 52 lines · Kevin
Quoted 52 lines

From: Baolu Lu <[email protected]>
Sent: Thursday, May 12, 2022 1:17 PM

Quoted 26 lines · Kevin
Quoted 19 lines

From: Baolu Lu <[email protected]>
Sent: Thursday, May 12, 2022 11:03 AM

Quoted 15 lines · Jason Gunthorpe
Quoted 15 lines
Quoted 3 lines

Also, given the current arrangement it might make sense to have a struct iommu_domain_sva given that no driver is wrappering this in something else.

Fair enough. How about below wrapper?

(quoted diff) +9 -0
+struct iommu_sva_domain {
+       /*
+        * Common iommu domain header,*must*  be put at the top
+        * of the structure.
+        */
+       struct iommu_domain domain;
+       struct mm_struct *mm;
+       struct iommu_sva bond;
+}
Quoted 6 lines
Quoted 2 lines
Quoted 1 line

The refcount is wrapped in bond.

I'm still not sure that bond is necessary

"bond" is the sva handle that the device drivers get through calling iommu_sva_bind().

Quoted 6 lines

'bond' was required before because we didn't have a domain to wrap the page table at that time.

Now we have a domain and it is 1:1 associated to bond. Probably make sense now by just returning the domain as the sva handle instead?

It also includes the device information that the domain has been attached. So the sva_unbind() looks like this:

/**
* 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)

Quoted 4 lines
Quoted 2 lines

It's fine to replace the iommu_sva with iommu_sva_domain for sva handle, if we can include the device in the unbind() interface.

can we just have unbind(domain, device)?

Yes. With this, we can remove bond.

This could be done in below phase 2.

Quoted 11 lines
Quoted 11 lines

Anyway, I'd expect to achieve all these in two steps:

- sva and iopf refactoring, only iommu internal changes;
- sva interface refactoring, only interface changes.

Does above work?

Best regards,
baolu

Best regards,
baolu

#30 lore raw

Re: [PATCH v6 08/12] iommu/sva: Use attach/detach_pasid_dev in SVA interfaces

In reply to Baolu Lu
Quoted 49 lines · Baolu Lu
Quoted 26 lines · Kevin
Quoted 19 lines

From: Baolu Lu <[email protected]>
Sent: Thursday, May 12, 2022 11:03 AM

Quoted 15 lines · Jason Gunthorpe
Quoted 15 lines
Quoted 3 lines

Also, given the current arrangement it might make sense to have a struct iommu_domain_sva given that no driver is wrappering this in something else.

Fair enough. How about below wrapper?

(quoted diff) +9 -0
+struct iommu_sva_domain {
+       /*
+        * Common iommu domain header,*must*  be put at the top
+        * of the structure.
+        */
+       struct iommu_domain domain;
+       struct mm_struct *mm;
+       struct iommu_sva bond;
+}
Quoted 6 lines
Quoted 2 lines
Quoted 1 line

The refcount is wrapped in bond.

I'm still not sure that bond is necessary

"bond" is the sva handle that the device drivers get through calling iommu_sva_bind().

Quoted 6 lines

'bond' was required before because we didn't have a domain to wrap the page table at that time.

Now we have a domain and it is 1:1 associated to bond. Probably make sense now by just returning the domain as the sva handle instead?

It also includes the device information that the domain has been attached. So the sva_unbind() looks like this:

/**
* 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)

Quoted 2 lines

It's fine to replace the iommu_sva with iommu_sva_domain for sva handle, if we can include the device in the unbind() interface.

Why would we have a special unbind for SVA?

SVA should not different from normal domains it should use the normal detach flow too.

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

author maintainer
#31 lore raw

Re: [PATCH v6 08/12] iommu/sva: Use attach/detach_pasid_dev in SVA interfaces

Quoted 50 lines · Jason Gunthorpe
Quoted 49 lines · Baolu Lu
Quoted 26 lines · Kevin
Quoted 19 lines

From: Baolu Lu <[email protected]>
Sent: Thursday, May 12, 2022 11:03 AM

Quoted 15 lines · Jason Gunthorpe
Quoted 15 lines
Quoted 3 lines

Also, given the current arrangement it might make sense to have a struct iommu_domain_sva given that no driver is wrappering this in something else.

Fair enough. How about below wrapper?

(quoted diff) +9 -0
+struct iommu_sva_domain {
+       /*
+        * Common iommu domain header,*must*  be put at the top
+        * of the structure.
+        */
+       struct iommu_domain domain;
+       struct mm_struct *mm;
+       struct iommu_sva bond;
+}
Quoted 6 lines
Quoted 2 lines
Quoted 1 line

The refcount is wrapped in bond.

I'm still not sure that bond is necessary

"bond" is the sva handle that the device drivers get through calling iommu_sva_bind().

Quoted 6 lines

'bond' was required before because we didn't have a domain to wrap the page table at that time.

Now we have a domain and it is 1:1 associated to bond. Probably make sense now by just returning the domain as the sva handle instead?

It also includes the device information that the domain has been attached. So the sva_unbind() looks like this:

/**
* 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)

Quoted 4 lines
Quoted 2 lines

It's fine to replace the iommu_sva with iommu_sva_domain for sva handle, if we can include the device in the unbind() interface.

Why would we have a special unbind for SVA?

It's about SVA kAPI for device drivers. The existing kAPIs include:

struct iommu_sva *iommu_sva_bind_device(struct device *dev,
struct mm_struct *mm, void *drvdata);
void iommu_sva_unbind_device(struct iommu_sva *handle); u32 iommu_sva_get_pasid(struct iommu_sva *handle);

Quoted 2 lines

SVA should not different from normal domains it should use the normal detach flow too.

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

#32 lore raw

Re: [PATCH v6 08/12] iommu/sva: Use attach/detach_pasid_dev in SVA interfaces

Quoted 56 lines · Baolu Lu
Quoted 50 lines · Jason Gunthorpe
Quoted 49 lines · Baolu Lu
Quoted 26 lines · Kevin
Quoted 19 lines

From: Baolu Lu <[email protected]>
Sent: Thursday, May 12, 2022 11:03 AM

Quoted 15 lines · Jason Gunthorpe
Quoted 15 lines
Quoted 3 lines

Also, given the current arrangement it might make sense to have a struct iommu_domain_sva given that no driver is wrappering this in something else.

Fair enough. How about below wrapper?

(quoted diff) +9 -0
+struct iommu_sva_domain {
+       /*
+        * Common iommu domain header,*must*  be put at the top
+        * of the structure.
+        */
+       struct iommu_domain domain;
+       struct mm_struct *mm;
+       struct iommu_sva bond;
+}
Quoted 6 lines
Quoted 2 lines
Quoted 1 line

The refcount is wrapped in bond.

I'm still not sure that bond is necessary

"bond" is the sva handle that the device drivers get through calling iommu_sva_bind().

Quoted 6 lines

'bond' was required before because we didn't have a domain to wrap the page table at that time.

Now we have a domain and it is 1:1 associated to bond. Probably make sense now by just returning the domain as the sva handle instead?

It also includes the device information that the domain has been attached. So the sva_unbind() looks like this:

/**
* 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)

Quoted 4 lines
Quoted 2 lines

It's fine to replace the iommu_sva with iommu_sva_domain for sva handle, if we can include the device in the unbind() interface.

Why would we have a special unbind for SVA?

Quoted 7 lines

It's about SVA kAPI for device drivers. The existing kAPIs include:

struct iommu_sva *iommu_sva_bind_device(struct device *dev,
struct mm_struct *mm, void *drvdata);
void iommu_sva_unbind_device(struct iommu_sva *handle); u32 iommu_sva_get_pasid(struct iommu_sva *handle);

This is not what we agreed the API should be. We agreed:

iommu_sva_domain_alloc()
iommu_attach_device_pasid()
iommu_detach_device_pasid()

Again, SVA should not be different from normal domain stuff.

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

author maintainer
#33 lore raw

Re: [PATCH v6 08/12] iommu/sva: Use attach/detach_pasid_dev in SVA interfaces

Quoted 65 lines · Jason Gunthorpe
Quoted 56 lines · Baolu Lu
Quoted 50 lines · Jason Gunthorpe
Quoted 49 lines · Baolu Lu
Quoted 26 lines · Kevin
Quoted 19 lines

From: Baolu Lu <[email protected]>
Sent: Thursday, May 12, 2022 11:03 AM

Quoted 15 lines · Jason Gunthorpe
Quoted 15 lines
Quoted 3 lines

Also, given the current arrangement it might make sense to have a struct iommu_domain_sva given that no driver is wrappering this in something else.

Fair enough. How about below wrapper?

(quoted diff) +9 -0
+struct iommu_sva_domain {
+       /*
+        * Common iommu domain header,*must*  be put at the top
+        * of the structure.
+        */
+       struct iommu_domain domain;
+       struct mm_struct *mm;
+       struct iommu_sva bond;
+}
Quoted 6 lines
Quoted 2 lines
Quoted 1 line

The refcount is wrapped in bond.

I'm still not sure that bond is necessary

"bond" is the sva handle that the device drivers get through calling iommu_sva_bind().

Quoted 6 lines

'bond' was required before because we didn't have a domain to wrap the page table at that time.

Now we have a domain and it is 1:1 associated to bond. Probably make sense now by just returning the domain as the sva handle instead?

It also includes the device information that the domain has been attached. So the sva_unbind() looks like this:

/**
* 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)

Quoted 4 lines
Quoted 2 lines

It's fine to replace the iommu_sva with iommu_sva_domain for sva handle, if we can include the device in the unbind() interface.

Why would we have a special unbind for SVA?

Quoted 7 lines

It's about SVA kAPI for device drivers. The existing kAPIs include:

struct iommu_sva *iommu_sva_bind_device(struct device *dev,
struct mm_struct *mm, void *drvdata);
void iommu_sva_unbind_device(struct iommu_sva *handle); u32 iommu_sva_get_pasid(struct iommu_sva *handle);

Quoted 7 lines

This is not what we agreed the API should be. We agreed:

iommu_sva_domain_alloc()
iommu_attach_device_pasid()
iommu_detach_device_pasid()

Again, SVA should not be different from normal domain stuff.

Yes, agreed.

I am trying to achieve this in two steps. This first step focuses on internal iommu implementation and keep the driver kAPI untouched. Then, the second step focus on the driver APIs.

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

#34 lore raw

Re: [PATCH v6 08/12] iommu/sva: Use attach/detach_pasid_dev in SVA interfaces

In reply to Baolu Lu
Quoted 8 lines · Baolu Lu
Quoted 8 lines
Quoted 8 lines
(quoted diff) +8 -0
+       mutex_lock(&group->mutex);
+       domain = xa_load(&group->pasid_array, pasid);
+       if (domain && domain->type != type)
+               domain = NULL;
+       mutex_unlock(&group->mutex);
+       iommu_group_put(group);
+
+       return domain;
Quoted 8 lines
Quoted 2 lines

This is bad locking, group->pasid_array values cannot be taken outside the lock.

It's not iommu core, but SVA (or other feature components) that manage the life cycle of a domain. The iommu core only provides a place to store the domain pointer. The feature components are free to fetch their domain pointers from iommu core as long as they are sure that the domain is alive during use.

I'm not convinced.

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

author maintainer
#35 lore raw

Re: [PATCH v6 08/12] iommu/sva: Use attach/detach_pasid_dev in SVA interfaces

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

Review

Quoted 8 lines
(quoted diff) +8 -0
+       mutex_lock(&group->mutex);
+       domain = xa_load(&group->pasid_array, pasid);
+       if (domain && domain->type != type)
+               domain = NULL;
+       mutex_unlock(&group->mutex);
+       iommu_group_put(group);
+
+       return domain;

This is bad locking, group->pasid_array values cannot be taken outside the lock.

Quoted 7 lines
Quoted 5 lines

It's not iommu core, but SVA (or other feature components) that manage the life cycle of a domain. The iommu core only provides a place to store the domain pointer. The feature components are free to fetch their domain pointers from iommu core as long as they are sure that the domain is alive during use.

I'm not convinced.

I'm sorry, I may not have explained it clearly. :-)

This helper is safe for uses inside the IOMMU subsystem. We could trust ourselves that nobody will abuse this helper to obtain domains belonging to others as the pasid has been allocated for SVA code. No other code should be able to setup another type of domain for this pasid. The SVA code has its own lock mechanism to avoid using after free.

Please correct me if I missed anything. :-) By the way, I can see some similar helpers in current IOMMU core. For example,

struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)

{
        struct iommu_domain *domain;
        struct iommu_group *group;

        group = iommu_group_get(dev);
        if (!group)
                return NULL;

        domain = group->domain;

        iommu_group_put(group);

        return domain;
}

EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);

Best regards,
baolu

author maintainer
#36 lore raw

PATCH v6 9/12 iommu: Remove SVA related callbacks from iommu ops

In reply to Lu Baolu

These ops'es have been replaced with the dev_attach/detach_pasid domain ops'es. There's no need for them anymore. Remove them to avoid dead code.

---
 include/linux/intel-iommu.h                   |  4 --
 include/linux/iommu.h                         |  8 ---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h   | 17 -------
 drivers/iommu/iommu-sva-lib.h                 |  1 -
 .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c   | 41 ----------------
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c   |  3 --
 drivers/iommu/intel/iommu.c                   |  3 --
 drivers/iommu/intel/svm.c                     | 49 -------------------
 drivers/iommu/iommu-sva-lib.c                 |  4 +-
 9 files changed, 2 insertions(+), 128 deletions(-)
include/linux/intel-iommu.h +0 -4
index 2397c2007cda..4a5cc796f917 100644
@@ -738,10 +738,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 *drvdata);
-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);
include/linux/iommu.h +0 -8
index 5a3ef4d58b1f..392b8adc3495 100644
@@ -215,9 +215,6 @@ struct iommu_iotlb_gather {
  * @dev_has/enable/disable_feat: per device entries to check/enable/disable
  *                               iommu specific features.
  * @dev_feat_enabled: check enabled feature
- * @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
@@ -251,11 +248,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 *drvdata);
-	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 -17
index e077f21e2528..15dd4c7e6d3a 100644
@@ -754,10 +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 *drvdata);
-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);
 #else /* CONFIG_ARM_SMMU_V3_SVA */
@@ -791,19 +787,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, void *drvdata)
-{
-	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/iommu-sva-lib.h +0 -1
index 8909ea1094e3..3420654c6e2f 100644
@@ -8,7 +8,6 @@
 #include <linux/ioasid.h>
 #include <linux/mm_types.h>
 
-int iommu_sva_alloc_pasid(struct mm_struct *mm, ioasid_t min, ioasid_t max);
 struct mm_struct *iommu_sva_find(ioasid_t pasid);
 
 /* I/O Page fault */
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +0 -41

53 lines not on this page · raw message

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -3
index 9daf3de7e539..6a10fa181827 100644
@@ -2857,9 +2857,6 @@ static struct iommu_ops arm_smmu_ops = {
 	.dev_feat_enabled	= arm_smmu_dev_feature_enabled,
 	.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 10b1e9dcbd98..a5728f743c6d 100644
@@ -4919,9 +4919,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

drivers/iommu/iommu-sva-lib.c +2 -2
index e7301514f286..ef6ed87d04ba 100644
@@ -24,7 +24,8 @@ static DECLARE_IOASID_SET(iommu_sva_pasid);
  *
  * Returns 0 on success and < 0 on error.
  */
-int iommu_sva_alloc_pasid(struct mm_struct *mm, ioasid_t min, ioasid_t max)
+static int iommu_sva_alloc_pasid(struct mm_struct *mm,
+				 ioasid_t min, ioasid_t max)
 {
 	int ret = 0;
 	ioasid_t pasid;
@@ -50,7 +51,6 @@ int iommu_sva_alloc_pasid(struct mm_struct *mm, ioasid_t min, ioasid_t max)
 	mutex_unlock(&iommu_sva_lock);
 	return ret;
 }
-EXPORT_SYMBOL_GPL(iommu_sva_alloc_pasid);
 
 /* ioasid_find getter() requires a void * argument */
 static bool __mmget_not_zero(void *mm)
Signature · 6 lines
2.25.1

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

PATCH v6 10/12 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.

A new helper iommu_get_domain_for_iopf() which retrieves attached domain for a {device, PASID} pair is added. 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() waits for pending faults with iopf_queue_flush_dev() before freeing the domain. Hence, there's no need to synchronize life cycle of the iommu domains between the unbind() and the interrupt threads.

This also prepares the SVA implementation to be the first consumer of the per-domain page fault handling model.

---
 include/linux/iommu.h         | 12 +++++++
 drivers/iommu/io-pgfault.c    |  7 ++++
 drivers/iommu/iommu-sva-lib.c | 65 +++++++++++++++++++++++++++++++++++
 drivers/iommu/iommu.c         | 27 +++++++++++++++
 4 files changed, 111 insertions(+)
include/linux/iommu.h +12 -0
index 392b8adc3495..9405034e3013 100644
@@ -103,6 +103,9 @@ struct iommu_domain {
 #ifdef CONFIG_IOMMU_SVA
 	struct mm_struct *mm;
 #endif
+	enum iommu_page_response_code (*iopf_handler)(struct iommu_fault *fault,
+						      void *data);
+	void *fault_data;
 };
 
 static inline bool iommu_is_dma_domain(struct iommu_domain *domain)
@@ -687,6 +690,9 @@ 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_iopf(struct device *dev, ioasid_t pasid);
+
 #else /* CONFIG_IOMMU_API */
 
 struct iommu_ops {};
@@ -1056,6 +1062,12 @@ 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_iopf(struct device *dev, ioasid_t pasid)
+{
+	return NULL;
+}
 #endif /* CONFIG_IOMMU_API */
 
 #ifdef CONFIG_IOMMU_SVA
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 +65 -0

77 lines not on this page · raw message

drivers/iommu/iommu.c +27 -0
index 367d0ecf6e12..00d0262a1fe9 100644
@@ -3172,3 +3172,30 @@ void iommu_detach_device_pasid(struct iommu_domain *domain,
 
 	iommu_group_put(group);
 }
+
+struct iommu_domain *iommu_get_domain_for_iopf(struct device *dev,
+					       ioasid_t pasid)
+{
+	struct iommu_domain *domain;
+	struct iommu_group *group;
+
+	if (!pasid_valid(pasid))
+		return NULL;
+
+	group = iommu_group_get(dev);
+	if (!group)
+		return NULL;
+
+	/*
+	 * Safe to fetch outside the group mutex because:
+	 *
+	 * - The xarray protects its internal state with RCU;
+	 * - The domain obtained is either NULL or fully formed;
+	 * - Current IOPF framework requires that all IOPFs should be quieted
+	 *   and flushed from the pending list before the domain is freed.
+	 */
+	domain = xa_load(&group->pasid_array, pasid);
+	iommu_group_put(group);
+
+	return domain;
+}
Signature · 6 lines
2.25.1

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

PATCH v6 11/12 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.

---
 drivers/iommu/iommu-sva-lib.h |  2 --
 drivers/iommu/io-pgfault.c    | 64 ++++-------------------------------
 drivers/iommu/iommu-sva-lib.c | 20 -----------
 3 files changed, 7 insertions(+), 79 deletions(-)
drivers/iommu/iommu-sva-lib.h +0 -2
index 3420654c6e2f..74ce2e76321b 100644
@@ -8,8 +8,6 @@
 #include <linux/ioasid.h>
 #include <linux/mm_types.h>
 
-struct mm_struct *iommu_sva_find(ioasid_t pasid);
-
 /* I/O Page fault */
 struct device;
 struct iommu_fault;
drivers/iommu/io-pgfault.c +7 -57

83 lines not on this page · raw message

drivers/iommu/iommu-sva-lib.c +0 -20
index 32c836e4a60e..ea12504a9e12 100644
@@ -52,26 +52,6 @@ static int iommu_sva_alloc_pasid(struct mm_struct *mm,
 	return ret;
 }
 
-/* ioasid_find getter() requires a void * argument */
-static bool __mmget_not_zero(void *mm)
-{
-	return mmget_not_zero(mm);
-}
-
-/**
- * iommu_sva_find() - Find mm associated to the given PASID
- * @pasid: Process Address Space ID assigned to the mm
- *
- * On success a reference to the mm is taken, and must be released with mmput().
- *
- * Returns the mm corresponding to this PASID, or an error if not found.
- */
-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);
-
 /*
  * I/O page fault handler for SVA
  *
Signature · 6 lines
2.25.1

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

PATCH v6 12/12 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}  | 0
 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/Makefile                          | 2 +-
 8 files changed, 7 insertions(+), 7 deletions(-)
 rename drivers/iommu/{iommu-sva-lib.h => iommu-sva.h} (100%)
 rename drivers/iommu/{iommu-sva-lib.c => iommu-sva.c} (99%)
drivers/iommu/iommu-sva.h renamed from drivers/iommu/iommu-sva-lib.h +0 -0
similarity index 100%
rename from drivers/iommu/iommu-sva-lib.h
rename to drivers/iommu/iommu-sva.h
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +1 -1
index 56644e553c42..265b125d7dc4 100644
@@ -9,7 +9,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 6a10fa181827..de3b6fbf8766 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 a5728f743c6d..1c2c92b657c7 100644
@@ -27,7 +27,7 @@
 #include <linux/tboot.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 ca83ebc708a8..44331db060e4 100644
@@ -25,7 +25,7 @@
 
 #include "pasid.h"
 #include "perf.h"
-#include "../iommu-sva-lib.h"
+#include "../iommu-sva.h"
 
 static irqreturn_t prq_event_thread(int irq, void *d);
 static void intel_svm_drain_prq(struct device *dev, u32 pasid);
drivers/iommu/io-pgfault.c +1 -1
index 9efe5259402b..2a8604013b7e 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 ea12504a9e12..1791ac1e3d34 100644
@@ -7,7 +7,7 @@
 #include <linux/slab.h>
 #include <linux/sched/mm.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/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 · 6 lines
2.25.1

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