Skip to contentSkip to conversation

Make set_dev_pasid op supportting domain replacement

PATCH Superseded Yi Liu 5 participants 34 messages Jun 28, 2024 – Aug 16, 2024

Subsystems (6)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Yi Liu

author
#0 lore raw

cover Make set_dev_pasid op supportting domain replacement

This splits the preparation works of the iommu and the Intel iommu driver out from the iommufd pasid attach/replace series. [1]

To support domain replacement, the definition of the set_dev_pasid op needs to be enhanced. Meanwhile, the existing set_dev_pasid callbacks should be extended as well to suit the new definition.

pasid attach/replace is mandatory on Intel VT-d given the PASID table locates in the physical address space hence must be managed by the kernel, both for supporting vSVA and coming SIOV. But it's optional on ARM/AMD which allow configuring the PASID/CD table either in host physical address space or nested on top of an GPA address space. This series only extends the Intel iommu driver as the minimal requirement.

This series first prepares the Intel iommu set_dev_pasid op for the new definition, adds the missing set_dev_pasid support for nested domain, and in the end enhances the definition of set_dev_pasid op. The ARM and AMD set_dev_pasid callbacks is extended to fail if the caller tries to do domain replacement to meet the new definition of set_dev_pasid op.

This series is on top of Baolu's paging domain alloc refactor, where his code can be found at [2].

[1] https://lore.kernel.org/linux-iommu/[email protected]/
[2] https://github.com/LuBaolu/intel-iommu/commits/vtd-paging-domain-refactor-v1

Regards,
Yi Liu

Lu Baolu (1):
iommu/vt-d: Add set_dev_pasid callback for nested domain

Yi Liu (5):
iommu: Pass old domain to set_dev_pasid op iommu/vt-d: Move intel_drain_pasid_prq() into
intel_pasid_tear_down_entry()
iommu/vt-d: Make helpers support modifying present pasid entry iommu/vt-d: Make intel_iommu_set_dev_pasid() to handle domain
replacement
iommu: Make set_dev_pasid op support domain replacement

 drivers/iommu/amd/amd_iommu.h                 |   3 +-
 drivers/iommu/amd/pasid.c                     |   6 +-
 .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c   |   6 +-
 drivers/iommu/intel/debugfs.c                 |   2 +
 drivers/iommu/intel/iommu.c                   | 117 ++++++++++++------
 drivers/iommu/intel/iommu.h                   |   3 +
 drivers/iommu/intel/nested.c                  |   1 +
 drivers/iommu/intel/pasid.c                   |  46 +++----
 drivers/iommu/intel/pasid.h                   |   5 +-
 drivers/iommu/intel/svm.c                     |   9 +-
 drivers/iommu/iommu.c                         |   3 +-
 include/linux/iommu.h                         |   5 +-
 12 files changed, 132 insertions(+), 74 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/6 iommu: Pass old domain to set_dev_pasid op

To support domain replacement for pasid, the underlying iommu driver needs to know the old domain hence be able to clean up the existing attachment. It would be much convenient for iommu layer to pass down the old domain. Otherwise, iommu drivers would need to track domain for pasids by themselves, this would duplicate code among the iommu drivers. Or iommu drivers would rely group->pasid_array to get domain, which may not always the correct one.

---
 drivers/iommu/amd/amd_iommu.h                   | 3 ++-
 drivers/iommu/amd/pasid.c                       | 3 ++-
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 3 ++-
 drivers/iommu/intel/iommu.c                     | 6 ++++--
 drivers/iommu/intel/svm.c                       | 3 ++-
 drivers/iommu/iommu.c                           | 3 ++-
 include/linux/iommu.h                           | 2 +-
 7 files changed, 15 insertions(+), 8 deletions(-)
drivers/iommu/amd/amd_iommu.h +2 -1
index 2fde1302a584..31141c6bdf54 100644
@@ -51,7 +51,8 @@ struct iommu_domain *amd_iommu_domain_alloc_sva(struct device *dev,
 						struct mm_struct *mm);
 void amd_iommu_domain_free(struct iommu_domain *dom);
 int iommu_sva_set_dev_pasid(struct iommu_domain *domain,
-			    struct device *dev, ioasid_t pasid);
+			    struct device *dev, ioasid_t pasid,
+			    struct iommu_domain *old);
 void amd_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid,
 				struct iommu_domain *domain);
 
drivers/iommu/amd/pasid.c +2 -1
index a68215f2b3e1..77bf5f5f947a 100644
@@ -100,7 +100,8 @@ static const struct mmu_notifier_ops sva_mn = {
 };
 
 int iommu_sva_set_dev_pasid(struct iommu_domain *domain,
-			    struct device *dev, ioasid_t pasid)
+			    struct device *dev, ioasid_t pasid,
+			    struct iommu_domain *old)
 {
 	struct pdom_dev_data *pdom_dev_data;
 	struct protection_domain *sva_pdom = to_pdomain(domain);
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +2 -1
index e490ffb38015..c058949749cb 100644
@@ -631,7 +631,8 @@ void arm_smmu_sva_remove_dev_pasid(struct iommu_domain *domain,
 }
 
 static int arm_smmu_sva_set_dev_pasid(struct iommu_domain *domain,
-				      struct device *dev, ioasid_t id)
+				      struct device *dev, ioasid_t id,
+				      struct iommu_domain *old)
 {
 	int ret = 0;
 	struct mm_struct *mm = domain->mm;
drivers/iommu/intel/iommu.c +4 -2
index 129e5b685762..288c929b3d15 100644
@@ -4065,7 +4065,8 @@ static void intel_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid,
 }
 
 static int intel_iommu_set_dev_pasid(struct iommu_domain *domain,
-				     struct device *dev, ioasid_t pasid)
+				     struct device *dev, ioasid_t pasid,
+				     struct iommu_domain *old)
 {
 	struct device_domain_info *info = dev_iommu_priv_get(dev);
 	struct dmar_domain *dmar_domain = to_dmar_domain(domain);
@@ -4352,7 +4353,8 @@ static int identity_domain_attach_dev(struct iommu_domain *domain,
 }
 
 static int identity_domain_set_dev_pasid(struct iommu_domain *domain,
-					 struct device *dev, ioasid_t pasid)
+					 struct device *dev, ioasid_t pasid,
+					 struct iommu_domain *old)
 {
 	struct device_domain_info *info = dev_iommu_priv_get(dev);
 	struct intel_iommu *iommu = info->iommu;
drivers/iommu/intel/svm.c +2 -1
index ef12e95e400a..a5845ca94867 100644
@@ -194,7 +194,8 @@ static const struct mmu_notifier_ops intel_mmuops = {
 };
 
 static int intel_svm_set_dev_pasid(struct iommu_domain *domain,
-				   struct device *dev, ioasid_t pasid)
+				   struct device *dev, ioasid_t pasid,
+				   struct iommu_domain *old)
 {
 	struct device_domain_info *info = dev_iommu_priv_get(dev);
 	struct dmar_domain *dmar_domain = to_dmar_domain(domain);
drivers/iommu/iommu.c +2 -1
index 8212fed27e18..b3a1dabed2dd 100644
@@ -3274,7 +3274,8 @@ static int __iommu_set_group_pasid(struct iommu_domain *domain,
 	int ret;
 
 	for_each_group_device(group, device) {
-		ret = domain->ops->set_dev_pasid(domain, device->dev, pasid);
+		ret = domain->ops->set_dev_pasid(domain, device->dev,
+						 pasid, NULL);
 		if (ret)
 			goto err_revert;
 	}
include/linux/iommu.h +1 -1
index bff6e7e81fa3..a33f53aab61b 100644
@@ -633,7 +633,7 @@ struct iommu_ops {
 struct iommu_domain_ops {
 	int (*attach_dev)(struct iommu_domain *domain, struct device *dev);
 	int (*set_dev_pasid)(struct iommu_domain *domain, struct device *dev,
-			     ioasid_t pasid);
+			     ioasid_t pasid, struct iommu_domain *old);
 
 	int (*map_pages)(struct iommu_domain *domain, unsigned long iova,
 			 phys_addr_t paddr, size_t pgsize, size_t pgcount,
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH 2/6 iommu/vt-d: Move intel_drain_pasid_prq() into intel_pasid_tear_down_entry()

In reply to Yi Liu

Draining PRQ is needed before repurposing a PASID. It makes sense to invoke it in the intel_pasid_tear_down_entry().

---
 drivers/iommu/intel/iommu.c | 5 ++---
 drivers/iommu/intel/pasid.c | 9 ++++++++-
 drivers/iommu/intel/pasid.h | 5 ++---
 drivers/iommu/intel/svm.c   | 6 +++++-
 4 files changed, 17 insertions(+), 8 deletions(-)
drivers/iommu/intel/iommu.c +2 -3
index 288c929b3d15..dd3de95c7122 100644
@@ -3241,7 +3241,7 @@ void device_block_translation(struct device *dev)
 	if (!dev_is_real_dma_subdevice(dev)) {
 		if (sm_supported(iommu))
 			intel_pasid_tear_down_entry(iommu, dev,
-						    IOMMU_NO_PASID, false);
+						    IOMMU_NO_PASID, false, false);
 		else
 			domain_context_clear(info);
 	}
@@ -4060,8 +4060,7 @@ static void intel_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid,
 	intel_iommu_debugfs_remove_dev_pasid(dev_pasid);
 	kfree(dev_pasid);
 out_tear_down:
-	intel_pasid_tear_down_entry(iommu, dev, pasid, false);
-	intel_drain_pasid_prq(dev, pasid);
+	intel_pasid_tear_down_entry(iommu, dev, pasid, false, true);
 }
 
 static int intel_iommu_set_dev_pasid(struct iommu_domain *domain,
drivers/iommu/intel/pasid.c +8 -1
index 573e1b8e3cfb..b18eebb479de 100644
@@ -233,8 +233,12 @@ devtlb_invalidation_with_pasid(struct intel_iommu *iommu,
 		qi_flush_dev_iotlb_pasid(iommu, sid, pfsid, pasid, qdep, 0, 64 - VTD_PAGE_SHIFT);
 }
 
+/*
+ * Not all PASID entry destroy requires PRQ drain as it can be handled in
+ * the remove_dev_pasid path. Caller should be clear on it.
+ */
 void intel_pasid_tear_down_entry(struct intel_iommu *iommu, struct device *dev,
-				 u32 pasid, bool fault_ignore)
+				 u32 pasid, bool fault_ignore, bool drain_prq)
 {
 	struct pasid_entry *pte;
 	u16 did, pgtt;
@@ -264,6 +268,9 @@ void intel_pasid_tear_down_entry(struct intel_iommu *iommu, struct device *dev,
 	/* Device IOTLB doesn't need to be flushed in caching mode. */
 	if (!cap_caching_mode(iommu->cap))
 		devtlb_invalidation_with_pasid(iommu, dev, pasid);
+
+	if (drain_prq)
+		intel_drain_pasid_prq(dev, pasid);
 }
 
 /*
drivers/iommu/intel/pasid.h +2 -3
index da9978fef7ac..8b77b0d21c6e 100644
@@ -313,9 +313,8 @@ int intel_pasid_setup_pass_through(struct intel_iommu *iommu,
 				   struct device *dev, u32 pasid);
 int intel_pasid_setup_nested(struct intel_iommu *iommu, struct device *dev,
 			     u32 pasid, struct dmar_domain *domain);
-void intel_pasid_tear_down_entry(struct intel_iommu *iommu,
-				 struct device *dev, u32 pasid,
-				 bool fault_ignore);
+void intel_pasid_tear_down_entry(struct intel_iommu *iommu, struct device *dev,
+				 u32 pasid, bool fault_ignore, bool drain_prq);
 void intel_pasid_setup_page_snoop_control(struct intel_iommu *iommu,
 					  struct device *dev, u32 pasid);
 int intel_pasid_setup_sm_context(struct device *dev);
drivers/iommu/intel/svm.c +5 -1
index a5845ca94867..679e094d9f52 100644
@@ -175,8 +175,12 @@ static void intel_mm_release(struct mmu_notifier *mn, struct mm_struct *mm)
 	spin_lock_irqsave(&domain->lock, flags);
 	list_for_each_entry(dev_pasid, &domain->dev_pasids, link_domain) {
 		info = dev_iommu_priv_get(dev_pasid->dev);
+		/*
+		 * PRQ drain would happen in the remove_dev_pasid() path,
+		 * no need to do it here.
+		 */
 		intel_pasid_tear_down_entry(info->iommu, dev_pasid->dev,
-					    dev_pasid->pasid, true);
+					    dev_pasid->pasid, true, false);
 	}
 	spin_unlock_irqrestore(&domain->lock, flags);
 
Signature · 1 line
2.34.1
maintainer
#3 lore raw

Re: [PATCH 2/6] iommu/vt-d: Move intel_drain_pasid_prq() into intel_pasid_tear_down_entry()

Quoted 2 lines · Yi Liu

Draining PRQ is needed before repurposing a PASID. It makes sense to invoke it in the intel_pasid_tear_down_entry().

Can you please elaborate on the value of this merge?

Draining the PRQ is necessary when PRI is enabled on the device, and a page table is about to be removed from the PASID. This might occur in conjunction with tearing down a PASID entry, but it seems they are two distinct actions.

Best regards,
baolu

author
#4 lore raw

Re: [PATCH 2/6] iommu/vt-d: Move intel_drain_pasid_prq() into intel_pasid_tear_down_entry()

Quoted 6 lines · Baolu Lu
Quoted 2 lines · Yi Liu

Draining PRQ is needed before repurposing a PASID. It makes sense to invoke it in the intel_pasid_tear_down_entry().

Can you please elaborate on the value of this merge?

The major reason is that the next patch would have multiple places that need to destroy pasid entry and do prq drain. Wrap them would make life easier I suppose.

Quoted 4 lines

Draining the PRQ is necessary when PRI is enabled on the device, and a page table is about to be removed from the PASID. This might occur in conjunction with tearing down a PASID entry, but it seems they are two distinct actions.

Seems like mostly they have conjunction, while there is indeed one exception in the intel_mm_release(). Given the above reason, do you have any suggestion for it?

Signature · 2 lines
Regards,
Yi Liu
#5 lore raw

RE: [PATCH 2/6] iommu/vt-d: Move intel_drain_pasid_prq() into intel_pasid_tear_down_entry()

Quoted 16 lines

From: Liu, Yi L <[email protected]>
Sent: Friday, June 28, 2024 6:52 PM

Quoted 2 lines · Baolu Lu
Quoted 1 line · Yi Liu

Draining PRQ is needed before repurposing a PASID. It makes sense to

invoke

Quoted 4 lines
Quoted 1 line

it in the intel_pasid_tear_down_entry().

Can you please elaborate on the value of this merge?

The major reason is that the next patch would have multiple places that need to destroy pasid entry and do prq drain. Wrap them would make life easier I suppose.

Quoted 9 lines
Quoted 4 lines

Draining the PRQ is necessary when PRI is enabled on the device, and a page table is about to be removed from the PASID. This might occur in conjunction with tearing down a PASID entry, but it seems they are two distinct actions.

Seems like mostly they have conjunction, while there is indeed one exception in the intel_mm_release(). Given the above reason, do you have any suggestion for it?

If we really have such need please don't add more boolean fields.

Let's extend the existing one into a flag instead, for better readability.

author
#6 lore raw

PATCH 3/6 iommu/vt-d: Make helpers support modifying present pasid entry

In reply to Yi Liu

To handle domain replacement, set_dev_pasid op needs to modify a present pasid entry. One way is sharing the most logics of remove_dev_pasid() in the beginning of set_dev_pasid() to remove the old config. But this means the set_dev_pasid path needs to rollback to the old config if it fails to set up the new pasid entry. This needs to invoke the set_dev_pasid op of the old domain. It breaks the iommu layering a bit. Another way is implementing the set_dev_pasid() without rollback to old hardware config. This can be achieved by implementing it in the order of preparing the dev_pasid info for the new domain, modify the pasid entry, then undo the dev_pasid info of the old domain, and if failed, undo the dev_pasid info of the new domain. This would keep the old domain unchanged.

Following the second way, needs to make the pasid entry set up helpers support modifying present pasid entry.

---
 drivers/iommu/intel/pasid.c | 37 ++++++++++++-------------------------
 1 file changed, 12 insertions(+), 25 deletions(-)
drivers/iommu/intel/pasid.c +12 -25
index b18eebb479de..5d3a12b081a2 100644
@@ -314,6 +314,9 @@ int intel_pasid_setup_first_level(struct intel_iommu *iommu,
 		return -EINVAL;
 	}
 
+	/* Clear the old configuration if it already exists */
+	intel_pasid_tear_down_entry(iommu, dev, pasid, false, true);
+
 	spin_lock(&iommu->lock);
 	pte = intel_pasid_get_entry(dev, pasid);
 	if (!pte) {
@@ -321,13 +324,6 @@ int intel_pasid_setup_first_level(struct intel_iommu *iommu,
 		return -ENODEV;
 	}
 
-	if (pasid_pte_is_present(pte)) {
-		spin_unlock(&iommu->lock);
-		return -EBUSY;
-	}
-
-	pasid_clear_entry(pte);
-
 	/* Setup the first level page table pointer: */
 	pasid_set_flptr(pte, (u64)__pa(pgd));
 
@@ -378,6 +374,9 @@ int intel_pasid_setup_second_level(struct intel_iommu *iommu,
 	pgd_val = virt_to_phys(pgd);
 	did = domain_id_iommu(domain, iommu);
 
+	/* Clear the old configuration if it already exists */
+	intel_pasid_tear_down_entry(iommu, dev, pasid, false, true);
+
 	spin_lock(&iommu->lock);
 	pte = intel_pasid_get_entry(dev, pasid);
 	if (!pte) {
@@ -385,12 +384,6 @@ int intel_pasid_setup_second_level(struct intel_iommu *iommu,
 		return -ENODEV;
 	}
 
-	if (pasid_pte_is_present(pte)) {
-		spin_unlock(&iommu->lock);
-		return -EBUSY;
-	}
-
-	pasid_clear_entry(pte);
 	pasid_set_domain_id(pte, did);
 	pasid_set_slptr(pte, pgd_val);
 	pasid_set_address_width(pte, domain->agaw);
@@ -488,6 +481,9 @@ int intel_pasid_setup_pass_through(struct intel_iommu *iommu,
 	u16 did = FLPT_DEFAULT_DID;
 	struct pasid_entry *pte;
 
+	/* Clear the old configuration if it already exists */
+	intel_pasid_tear_down_entry(iommu, dev, pasid, false, true);
+
 	spin_lock(&iommu->lock);
 	pte = intel_pasid_get_entry(dev, pasid);
 	if (!pte) {
@@ -495,12 +491,6 @@ int intel_pasid_setup_pass_through(struct intel_iommu *iommu,
 		return -ENODEV;
 	}
 
-	if (pasid_pte_is_present(pte)) {
-		spin_unlock(&iommu->lock);
-		return -EBUSY;
-	}
-
-	pasid_clear_entry(pte);
 	pasid_set_domain_id(pte, did);
 	pasid_set_address_width(pte, iommu->agaw);
 	pasid_set_translation_type(pte, PASID_ENTRY_PGTT_PT);
@@ -606,18 +596,15 @@ int intel_pasid_setup_nested(struct intel_iommu *iommu, struct device *dev,
 		return -EINVAL;
 	}
 
+	/* Clear the old configuration if it already exists */
+	intel_pasid_tear_down_entry(iommu, dev, pasid, false, true);
+
 	spin_lock(&iommu->lock);
 	pte = intel_pasid_get_entry(dev, pasid);
 	if (!pte) {
 		spin_unlock(&iommu->lock);
 		return -ENODEV;
 	}
-	if (pasid_pte_is_present(pte)) {
-		spin_unlock(&iommu->lock);
-		return -EBUSY;
-	}
-
-	pasid_clear_entry(pte);
 
 	if (s1_cfg->addr_width == ADDR_WIDTH_5LEVEL)
 		pasid_set_flpm(pte, 1);
Signature · 1 line
2.34.1
maintainer
#7 lore raw

Re: [PATCH 3/6] iommu/vt-d: Make helpers support modifying present pasid entry

Quoted 37 lines · Yi Liu

To handle domain replacement, set_dev_pasid op needs to modify a present pasid entry. One way is sharing the most logics of remove_dev_pasid() in the beginning of set_dev_pasid() to remove the old config. But this means the set_dev_pasid path needs to rollback to the old config if it fails to set up the new pasid entry. This needs to invoke the set_dev_pasid op of the old domain. It breaks the iommu layering a bit. Another way is implementing the set_dev_pasid() without rollback to old hardware config. This can be achieved by implementing it in the order of preparing the dev_pasid info for the new domain, modify the pasid entry, then undo the dev_pasid info of the old domain, and if failed, undo the dev_pasid info of the new domain. This would keep the old domain unchanged.

Following the second way, needs to make the pasid entry set up helpers support modifying present pasid entry.

---
  drivers/iommu/intel/pasid.c | 37 ++++++++++++-------------------------
  1 file changed, 12 insertions(+), 25 deletions(-)
drivers/iommu/intel/pasid.c +3 -0
index b18eebb479de..5d3a12b081a2 100644
@@ -314,6 +314,9 @@ int intel_pasid_setup_first_level(struct intel_iommu *iommu,
  		return -EINVAL;
  	}
 
+	/* Clear the old configuration if it already exists */
+	intel_pasid_tear_down_entry(iommu, dev, pasid, false, true);
+
  	spin_lock(&iommu->lock);
  	pte = intel_pasid_get_entry(dev, pasid);
  	if (!pte) {
@@ -321,13 +324,6 @@ int intel_pasid_setup_first_level(struct intel_iommu *iommu,
  		return -ENODEV;
  	}

drivers/iommu/intel/pasid.c int intel_pasid_setup_first_level(struct intel_iommu *iommu, · line 328

Quoted 10 lines
drivers/iommu/intel/pasid.c +0 -7
 
-	if (pasid_pte_is_present(pte)) {
-		spin_unlock(&iommu->lock);
-		return -EBUSY;
-	}
-
-	pasid_clear_entry(pte);
-
  	/* Setup the first level page table pointer: */
  	pasid_set_flptr(pte, (u64)__pa(pgd));

The above changes the previous assumption that when a new page table is about to be set up on a PASID, there should be no existing one still in place.

Is this a requirement for the replace functionality?

Best regards,
baolu

author
#8 lore raw

Re: [PATCH 3/6] iommu/vt-d: Make helpers support modifying present pasid entry

Quoted 21 lines · Baolu Lu
Quoted 20 lines · Yi Liu

To handle domain replacement, set_dev_pasid op needs to modify a present pasid entry. One way is sharing the most logics of remove_dev_pasid() in the beginning of set_dev_pasid() to remove the old config. But this means the set_dev_pasid path needs to rollback to the old config if it fails to set up the new pasid entry. This needs to invoke the set_dev_pasid op of the old domain. It breaks the iommu layering a bit. Another way is implementing the set_dev_pasid() without rollback to old hardware config. This can be achieved by implementing it in the order of preparing the dev_pasid info for the new domain, modify the pasid entry, then undo the dev_pasid info of the old domain, and if failed, undo the dev_pasid info of the new domain. This would keep the old domain unchanged.

Following the second way, needs to make the pasid entry set up helpers support modifying present pasid entry.

---
  drivers/iommu/intel/pasid.c | 37 ++++++++++++-------------------------

  1 file changed, 12 insertions(+), 25 deletions(-)
Quoted 11 lines

drivers/iommu/intel/pasid.c int intel_pasid_setup_first_level(struct intel_iommu · line 319

Quoted 11 lines
drivers/iommu/intel/pasid.c +3 -0
index b18eebb479de..5d3a12b081a2 100644
@@ -314,6 +314,9 @@ int intel_pasid_setup_first_level(struct intel_iommu 
 *iommu,
           return -EINVAL;
       }
+    /* Clear the old configuration if it already exists */
+    intel_pasid_tear_down_entry(iommu, dev, pasid, false, true);
+

[1]

Quoted 12 lines
Quoted 12 lines
drivers/iommu/intel/pasid.c +0 -5
       spin_lock(&iommu->lock);
       pte = intel_pasid_get_entry(dev, pasid);
       if (!pte) {
@@ -321,13 +324,6 @@ int intel_pasid_setup_first_level(struct intel_iommu 
 *iommu,
           return -ENODEV;
       }
-    if (pasid_pte_is_present(pte)) {
-        spin_unlock(&iommu->lock);
-        return -EBUSY;
-    }
-
Quoted 8 lines

drivers/iommu/intel/pasid.c int intel_pasid_setup_first_level(struct intel_iommu *iommu, · line 328

Quoted 4 lines
drivers/iommu/intel/pasid.c +0 -2
-    pasid_clear_entry(pte);
-
       /* Setup the first level page table pointer: */
       pasid_set_flptr(pte, (u64)__pa(pgd));

The above changes the previous assumption that when a new page table is about to be set up on a PASID, there should be no existing one still in place.

actually, this does not break the assumption. In [1]. it already clears the pasid entry.

Quoted 1 line

Is this a requirement for the replace functionality?

Replace would surely need to modify a present pasid entry. But this patch only moves adds the pasid entry tear down and prq drain into the helpers.

Signature · 2 lines
Regards,
Yi Liu
#9 lore raw

RE: [PATCH 3/6] iommu/vt-d: Make helpers support modifying present pasid entry

In reply to Yi Liu
Quoted 17 lines

From: Liu, Yi L <[email protected]>
Sent: Friday, June 28, 2024 4:56 PM

drivers/iommu/intel/pasid.c +3 -0
index b18eebb479de..5d3a12b081a2 100644
@@ -314,6 +314,9 @@ int intel_pasid_setup_first_level(struct intel_iommu
 *iommu,
 		return -EINVAL;
 	}
 
+	/* Clear the old configuration if it already exists */
+	intel_pasid_tear_down_entry(iommu, dev, pasid, false, true);
+
 	spin_lock(&iommu->lock);
 	pte = intel_pasid_get_entry(dev, pasid);
Quoted 1 line

if (!pte) {

with this change there will be two invocations on intel_pasid_tear_down_entry() in the call stack of RID attach:

intel_iommu_attach_device()
device_block_translation()
intel_pasid_tear_down_entry()
dmar_domain_attach_device()
domain_setup_first_level()
intel_pasid_tear_down_entry()

it's not being a real problem as intel_pasid_tear_down_entry() exits early if the pasid entry is non-present, but it will likely cause confusion when reading the code.

What about moving it into intel_iommu_set_dev_pasid() to better show the purpose?

author
#10 lore raw

Re: [PATCH 3/6] iommu/vt-d: Make helpers support modifying present pasid entry

Quoted 29 lines · Kevin
Quoted 17 lines

From: Liu, Yi L <[email protected]>
Sent: Friday, June 28, 2024 4:56 PM

drivers/iommu/intel/pasid.c +3 -0
index b18eebb479de..5d3a12b081a2 100644
@@ -314,6 +314,9 @@ int intel_pasid_setup_first_level(struct intel_iommu
 *iommu,
  		return -EINVAL;
  	}
 
+	/* Clear the old configuration if it already exists */
+	intel_pasid_tear_down_entry(iommu, dev, pasid, false, true);
+
  	spin_lock(&iommu->lock);
  	pte = intel_pasid_get_entry(dev, pasid);
Quoted 1 line
  	if (!pte) {

with this change there will be two invocations on intel_pasid_tear_down_entry() in the call stack of RID attach:

intel_iommu_attach_device()
device_block_translation()
intel_pasid_tear_down_entry()
dmar_domain_attach_device()
domain_setup_first_level()
intel_pasid_tear_down_entry()

Quoted 6 lines

it's not being a real problem as intel_pasid_tear_down_entry() exits early if the pasid entry is non-present, but it will likely cause confusion when reading the code.

What about moving it into intel_iommu_set_dev_pasid() to better show the purpose?

I see. will do.

Signature · 2 lines
Regards,
Yi Liu
author
#11 lore raw

PATCH 4/6 iommu/vt-d: Make intel_iommu_set_dev_pasid() to handle domain replacement

In reply to Yi Liu

set_dev_pasid op is going to support domain replacement and keep the old hardware config if it fails. Make the Intel iommu driver be prepared for it.

---
 drivers/iommu/intel/debugfs.c |  2 +
 drivers/iommu/intel/iommu.c   | 94 +++++++++++++++++++++++------------
 2 files changed, 64 insertions(+), 32 deletions(-)
drivers/iommu/intel/debugfs.c +2 -0
index affbf4a1558d..8deb6bc262c5 100644
@@ -806,5 +806,7 @@ void intel_iommu_debugfs_create_dev_pasid(struct dev_pasid_info *dev_pasid)
 /* Remove the device pasid debugfs directory. */
 void intel_iommu_debugfs_remove_dev_pasid(struct dev_pasid_info *dev_pasid)
 {
+	if (!dev_pasid->debugfs_dentry)
+		return;
 	debugfs_remove_recursive(dev_pasid->debugfs_dentry);
 }
drivers/iommu/intel/iommu.c +62 -32

144 lines not on this page · raw message

Signature · 1 line
2.34.1
#12 lore raw

RE: [PATCH 4/6] iommu/vt-d: Make intel_iommu_set_dev_pasid() to handle domain replacement

Quoted 11 lines

From: Liu, Yi L <[email protected]>
Sent: Friday, June 28, 2024 4:56 PM

drivers/iommu/intel/debugfs.c +2 -0
@@ -806,5 +806,7 @@ void intel_iommu_debugfs_create_dev_pasid(struct
 dev_pasid_info *dev_pasid)
 /* Remove the device pasid debugfs directory. */
 void intel_iommu_debugfs_remove_dev_pasid(struct dev_pasid_info
 *dev_pasid)
 {
+	if (!dev_pasid->debugfs_dentry)
+		return;
Quoted 1 line

debugfs_remove_recursive(dev_pasid->debugfs_dentry);

debugfs_remove_recursive() already includes a check on null entry.

drivers/iommu/intel/iommu.c static int intel_iommu_iotlb_sync_map(struct iommu_domain *domain, · line 4035

Quoted 5 lines

-static void intel_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid,

drivers/iommu/intel/iommu.c +2 -1
-					 struct iommu_domain *domain)
+static void domain_undo_dev_pasid(struct iommu_domain *domain,
+				  struct device *dev, ioasid_t pasid)

'domain_remove_dev_pasid'

drivers/iommu/intel/iommu.c static void intel_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid, · line 4070

Quoted 11 lines

+static void intel_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid,

drivers/iommu/intel/iommu.c +8 -0
+					 struct iommu_domain *domain)
+{
+	struct device_domain_info *info = dev_iommu_priv_get(dev);
+	struct intel_iommu *iommu = info->iommu;
+
 	intel_pasid_tear_down_entry(iommu, dev, pasid, false, true);
+	if (domain->type == IOMMU_DOMAIN_IDENTITY)
+		return;
+	domain_undo_dev_pasid(domain, dev, pasid);

this reverts the order by tearing down the pasid entry in the beginning. but I cannot think of a problem here.

drivers/iommu/intel/iommu.c static int intel_iommu_set_dev_pasid(struct iommu_domain *domain, · line 4148

Quoted 10 lines
drivers/iommu/intel/iommu.c +1 -5
-	dev_pasid->dev = dev;
-	dev_pasid->pasid = pasid;
-	spin_lock_irqsave(&dmar_domain->lock, flags);
-	list_add(&dev_pasid->link_domain, &dmar_domain->dev_pasids);
-	spin_unlock_irqrestore(&dmar_domain->lock, flags);
+	/* Undo the association between the old domain and pasid of a

device */

drivers/iommu/intel/iommu.c +2 -0
+	if (old)
+		domain_undo_dev_pasid(old, dev, pasid);

the comment is useless

author
#13 lore raw

PATCH 5/6 iommu/vt-d: Add set_dev_pasid callback for nested domain

In reply to Yi Liu

From: Lu Baolu <[email protected]>

Extend intel_iommu_set_dev_pasid() to set a nested type domain to a PASID of a device.

---
 drivers/iommu/intel/iommu.c  | 22 +++++++++++++++++-----
 drivers/iommu/intel/iommu.h  |  3 +++
 drivers/iommu/intel/nested.c |  1 +
 3 files changed, 21 insertions(+), 5 deletions(-)
drivers/iommu/intel/iommu.c +17 -5
index 8ef6c06f7e73..b7051d9460cd 100644
@@ -284,6 +284,11 @@ static int __init intel_iommu_setup(char *str)
 }
 __setup("intel_iommu=", intel_iommu_setup);
 
+static int domain_type_is_nested(struct dmar_domain *domain)
+{
+	return domain->domain.type == IOMMU_DOMAIN_NESTED;
+}
+
 static int domain_pfn_supported(struct dmar_domain *domain, unsigned long pfn)
 {
 	int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
@@ -4081,7 +4086,12 @@ domain_prepare_dev_pasid(struct iommu_domain *domain,
 	unsigned long flags;
 	int ret;
 
-	ret = prepare_domain_attach_device(domain, dev);
+	/* Nested type domain should prepare its parent domain */
+	if (domain_type_is_nested(dmar_domain))
+		ret = prepare_domain_attach_device(
+				&dmar_domain->s2_domain->domain, dev);
+	else
+		ret = prepare_domain_attach_device(domain, dev);
 	if (ret)
 		return ERR_PTR(ret);
 
@@ -4111,9 +4121,9 @@ domain_prepare_dev_pasid(struct iommu_domain *domain,
 	return ERR_PTR(ret);
 }
 
-static int intel_iommu_set_dev_pasid(struct iommu_domain *domain,
-				     struct device *dev, ioasid_t pasid,
-				     struct iommu_domain *old)
+int intel_iommu_set_dev_pasid(struct iommu_domain *domain,
+			      struct device *dev, ioasid_t pasid,
+			      struct iommu_domain *old)
 {
 	struct device_domain_info *info = dev_iommu_priv_get(dev);
 	struct dmar_domain *dmar_domain = to_dmar_domain(domain);
@@ -4134,7 +4144,9 @@ static int intel_iommu_set_dev_pasid(struct iommu_domain *domain,
 	if (IS_ERR(dev_pasid))
 		return PTR_ERR(dev_pasid);
 
-	if (dmar_domain->use_first_level)
+	if (domain_type_is_nested(dmar_domain))
+		ret = intel_pasid_setup_nested(iommu, dev, pasid, dmar_domain);
+	else if (dmar_domain->use_first_level)
 		ret = domain_setup_first_level(iommu, dmar_domain,
 					       dev, pasid);
 	else
drivers/iommu/intel/iommu.h +3 -0
index eaf015b4353b..63ef3a8a8832 100644
@@ -1104,6 +1104,9 @@ void device_block_translation(struct device *dev);
 int prepare_domain_attach_device(struct iommu_domain *domain,
 				 struct device *dev);
 void domain_update_iommu_cap(struct dmar_domain *domain);
+int intel_iommu_set_dev_pasid(struct iommu_domain *domain,
+			      struct device *dev, ioasid_t pasid,
+			      struct iommu_domain *old);
 
 int dmar_ir_support(void);
 
drivers/iommu/intel/nested.c +1 -0
index 16a2bcf5cfeb..179868d34a4b 100644
@@ -133,6 +133,7 @@ static int intel_nested_cache_invalidate_user(struct iommu_domain *domain,
 
 static const struct iommu_domain_ops intel_nested_domain_ops = {
 	.attach_dev		= intel_nested_attach_dev,
+	.set_dev_pasid		= intel_iommu_set_dev_pasid,
 	.free			= intel_nested_domain_free,
 	.cache_invalidate_user	= intel_nested_cache_invalidate_user,
 };
Signature · 1 line
2.34.1
author
#14 lore raw

PATCH 6/6 iommu: Make set_dev_pasid op support domain replacement

In reply to Yi Liu

The iommu core is going to support domain replacement for pasid, it needs to make the set_dev_pasid op support replacing domain and keep the old domain config in the failure case.

Currently only the Intel iommu driver supports the latest set_dev_pasid op definition. ARM and AMD iommu driver do not support domain replacement for pasid yet, both drivers would fail the set_dev_pasid op to keep the old config if the input @old is non-NULL.

---
 drivers/iommu/amd/pasid.c                       | 3 +++
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 3 +++
 include/linux/iommu.h                           | 3 ++-
 3 files changed, 8 insertions(+), 1 deletion(-)
drivers/iommu/amd/pasid.c +3 -0
index 77bf5f5f947a..30e27bda3fac 100644
@@ -109,6 +109,9 @@ int iommu_sva_set_dev_pasid(struct iommu_domain *domain,
 	unsigned long flags;
 	int ret = -EINVAL;
 
+	if (old)
+		return -EOPNOTSUPP;
+
 	/* PASID zero is used for requests from the I/O device without PASID */
 	if (!is_pasid_valid(dev_data, pasid))
 		return ret;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +3 -0
index c058949749cb..a1e411c71efa 100644
@@ -637,6 +637,9 @@ static int arm_smmu_sva_set_dev_pasid(struct iommu_domain *domain,
 	int ret = 0;
 	struct mm_struct *mm = domain->mm;
 
+	if (old)
+		return -EOPNOTSUPP;
+
 	if (mm_get_enqcmd_pasid(mm) != id)
 		return -EINVAL;
 
include/linux/iommu.h +2 -1
index a33f53aab61b..3259f77ff2e3 100644
@@ -607,7 +607,8 @@ struct iommu_ops {
  * * EBUSY	- device is attached to a domain and cannot be changed
  * * ENODEV	- device specific errors, not able to be attached
  * * <others>	- treated as ENODEV by the caller. Use is discouraged
- * @set_dev_pasid: set an iommu domain to a pasid of device
+ * @set_dev_pasid: set or replace an iommu domain to a pasid of device. The pasid of
+ *                 the device should be left in the old config in error case.
  * @map_pages: map a physically contiguous set of pages of the same size to
  *             an iommu domain.
  * @unmap_pages: unmap a number of pages of the same size from an iommu domain
Signature · 1 line
2.34.1
#15 lore raw

RE: [PATCH 6/6] iommu: Make set_dev_pasid op support domain replacement

Quoted 7 lines

From: Liu, Yi L <[email protected]>
Sent: Friday, June 28, 2024 4:56 PM

The iommu core is going to support domain replacement for pasid, it needs to make the set_dev_pasid op support replacing domain and keep the old domain config in the failure case.

Quoted 5 lines

Currently only the Intel iommu driver supports the latest set_dev_pasid op definition. ARM and AMD iommu driver do not support domain replacement
for pasid yet, both drivers would fail the set_dev_pasid op to keep the old config if the input @old is non-NULL.

why splitting this from patch01?

Quoted 60 lines
---
 drivers/iommu/amd/pasid.c                       | 3 +++
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 3 +++
 include/linux/iommu.h                           | 3 ++-
 3 files changed, 8 insertions(+), 1 deletion(-)
drivers/iommu/amd/pasid.c +3 -0
index 77bf5f5f947a..30e27bda3fac 100644
@@ -109,6 +109,9 @@ int iommu_sva_set_dev_pasid(struct iommu_domain
 *domain,
 	unsigned long flags;
 	int ret = -EINVAL;
 
+	if (old)
+		return -EOPNOTSUPP;
+
 	/* PASID zero is used for requests from the I/O device without PASID
 */
drivers/iommu/amd/pasid.c +0 -0
 	if (!is_pasid_valid(dev_data, pasid))
 		return ret;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +3 -0
index c058949749cb..a1e411c71efa 100644
@@ -637,6 +637,9 @@ static int arm_smmu_sva_set_dev_pasid(struct
 iommu_domain *domain,
 	int ret = 0;
 	struct mm_struct *mm = domain->mm;
 
+	if (old)
+		return -EOPNOTSUPP;
+
 	if (mm_get_enqcmd_pasid(mm) != id)
 		return -EINVAL;
include/linux/iommu.h +1 -1
index a33f53aab61b..3259f77ff2e3 100644
@@ -607,7 +607,8 @@ struct iommu_ops {
  * * EBUSY	- device is attached to a domain and cannot be changed
  * * ENODEV	- device specific errors, not able to be attached
  * * <others>	- treated as ENODEV by the caller. Use is discouraged
- * @set_dev_pasid: set an iommu domain to a pasid of device
+ * @set_dev_pasid: set or replace an iommu domain to a pasid of device.

The pasid of
+ * the device should be left in the old config in error case.
* @map_pages: map a physically contiguous set of pages of the same size
to
* an iommu domain.
* @unmap_pages: unmap a number of pages of the same size from an
iommu domain
--
2.34.1

author
#16 lore raw

Re: [PATCH 6/6] iommu: Make set_dev_pasid op support domain replacement

Quoted 7 lines · Kevin
Quoted 7 lines

From: Liu, Yi L <[email protected]>
Sent: Friday, June 28, 2024 4:56 PM

The iommu core is going to support domain replacement for pasid, it needs to make the set_dev_pasid op support replacing domain and keep the old domain config in the failure case.

Quoted 7 lines
Quoted 5 lines

Currently only the Intel iommu driver supports the latest set_dev_pasid op definition. ARM and AMD iommu driver do not support domain replacement
for pasid yet, both drivers would fail the set_dev_pasid op to keep the old config if the input @old is non-NULL.

why splitting this from patch01?

The major reason is we already have multiple set_dev_pasid callbacks. It's better to make the existing callbacks suit the new definition before claiming it. Although this is not quite true for ARM and AMD since their set_dev_pasid() callback would fail replacement attempts. But it looks like there is no pasid domain replacement usage on AMD and ARM due to arch difference.

Quoted 61 lines
Quoted 60 lines
---
  drivers/iommu/amd/pasid.c                       | 3 +++
  drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 3 +++
  include/linux/iommu.h                           | 3 ++-
  3 files changed, 8 insertions(+), 1 deletion(-)
drivers/iommu/amd/pasid.c +3 -0
index 77bf5f5f947a..30e27bda3fac 100644
@@ -109,6 +109,9 @@ int iommu_sva_set_dev_pasid(struct iommu_domain
 *domain,
  	unsigned long flags;
  	int ret = -EINVAL;
 
+	if (old)
+		return -EOPNOTSUPP;
+
  	/* PASID zero is used for requests from the I/O device without PASID
 */

if (!is_pasid_valid(dev_data, pasid))
return ret;

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +3 -0
index c058949749cb..a1e411c71efa 100644
@@ -637,6 +637,9 @@ static int arm_smmu_sva_set_dev_pasid(struct
 iommu_domain *domain,
  	int ret = 0;
  	struct mm_struct *mm = domain->mm;
 
+	if (old)
+		return -EOPNOTSUPP;
+
  	if (mm_get_enqcmd_pasid(mm) != id)
  		return -EINVAL;
include/linux/iommu.h +1 -1
index a33f53aab61b..3259f77ff2e3 100644
@@ -607,7 +607,8 @@ struct iommu_ops {
   * * EBUSY	- device is attached to a domain and cannot be changed
   * * ENODEV	- device specific errors, not able to be attached
   * * <others>	- treated as ENODEV by the caller. Use is discouraged
- * @set_dev_pasid: set an iommu domain to a pasid of device
+ * @set_dev_pasid: set or replace an iommu domain to a pasid of device.

The pasid of
+ * the device should be left in the old config in error case.
* @map_pages: map a physically contiguous set of pages of the same size
to
* an iommu domain.
* @unmap_pages: unmap a number of pages of the same size from an
iommu domain
--
2.34.1

Signature · 2 lines
Regards,
Yi Liu
#17 lore raw
In reply to Yi Liu
Quoted 10 lines

From: Liu, Yi L <[email protected]>
Sent: Friday, June 28, 2024 4:56 PM

This splits the preparation works of the iommu and the Intel iommu driver out from the iommufd pasid attach/replace series. [1]

To support domain replacement, the definition of the set_dev_pasid op needs to be enhanced. Meanwhile, the existing set_dev_pasid callbacks should be extended as well to suit the new definition.

Quoted 6 lines

pasid attach/replace is mandatory on Intel VT-d given the PASID table locates in the physical address space hence must be managed by the kernel, both for supporting vSVA and coming SIOV. But it's optional on ARM/AMD which allow configuring the PASID/CD table either in host physical address space or nested on top of an GPA address space. This series only extends the Intel iommu driver as the minimal requirement.

Looks above is only within VFIO/IOMMUFD context (copied from the old series). But this series is all in IOMMU and pasid attach is certainly not optional for SVA on all platforms. this needs to be revised.

Quoted 46 lines

This series first prepares the Intel iommu set_dev_pasid op for the new definition, adds the missing set_dev_pasid support for nested domain, and in the end enhances the definition of set_dev_pasid op. The ARM and AMD set_dev_pasid callbacks is extended to fail if the caller tries to do domain replacement to meet the new definition of set_dev_pasid op.

This series is on top of Baolu's paging domain alloc refactor, where his code can be found at [2].

[1] https://lore.kernel.org/linux-iommu/20240412081516.31168-1-
[email protected]/
[2] https://github.com/LuBaolu/intel-iommu/commits/vtd-paging-domain-
refactor-v1

Regards,
Yi Liu

Lu Baolu (1):
iommu/vt-d: Add set_dev_pasid callback for nested domain

Yi Liu (5):
iommu: Pass old domain to set_dev_pasid op iommu/vt-d: Move intel_drain_pasid_prq() into
intel_pasid_tear_down_entry()
iommu/vt-d: Make helpers support modifying present pasid entry iommu/vt-d: Make intel_iommu_set_dev_pasid() to handle domain
replacement
iommu: Make set_dev_pasid op support domain replacement

 drivers/iommu/amd/amd_iommu.h                 |   3 +-
 drivers/iommu/amd/pasid.c                     |   6 +-
 .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c   |   6 +-
 drivers/iommu/intel/debugfs.c                 |   2 +
 drivers/iommu/intel/iommu.c                   | 117 ++++++++++++------
 drivers/iommu/intel/iommu.h                   |   3 +
 drivers/iommu/intel/nested.c                  |   1 +
 drivers/iommu/intel/pasid.c                   |  46 +++----
 drivers/iommu/intel/pasid.h                   |   5 +-
 drivers/iommu/intel/svm.c                     |   9 +-
 drivers/iommu/iommu.c                         |   3 +-
 include/linux/iommu.h                         |   5 +-
 12 files changed, 132 insertions(+), 74 deletions(-)

--
2.34.1

#18 lore raw
Quoted 17 lines · Kevin
Quoted 10 lines

From: Liu, Yi L <[email protected]>
Sent: Friday, June 28, 2024 4:56 PM

This splits the preparation works of the iommu and the Intel iommu driver out from the iommufd pasid attach/replace series. [1]

To support domain replacement, the definition of the set_dev_pasid op needs to be enhanced. Meanwhile, the existing set_dev_pasid callbacks should be extended as well to suit the new definition.

Quoted 6 lines

pasid attach/replace is mandatory on Intel VT-d given the PASID table locates in the physical address space hence must be managed by the kernel, both for supporting vSVA and coming SIOV. But it's optional on ARM/AMD which allow configuring the PASID/CD table either in host physical address space or nested on top of an GPA address space. This series only extends the Intel iommu driver as the minimal requirement.

Quoted 3 lines

Looks above is only within VFIO/IOMMUFD context (copied from the old series). But this series is all in IOMMU and pasid attach is certainly not optional for SVA on all platforms. this needs to be revised.

I feel like we should explicitly block replace on AMD by checking if old_domain is !NULL and failing.

Then the description is sort of like

Replace is useful for iommufd/VFIO to provide perfect HW emulation in case the VM is expecting to be able to change a PASID on the fly. As AMD will only support PASID in VM's using nested translation where we don't use the set_dev_pasid API leave it disabled for now.

Jason

#19 lore raw
Quoted 25 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, July 12, 2024 2:41 AM

Quoted 4 lines · Kevin
Quoted 4 lines

From: Liu, Yi L <[email protected]>
Sent: Friday, June 28, 2024 4:56 PM

This splits the preparation works of the iommu and the Intel iommu

driver

Quoted 8 lines
Quoted 8 lines

out from the iommufd pasid attach/replace series. [1]

To support domain replacement, the definition of the set_dev_pasid op needs to be enhanced. Meanwhile, the existing set_dev_pasid callbacks should be extended as well to suit the new definition.

pasid attach/replace is mandatory on Intel VT-d given the PASID table locates in the physical address space hence must be managed by the

kernel,

Quoted 1 line
Quoted 1 line

both for supporting vSVA and coming SIOV. But it's optional on

ARM/AMD

Quoted 1 line
Quoted 1 line

which allow configuring the PASID/CD table either in host physical

address

Quoted 3 lines
Quoted 2 lines

space or nested on top of an GPA address space. This series only extends the Intel iommu driver as the minimal requirement.

Quoted 6 lines
Quoted 3 lines

Looks above is only within VFIO/IOMMUFD context (copied from the old series). But this series is all in IOMMU and pasid attach is certainly not optional for SVA on all platforms. this needs to be revised.

I feel like we should explicitly block replace on AMD by checking if old_domain is !NULL and failing.

this is what patch06 does.

Quoted 8 lines

Then the description is sort of like

Replace is useful for iommufd/VFIO to provide perfect HW emulation in case the VM is expecting to be able to change a PASID on the fly. As AMD will only support PASID in VM's using nested translation where we don't use the set_dev_pasid API leave it disabled for now.

yes that's clearer.

btw I don't remember whether we have discussed the rationale behind the different driver semantics between RID and PASID. Currently RID replace is same as RID attach, with the driver simply blocking the old translation from the start and then no rollback upon failure when switching to the new domain (expecting the iommu core to recover), while for PASID replace we expect the driver to implement the hitless switch.

Is it because there is no need of perfect HW emulation for RID or just to be cleaned up later?

This difference at least starts bringing some puzzle to me when reading the related code in the intel-iommu driver.

#20 lore raw
Quoted 18 lines · Kevin
Quoted 7 lines

Then the description is sort of like

Replace is useful for iommufd/VFIO to provide perfect HW emulation in case the VM is expecting to be able to change a PASID on the fly. As AMD will only support PASID in VM's using nested translation where we don't use the set_dev_pasid API leave it disabled for now.

yes that's clearer.

btw I don't remember whether we have discussed the rationale behind the different driver semantics between RID and PASID. Currently RID replace is same as RID attach, with the driver simply blocking the old translation from the start and then no rollback upon failure when switching to the new domain (expecting the iommu core to recover), while for PASID replace we expect the driver to implement the hitless switch.

Quoted 2 lines

Is it because there is no need of perfect HW emulation for RID or just to be cleaned up later?

Yeah, too much legacy it would be hard to go in and do something about this for RID across every driver.

PASID can be a bit more well defined from the start since we only have three drivers and two of them support replace.

An ideal driver should be like ARM and support hitless replace whenever possible in all paths so there is no confusion within the driver.

Jason

author
#21 lore raw
In reply to Jason Gunthorpe
Quoted 18 lines · Jason Gunthorpe
Quoted 17 lines · Kevin
Quoted 16 lines

From: Liu, Yi L <[email protected]>
Sent: Friday, June 28, 2024 4:56 PM

This splits the preparation works of the iommu and the Intel iommu driver out from the iommufd pasid attach/replace series. [1]

To support domain replacement, the definition of the set_dev_pasid op needs to be enhanced. Meanwhile, the existing set_dev_pasid callbacks should be extended as well to suit the new definition.

pasid attach/replace is mandatory on Intel VT-d given the PASID table locates in the physical address space hence must be managed by the kernel, both for supporting vSVA and coming SIOV. But it's optional on ARM/AMD which allow configuring the PASID/CD table either in host physical address space or nested on top of an GPA address space. This series only extends the Intel iommu driver as the minimal requirement.

Quoted 6 lines
Quoted 3 lines

Looks above is only within VFIO/IOMMUFD context (copied from the old series). But this series is all in IOMMU and pasid attach is certainly not optional for SVA on all platforms. this needs to be revised.

I feel like we should explicitly block replace on AMD by checking if old_domain is !NULL and failing.

yes, patch 6 of this series has made it fail in AMD's set_dev_pasid callback.

Quoted 6 lines

Then the description is sort of like

Replace is useful for iommufd/VFIO to provide perfect HW emulation in case the VM is expecting to be able to change a PASID on the fly. As AMD will only support PASID in VM's using nested translation where we don't use the set_dev_pasid API leave it disabled for now.

Does it apply to SMMUv3 as well? IIRC. ARM SMMUv3 also has the CD table (a.k.a PASID table) within the guest. And I think this is the major reason for your above statement. right?

Signature · 2 lines
Regards,
Yi Liu
#22 lore raw
Quoted 10 lines · Yi Liu
Quoted 6 lines

Then the description is sort of like

Replace is useful for iommufd/VFIO to provide perfect HW emulation in case the VM is expecting to be able to change a PASID on the fly. As AMD will only support PASID in VM's using nested translation where we don't use the set_dev_pasid API leave it disabled for now.

Does it apply to SMMUv3 as well? IIRC. ARM SMMUv3 also has the CD table (a.k.a PASID table) within the guest. And I think this is the major reason for your above statement. right?

It does, but it also supports replace so no need to explain why we don't enable it :)

Jason

author
#23 lore raw
Quoted 8 lines · Jason Gunthorpe
Quoted 7 lines · Yi Liu
Quoted 6 lines

Then the description is sort of like

Replace is useful for iommufd/VFIO to provide perfect HW emulation in case the VM is expecting to be able to change a PASID on the fly. As AMD will only support PASID in VM's using nested translation where we don't use the set_dev_pasid API leave it disabled for now.

Quoted 6 lines
Quoted 3 lines

Does it apply to SMMUv3 as well? IIRC. ARM SMMUv3 also has the CD table (a.k.a PASID table) within the guest. And I think this is the major reason for your above statement. right?

It does, but it also supports replace so no need to explain why we don't enable it :)

got it. :)

Signature · 2 lines
Regards,
Yi Liu
#24 lore raw
In reply to Yi Liu
Quoted 7 lines · Yi Liu

This splits the preparation works of the iommu and the Intel iommu driver out from the iommufd pasid attach/replace series. [1]

To support domain replacement, the definition of the set_dev_pasid op needs to be enhanced. Meanwhile, the existing set_dev_pasid callbacks should be extended as well to suit the new definition.

Quoted 6 lines

pasid attach/replace is mandatory on Intel VT-d given the PASID table locates in the physical address space hence must be managed by the kernel, both for supporting vSVA and coming SIOV. But it's optional on ARM/AMD which allow configuring the PASID/CD table either in host physical address space or nested on top of an GPA address space. This series only extends the Intel iommu driver as the minimal requirement.

Sicne this will be pushed to the next cyle that will have my ARM code the smmuv3 will need to be updated too. It is already prepped to support replace, just add this please:

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +3 -2
index ead83d67421f10..44434978a218ae 100644
@@ -350,7 +351,8 @@ void arm_smmu_sva_notifier_synchronize(void)
 }
 
 static int arm_smmu_sva_set_dev_pasid(struct iommu_domain *domain,
-				      struct device *dev, ioasid_t id)
+				      struct device *dev, ioasid_t id,
+				      struct iommu_domain *old_domain)
 {
 	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
 	struct arm_smmu_master *master = dev_iommu_priv_get(dev);
@@ -367,7 +369,7 @@ static int arm_smmu_sva_set_dev_pasid(struct iommu_domain *domain,
 	 */
 	arm_smmu_make_sva_cd(&target, master, domain->mm, smmu_domain->asid,
 			     smmu_domain->btm_invalidation);
-	ret = arm_smmu_set_pasid(master, smmu_domain, id, &target);
+	ret = arm_smmu_set_pasid(master, smmu_domain, id, &target, old_domain);
 
 	mmput(domain->mm);
 	return ret;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +5 -3
index 238968b1709936..140aac5cd4ef57 100644
@@ -2943,7 +2943,8 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
 }
 
 static int arm_smmu_s1_set_dev_pasid(struct iommu_domain *domain,
-				      struct device *dev, ioasid_t id)
+				      struct device *dev, ioasid_t id,
+				      struct iommu_domain *old_domain)
 {
 	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
 	struct arm_smmu_master *master = dev_iommu_priv_get(dev);
@@ -2969,7 +2970,7 @@ static int arm_smmu_s1_set_dev_pasid(struct iommu_domain *domain,
 	 */
 	arm_smmu_make_s1_cd(&target_cd, master, smmu_domain);
 	return arm_smmu_set_pasid(master, to_smmu_domain(domain), id,
-				  &target_cd);
+				  &target_cd, old_domain);
 }
 
 static void arm_smmu_update_ste(struct arm_smmu_master *master,
@@ -2999,7 +3000,7 @@ static void arm_smmu_update_ste(struct arm_smmu_master *master,
 
 int arm_smmu_set_pasid(struct arm_smmu_master *master,
 		       struct arm_smmu_domain *smmu_domain, ioasid_t pasid,
-		       struct arm_smmu_cd *cd)
+		       struct arm_smmu_cd *cd, struct iommu_domain *old_domain)
 {
 	struct iommu_domain *sid_domain = iommu_get_domain_for_dev(master->dev);
 	struct arm_smmu_attach_state state = {
@@ -3009,6 +3010,7 @@ int arm_smmu_set_pasid(struct arm_smmu_master *master,
 		 * already attached, no need to set old_domain.
 		 */
 		.ssid = pasid,
+		.old_domain = old_domain,
 	};
 	struct arm_smmu_cd *cdptr;
 	int ret;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +1 -1
index bcf9ea9d929f5f..447a3cdf1c4e1c 100644
@@ -828,7 +828,7 @@ void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid,
 
 int arm_smmu_set_pasid(struct arm_smmu_master *master,
 		       struct arm_smmu_domain *smmu_domain, ioasid_t pasid,
-		       struct arm_smmu_cd *cd);
+		       struct arm_smmu_cd *cd, struct iommu_domain *old_domain);
 
 int arm_smmu_domain_alloc_id(struct arm_smmu_device *smmu,
 			     struct arm_smmu_domain *smmu_domain);
author
#25 lore raw
Quoted 15 lines · Jason Gunthorpe
Quoted 7 lines · Yi Liu

This splits the preparation works of the iommu and the Intel iommu driver out from the iommufd pasid attach/replace series. [1]

To support domain replacement, the definition of the set_dev_pasid op needs to be enhanced. Meanwhile, the existing set_dev_pasid callbacks should be extended as well to suit the new definition.

Quoted 6 lines

pasid attach/replace is mandatory on Intel VT-d given the PASID table locates in the physical address space hence must be managed by the kernel, both for supporting vSVA and coming SIOV. But it's optional on ARM/AMD which allow configuring the PASID/CD table either in host physical address space or nested on top of an GPA address space. This series only extends the Intel iommu driver as the minimal requirement.

Quoted 3 lines

Sicne this will be pushed to the next cyle that will have my ARM code the smmuv3 will need to be updated too. It is already prepped to support replace, just add this please:

thanks. So your related series has made the internal helpers to support domain replacement in set_dev_pasid path. The below diff just passes the old_domain across the helpers. Is it? Just to double confirm with you. :)

Quoted 77 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +3 -2
index ead83d67421f10..44434978a218ae 100644
@@ -350,7 +351,8 @@ void arm_smmu_sva_notifier_synchronize(void)
  }
 
  static int arm_smmu_sva_set_dev_pasid(struct iommu_domain *domain,
-				      struct device *dev, ioasid_t id)
+				      struct device *dev, ioasid_t id,
+				      struct iommu_domain *old_domain)
  {
  	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  	struct arm_smmu_master *master = dev_iommu_priv_get(dev);
@@ -367,7 +369,7 @@ static int arm_smmu_sva_set_dev_pasid(struct iommu_domain *domain,
  	 */
  	arm_smmu_make_sva_cd(&target, master, domain->mm, smmu_domain->asid,
  			     smmu_domain->btm_invalidation);
-	ret = arm_smmu_set_pasid(master, smmu_domain, id, &target);
+	ret = arm_smmu_set_pasid(master, smmu_domain, id, &target, old_domain);
 
  	mmput(domain->mm);
  	return ret;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +5 -3
index 238968b1709936..140aac5cd4ef57 100644
@@ -2943,7 +2943,8 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
  }
 
  static int arm_smmu_s1_set_dev_pasid(struct iommu_domain *domain,
-				      struct device *dev, ioasid_t id)
+				      struct device *dev, ioasid_t id,
+				      struct iommu_domain *old_domain)
  {
  	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  	struct arm_smmu_master *master = dev_iommu_priv_get(dev);
@@ -2969,7 +2970,7 @@ static int arm_smmu_s1_set_dev_pasid(struct iommu_domain *domain,
  	 */
  	arm_smmu_make_s1_cd(&target_cd, master, smmu_domain);
  	return arm_smmu_set_pasid(master, to_smmu_domain(domain), id,
-				  &target_cd);
+				  &target_cd, old_domain);
  }
 
  static void arm_smmu_update_ste(struct arm_smmu_master *master,
@@ -2999,7 +3000,7 @@ static void arm_smmu_update_ste(struct arm_smmu_master *master,
 
  int arm_smmu_set_pasid(struct arm_smmu_master *master,
  		       struct arm_smmu_domain *smmu_domain, ioasid_t pasid,
-		       struct arm_smmu_cd *cd)
+		       struct arm_smmu_cd *cd, struct iommu_domain *old_domain)
  {
  	struct iommu_domain *sid_domain = iommu_get_domain_for_dev(master->dev);
  	struct arm_smmu_attach_state state = {
@@ -3009,6 +3010,7 @@ int arm_smmu_set_pasid(struct arm_smmu_master *master,
  		 * already attached, no need to set old_domain.
  		 */
  		.ssid = pasid,
+		.old_domain = old_domain,
  	};
  	struct arm_smmu_cd *cdptr;
  	int ret;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +1 -1
index bcf9ea9d929f5f..447a3cdf1c4e1c 100644
@@ -828,7 +828,7 @@ void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid,
 
  int arm_smmu_set_pasid(struct arm_smmu_master *master,
  		       struct arm_smmu_domain *smmu_domain, ioasid_t pasid,
-		       struct arm_smmu_cd *cd);
+		       struct arm_smmu_cd *cd, struct iommu_domain *old_domain);
 
  int arm_smmu_domain_alloc_id(struct arm_smmu_device *smmu,
  			     struct arm_smmu_domain *smmu_domain);
Signature · 2 lines
Regards,
Yi Liu
#26 lore raw
Quoted 16 lines · Yi Liu
Quoted 15 lines · Jason Gunthorpe
Quoted 13 lines · Yi Liu

This splits the preparation works of the iommu and the Intel iommu driver out from the iommufd pasid attach/replace series. [1]

To support domain replacement, the definition of the set_dev_pasid op needs to be enhanced. Meanwhile, the existing set_dev_pasid callbacks should be extended as well to suit the new definition.

pasid attach/replace is mandatory on Intel VT-d given the PASID table locates in the physical address space hence must be managed by the kernel, both for supporting vSVA and coming SIOV. But it's optional on ARM/AMD which allow configuring the PASID/CD table either in host physical address space or nested on top of an GPA address space. This series only extends the Intel iommu driver as the minimal requirement.

Quoted 7 lines
Quoted 3 lines

Sicne this will be pushed to the next cyle that will have my ARM code the smmuv3 will need to be updated too. It is already prepped to support replace, just add this please:

thanks. So your related series has made the internal helpers to support domain replacement in set_dev_pasid path. The below diff just passes the old_domain across the helpers. Is it? Just to double confirm with you. :)

Yes

Jason

maintainer
#27 lore raw
In reply to Yi Liu

Hi All,

Quoted 6 lines · Yi Liu

This splits the preparation works of the iommu and the Intel iommu driver out from the iommufd pasid attach/replace series. [1]

To support domain replacement, the definition of the set_dev_pasid op needs to be enhanced. Meanwhile, the existing set_dev_pasid callbacks should be extended as well to suit the new definition.

IIUC this will remove PASID from old SVA domain and attaches to new SVA domain. (basically attaching same dev/PASID to different process). Is that the correct?

So the expectation is replace existing PASID from PASID table only if old_domain is passed. Otherwise sev_dev_pasid() should throw an error right?

-Vasant

author
#28 lore raw
Quoted 6 lines · Vasant Hegde

Hi All,

Quoted 3 lines · Yi Liu

This splits the preparation works of the iommu and the Intel iommu driver out from the iommufd pasid attach/replace series. [1]

Quoted 6 lines
Quoted 3 lines

To support domain replacement, the definition of the set_dev_pasid op needs to be enhanced. Meanwhile, the existing set_dev_pasid callbacks should be extended as well to suit the new definition.

IIUC this will remove PASID from old SVA domain and attaches to new SVA domain. (basically attaching same dev/PASID to different process). Is that the correct?

In brief, yes. But it's not only for SVA domain. Remember that SIOVr1 extends the usage of PASID. At least on Intel side, a PASID may be attached to paging domains.

Quoted 3 lines

So the expectation is replace existing PASID from PASID table only if old_domain is passed. Otherwise sev_dev_pasid() should throw an error right?

yes. If no old_domain passed in, then it is just a normal attachment. As you are working on AMD iommu, it would be great if you can have a patch to make the AMD set_dev_pasid() op suit this expectation. Then it can be incorporated in this series. :)

Signature · 2 lines
Regards,
Yi Liu
maintainer
#29 lore raw
Quoted 11 lines · Yi Liu
Quoted 10 lines · Vasant Hegde

Hi All,

Quoted 6 lines · Yi Liu

This splits the preparation works of the iommu and the Intel iommu driver out from the iommufd pasid attach/replace series. [1]

To support domain replacement, the definition of the set_dev_pasid op needs to be enhanced. Meanwhile, the existing set_dev_pasid callbacks should be extended as well to suit the new definition.

Quoted 6 lines
Quoted 2 lines

IIUC this will remove PASID from old SVA domain and attaches to new SVA domain. (basically attaching same dev/PASID to different process). Is that the correct?

In brief, yes. But it's not only for SVA domain. Remember that SIOVr1 extends the usage of PASID. At least on Intel side, a PASID may be attached to paging domains.

You are correct.

The idxd driver attaches a paging domain to a non-zero PASID for kernel DMA with PASID. From an architectural perspective, other architectures, like ARM, AMD, and RISC-V, also support this. Therefore, attaching a paging domain to a PASID is not Intel-specific but a generic feature.

Thanks,
baolu

maintainer
#30 lore raw

Baolu,

Quoted 12 lines · Baolu Lu
Quoted 11 lines · Yi Liu
Quoted 10 lines · Vasant Hegde

Hi All,

Quoted 6 lines · Yi Liu

This splits the preparation works of the iommu and the Intel iommu driver out from the iommufd pasid attach/replace series. [1]

To support domain replacement, the definition of the set_dev_pasid op needs to be enhanced. Meanwhile, the existing set_dev_pasid callbacks should be extended as well to suit the new definition.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

IIUC this will remove PASID from old SVA domain and attaches to new SVA domain. (basically attaching same dev/PASID to different process). Is that the correct?

In brief, yes. But it's not only for SVA domain. Remember that SIOVr1 extends the usage of PASID. At least on Intel side, a PASID may be attached to paging domains.

You are correct.

Thanks.

Quoted 5 lines

The idxd driver attaches a paging domain to a non-zero PASID for kernel DMA with PASID. From an architectural perspective, other architectures, like ARM, AMD, and RISC-V, also support this. Therefore, attaching a paging domain to a PASID is not Intel-specific but a generic feature.

Right. We can enhance AMD driver to support attaching PASID to paging/UNMANAGED domains. It needs some more rework/cleanup before we do that.

-Vasant

maintainer
#31 lore raw
In reply to Yi Liu
Quoted 8 lines · Yi Liu
Quoted 3 lines

So the expectation is replace existing PASID from PASID table only if old_domain is passed. Otherwise sev_dev_pasid() should throw an error right?

yes. If no old_domain passed in, then it is just a normal attachment. As you are working on AMD iommu, it would be great if you can have a patch to make the AMD set_dev_pasid() op suit this expectation. Then it can be incorporated in this series. 🙂

Perhaps this has been discussed before, but I can't recall. Out of curiosity, why not introduce a specific replace_dev_pasid callback?

Thanks,
baolu

author
#32 lore raw
Quoted 5 lines · Baolu Lu
Quoted 4 lines · Yi Liu
Quoted 3 lines

So the expectation is replace existing PASID from PASID table only if old_domain is passed. Otherwise sev_dev_pasid() should throw an error right?

Quoted 7 lines
Quoted 4 lines

yes. If no old_domain passed in, then it is just a normal attachment. As you are working on AMD iommu, it would be great if you can have a patch to make the AMD set_dev_pasid() op suit this expectation. Then it can be incorporated in this series. 🙂

Perhaps this has been discussed before, but I can't recall. Out of curiosity, why not introduce a specific replace_dev_pasid callback?

you have a good memory. It was discussed in the below link. I suppose it reached agreement at that time.

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

Signature · 2 lines
Regards,
Yi Liu
maintainer
#33 lore raw
In reply to Yi Liu

Yi,

Quoted 11 lines · Yi Liu
Quoted 10 lines · Vasant Hegde

Hi All,

Quoted 6 lines · Yi Liu

This splits the preparation works of the iommu and the Intel iommu driver out from the iommufd pasid attach/replace series. [1]

To support domain replacement, the definition of the set_dev_pasid op needs to be enhanced. Meanwhile, the existing set_dev_pasid callbacks should be extended as well to suit the new definition.

Quoted 6 lines
Quoted 2 lines

IIUC this will remove PASID from old SVA domain and attaches to new SVA domain. (basically attaching same dev/PASID to different process). Is that the correct?

In brief, yes. But it's not only for SVA domain. Remember that SIOVr1 extends the usage of PASID. At least on Intel side, a PASID may be attached to paging domains.

Right. I missed SIOV case.

Quoted 9 lines
Quoted 4 lines

So the expectation is replace existing PASID from PASID table only if old_domain is passed. Otherwise sev_dev_pasid() should throw an error right?

yes. If no old_domain passed in, then it is just a normal attachment. As you are working on AMD iommu, it would be great if you can have a patch to make the AMD set_dev_pasid() op suit this expectation. Then it can be incorporated in this series. :)'

Sure. It should be simple. I will try to get the patch next week.

-Vasant