Skip to contentSkip to conversation

iommu/vt-d: Force snooping improvement

PATCH Superseded Lu Baolu 3 participants 20 messages May 1, 2022 – May 4, 2022

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Lu Baolu

author maintainer
#0 lore raw

cover iommu/vt-d: Force snooping improvement

Hi folks,

Previously, the IOMMU capability of enforcing cache coherency is queried through iommu_capable(IOMMU_CAP_CACHE_COHERENCY). This is a global capability, hence the IOMMU driver reports support for this capability only when all IOMMUs in the system has this support.

Commit 6043257b1de06 ("iommu: Introduce the domain op enforce_cache_coherency()") converts this into a per-domain test-and-set option, and the previous iommu_capable(IOMMU_CAP_CACHE_COHERENCY) is deprecated.

This is a follow-up series which improves the Intel IOMMU driver to support the per-domain scheme better.

Best regards,
baolu

Lu Baolu (5):
iommu/vt-d: Block force-snoop domain attaching if no SC support iommu/vt-d: Set SNP bit only in second-level page table entries iommu/vt-d: Check domain force_snooping against attached devices iommu/vt-d: Remove domain_update_iommu_snooping() iommu/vt-d: Remove hard coding PGSNP bit in PASID entries

 drivers/iommu/intel/pasid.h |  3 +-
 drivers/iommu/intel/iommu.c | 92 ++++++++++++++++++++++---------------
 drivers/iommu/intel/pasid.c | 24 +++++++---
 3 files changed, 74 insertions(+), 45 deletions(-)
Signature · 1 line
2.25.1
author maintainer
#1 lore raw

PATCH 1/5 iommu/vt-d: Block force-snoop domain attaching if no SC support

In the attach_dev callback of the default domain ops, if the domain has been set force_snooping, but the iommu hardware of the device does not support SC(Snoop Control) capability, the callback should block it and return a corresponding error code.

---
 drivers/iommu/intel/iommu.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/iommu/intel/iommu.c +3 -0
index cf43e8f9091b..d68f5bbf3e93 100644
@@ -4354,6 +4354,9 @@ static int prepare_domain_attach_device(struct iommu_domain *domain,
 	if (!iommu)
 		return -ENODEV;
 
+	if (dmar_domain->force_snooping && !ecap_sc_support(iommu->ecap))
+		return -EOPNOTSUPP;
+
 	/* check if this iommu agaw is sufficient for max mapped address */
 	addr_width = agaw_to_width(iommu->agaw);
 	if (addr_width > cap_mgaw(iommu->cap))
Signature · 6 lines
2.25.1

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

PATCH 2/5 iommu/vt-d: Set SNP bit only in second-level page table entries

In reply to Lu Baolu

The SNP bit is only valid for second-level PTEs. Setting this bit in the first-level PTEs has no functional impact because the Intel IOMMU always ignores the same bit in first-level PTEs. Anyway, let's check the page table type before setting SNP bit in PTEs to make the code more readable.

---
 drivers/iommu/intel/iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/intel/iommu.c +1 -1
index d68f5bbf3e93..98050943d863 100644
@@ -4431,7 +4431,7 @@ static int intel_iommu_map(struct iommu_domain *domain,
 		prot |= DMA_PTE_READ;
 	if (iommu_prot & IOMMU_WRITE)
 		prot |= DMA_PTE_WRITE;
-	if (dmar_domain->force_snooping)
+	if (dmar_domain->force_snooping && !domain_use_first_level(dmar_domain))
 		prot |= DMA_PTE_SNP;
 
 	max_addr = iova + size;
Signature · 6 lines
2.25.1

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

PATCH 3/5 iommu/vt-d: Check domain force_snooping against attached devices

In reply to Lu Baolu

As domain->force_snooping only impacts the devices attached with the domain, there's no need to check against all IOMMU units. At the same time, for a brand new domain (hasn't been attached to any device), the force_snooping field could be set, but the attach_dev callback will return failure if it wants to attach to a device which IOMMU has no snoop control capability.

---
 drivers/iommu/intel/pasid.h |  2 ++
 drivers/iommu/intel/iommu.c | 50 ++++++++++++++++++++++++++++++++++++-
 drivers/iommu/intel/pasid.c | 18 +++++++++++++
 3 files changed, 69 insertions(+), 1 deletion(-)
drivers/iommu/intel/pasid.h +2 -0
index ab4408c824a5..583ea67fc783 100644
@@ -123,4 +123,6 @@ void intel_pasid_tear_down_entry(struct intel_iommu *iommu,
 				 bool fault_ignore);
 int vcmd_alloc_pasid(struct intel_iommu *iommu, u32 *pasid);
 void vcmd_free_pasid(struct intel_iommu *iommu, u32 pasid);
+void intel_pasid_setup_page_snoop_control(struct intel_iommu *iommu,
+					  struct device *dev, u32 pasid);
 #endif /* __INTEL_PASID_H */
drivers/iommu/intel/iommu.c +49 -1

62 lines not on this page · raw message

drivers/iommu/intel/pasid.c +18 -0
index f8d215d85695..815c744e6a34 100644
@@ -762,3 +762,21 @@ int intel_pasid_setup_pass_through(struct intel_iommu *iommu,
 
 	return 0;
 }
+
+/*
+ * Set the page snoop control for a pasid entry which has been set up.
+ */
+void intel_pasid_setup_page_snoop_control(struct intel_iommu *iommu,
+					  struct device *dev, u32 pasid)
+{
+	struct pasid_entry *pte;
+	u16 did;
+
+	pte = intel_pasid_get_entry(dev, pasid);
+	if (WARN_ON(!pte || !pasid_pte_is_present(pte)))
+		return;
+
+	pasid_set_pgsnp(pte);
+	did = pasid_get_domain_id(pte);
+	pasid_flush_caches(iommu, pte, pasid, did);
+}
Signature · 6 lines
2.25.1

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

PATCH 4/5 iommu/vt-d: Remove domain_update_iommu_snooping()

In reply to Lu Baolu

The IOMMU force snooping capability is not required to be consistent among all the IOMMUs anymore. Remove force snooping capability check in the IOMMU hot-add path and domain_update_iommu_snooping() becomes a dead code now.

---
 drivers/iommu/intel/iommu.c | 34 +---------------------------------
 1 file changed, 1 insertion(+), 33 deletions(-)
drivers/iommu/intel/iommu.c +1 -33
index 3c1c228f9031..d5808495eb64 100644
@@ -533,33 +533,6 @@ static void domain_update_iommu_coherency(struct dmar_domain *domain)
 	rcu_read_unlock();
 }
 
-static bool domain_update_iommu_snooping(struct intel_iommu *skip)
-{
-	struct dmar_drhd_unit *drhd;
-	struct intel_iommu *iommu;
-	bool ret = true;
-
-	rcu_read_lock();
-	for_each_active_iommu(iommu, drhd) {
-		if (iommu != skip) {
-			/*
-			 * If the hardware is operating in the scalable mode,
-			 * the snooping control is always supported since we
-			 * always set PASID-table-entry.PGSNP bit if the domain
-			 * is managed outside (UNMANAGED).
-			 */
-			if (!sm_supported(iommu) &&
-			    !ecap_sc_support(iommu->ecap)) {
-				ret = false;
-				break;
-			}
-		}
-	}
-	rcu_read_unlock();
-
-	return ret;
-}
-
 static int domain_update_iommu_superpage(struct dmar_domain *domain,
 					 struct intel_iommu *skip)
 {
@@ -3593,12 +3566,7 @@ static int intel_iommu_add(struct dmar_drhd_unit *dmaru)
 			iommu->name);
 		return -ENXIO;
 	}
-	if (!ecap_sc_support(iommu->ecap) &&
-	    domain_update_iommu_snooping(iommu)) {
-		pr_warn("%s: Doesn't support snooping.\n",
-			iommu->name);
-		return -ENXIO;
-	}
+
 	sp = domain_update_iommu_superpage(NULL, iommu) - 1;
 	if (sp >= 0 && !(cap_super_page_val(iommu->cap) & (1 << sp))) {
 		pr_warn("%s: Doesn't support large page.\n",
Signature · 6 lines
2.25.1

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

PATCH 5/5 iommu/vt-d: Remove hard coding PGSNP bit in PASID entries

In reply to Lu Baolu

As enforce_cache_coherency has been introduced into the iommu_domain_ops, the kernel component which owns the iommu domain is able to opt-in its requirement for force snooping support. The iommu driver has no need to hard code the page snoop control bit in the PASID table entries anymore.

---
 drivers/iommu/intel/pasid.h | 1 -
 drivers/iommu/intel/iommu.c | 3 ---
 drivers/iommu/intel/pasid.c | 6 ------
 3 files changed, 10 deletions(-)
drivers/iommu/intel/pasid.h +0 -1
index 583ea67fc783..394e6284ce1f 100644
@@ -48,7 +48,6 @@
  */
 #define PASID_FLAG_SUPERVISOR_MODE	BIT(0)
 #define PASID_FLAG_NESTED		BIT(1)
-#define PASID_FLAG_PAGE_SNOOP		BIT(2)
 
 /*
  * The PASID_FLAG_FL5LP flag Indicates using 5-level paging for first-
drivers/iommu/intel/iommu.c +0 -3
index d5808495eb64..edd3d940eb25 100644
@@ -2432,9 +2432,6 @@ static int domain_setup_first_level(struct intel_iommu *iommu,
 	if (level == 5)
 		flags |= PASID_FLAG_FL5LP;
 
-	if (domain->domain.type == IOMMU_DOMAIN_UNMANAGED)
-		flags |= PASID_FLAG_PAGE_SNOOP;
-
 	return intel_pasid_setup_first_level(iommu, dev, (pgd_t *)pgd, pasid,
 					     domain->iommu_did[iommu->seq_id],
 					     flags);
drivers/iommu/intel/pasid.c +0 -6
index 815c744e6a34..dc5020320323 100644
@@ -625,9 +625,6 @@ int intel_pasid_setup_first_level(struct intel_iommu *iommu,
 		}
 	}
 
-	if (flags & PASID_FLAG_PAGE_SNOOP)
-		pasid_set_pgsnp(pte);
-
 	pasid_set_domain_id(pte, did);
 	pasid_set_address_width(pte, iommu->agaw);
 	pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
@@ -710,9 +707,6 @@ int intel_pasid_setup_second_level(struct intel_iommu *iommu,
 	pasid_set_fault_enable(pte);
 	pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap));
 
-	if (domain->domain.type == IOMMU_DOMAIN_UNMANAGED)
-		pasid_set_pgsnp(pte);
-
 	/*
 	 * Since it is a second level only translation setup, we should
 	 * set SRE bit as well (addresses are expected to be GPAs).
Signature · 6 lines
2.25.1

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

Re: [PATCH 1/5] iommu/vt-d: Block force-snoop domain attaching if no SC support

In reply to Lu Baolu
Quoted 9 lines · Lu Baolu

In the attach_dev callback of the default domain ops, if the domain has been set force_snooping, but the iommu hardware of the device does not support SC(Snoop Control) capability, the callback should block it and return a corresponding error code.

---
 drivers/iommu/intel/iommu.c | 3 +++
 1 file changed, 3 insertions(+)

Jason

#4 lore raw

Re: [PATCH 2/5] iommu/vt-d: Set SNP bit only in second-level page table entries

In reply to Lu Baolu
Quoted 4 lines · Lu Baolu

The SNP bit is only valid for second-level PTEs. Setting this bit in the first-level PTEs has no functional impact because the Intel IOMMU always ignores the same bit in first-level PTEs. Anyway, let's check the page table type before setting SNP bit in PTEs to make the code more readable.

Shouldn't this be tested before setting force_snooping and not during every map?

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

#9 lore raw

Re: [PATCH 3/5] iommu/vt-d: Check domain force_snooping against attached devices

In reply to Lu Baolu

drivers/iommu/intel/iommu.c static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain, · line 4565

Quoted 9 lines · Lu Baolu
drivers/iommu/intel/iommu.c +9 -0
+static bool domain_support_force_snooping(struct dmar_domain *domain)
+{
+	struct device_domain_info *info;
+	unsigned long flags;
+	bool support = true;
+
+	spin_lock_irqsave(&device_domain_lock, flags);
+	if (list_empty(&domain->devices))
+		goto out;

Why? list_for_each_entry will just do nothing..

Quoted 32 lines
drivers/iommu/intel/iommu.c +32 -0
+	list_for_each_entry(info, &domain->devices, link) {
+		if (!ecap_sc_support(info->iommu->ecap)) {
+			support = false;
+			break;
+		}
+	}
+out:
+	spin_unlock_irqrestore(&device_domain_lock, flags);
+	return support;
+}
+
+static void domain_set_force_snooping(struct dmar_domain *domain)
+{
+	struct device_domain_info *info;
+	unsigned long flags;
+
+	/*
+	 * Second level page table supports per-PTE snoop control. The
+	 * iommu_map() interface will handle this by setting SNP bit.
+	 */
+	if (!domain_use_first_level(domain))
+		return;
+
+	spin_lock_irqsave(&device_domain_lock, flags);
+	if (list_empty(&domain->devices))
+		goto out_unlock;
+
+	list_for_each_entry(info, &domain->devices, link)
+		intel_pasid_setup_page_snoop_control(info->iommu, info->dev,
+						     PASID_RID2PASID);
+
+out_unlock:

drivers/iommu/intel/iommu.c static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain, · line 4607

Quoted 10 lines
drivers/iommu/intel/iommu.c +4 -1
+	spin_unlock_irqrestore(&device_domain_lock, flags);
+}
+
 static bool intel_iommu_enforce_cache_coherency(struct iommu_domain *domain)
 {
 	struct dmar_domain *dmar_domain = to_dmar_domain(domain);
 
-	if (!domain_update_iommu_snooping(NULL))
+	if (!domain_support_force_snooping(dmar_domain))
 		return false;

Maybe exit early if force_snooping = true?

Quoted 10 lines
drivers/iommu/intel/iommu.c +2 -0
+	domain_set_force_snooping(dmar_domain);
 	dmar_domain->force_snooping = true;
+
 	return true;
 }
drivers/iommu/intel/pasid.c +1 -0
index f8d215d85695..815c744e6a34 100644
+++ b/drivers/iommu/intel/pasid.c

drivers/iommu/intel/pasid.c int intel_pasid_setup_pass_through(struct intel_iommu *iommu, · line 768

Quoted 7 lines
drivers/iommu/intel/pasid.c +4 -0
@@ -762,3 +762,21 @@ int intel_pasid_setup_pass_through(struct intel_iommu *iommu,
 
 	return 0;
 }
+
+/*
+ * Set the page snoop control for a pasid entry which has been set up.
+ */

So the 'first level' is only used with pasid?

drivers/iommu/intel/pasid.c int intel_pasid_setup_pass_through(struct intel_iommu *iommu, · line 779

Quoted 11 lines
drivers/iommu/intel/pasid.c +11 -0
+void intel_pasid_setup_page_snoop_control(struct intel_iommu *iommu,
+					  struct device *dev, u32 pasid)
+{
+	struct pasid_entry *pte;
+	u16 did;
+
+	pte = intel_pasid_get_entry(dev, pasid);
+	if (WARN_ON(!pte || !pasid_pte_is_present(pte)))
+		return;
+
+	pasid_set_pgsnp(pte);

Doesn't this need to be done in other places too, like when a new attach is made? Patch 5 removed it, but should that be made if domain->force_snooping?

Jason

Reviewed-by on v1 4/5
#14 lore raw

Re: [PATCH 4/5] iommu/vt-d: Remove domain_update_iommu_snooping()

In reply to Lu Baolu
Quoted 9 lines · Lu Baolu

The IOMMU force snooping capability is not required to be consistent among all the IOMMUs anymore. Remove force snooping capability check in the IOMMU hot-add path and domain_update_iommu_snooping() becomes a dead code now.

---
 drivers/iommu/intel/iommu.c | 34 +---------------------------------
 1 file changed, 1 insertion(+), 33 deletions(-)

Jason

#18 lore raw

Re: [PATCH 5/5] iommu/vt-d: Remove hard coding PGSNP bit in PASID entries

In reply to Lu Baolu
Quoted 11 lines · Lu Baolu

As enforce_cache_coherency has been introduced into the iommu_domain_ops, the kernel component which owns the iommu domain is able to opt-in its requirement for force snooping support. The iommu driver has no need to hard code the page snoop control bit in the PASID table entries anymore.

---
 drivers/iommu/intel/pasid.h | 1 -
 drivers/iommu/intel/iommu.c | 3 ---
 drivers/iommu/intel/pasid.c | 6 ------
 3 files changed, 10 deletions(-)

It seems fine, but as in the other email where do we do pasid_set_pgsnp() for a new device attach on an already no-snopp domain?

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

#11 lore raw

Re: [PATCH 3/5] iommu/vt-d: Check domain force_snooping against attached devices

In reply to Lu Baolu

Hi BaoLu,

Quoted 39 lines · Lu Baolu

As domain->force_snooping only impacts the devices attached with the domain, there's no need to check against all IOMMU units. At the same time, for a brand new domain (hasn't been attached to any device), the force_snooping field could be set, but the attach_dev callback will return failure if it wants to attach to a device which IOMMU has no snoop control capability.

---
 drivers/iommu/intel/pasid.h |  2 ++
 drivers/iommu/intel/iommu.c | 50 ++++++++++++++++++++++++++++++++++++-
 drivers/iommu/intel/pasid.c | 18 +++++++++++++
 3 files changed, 69 insertions(+), 1 deletion(-)
drivers/iommu/intel/pasid.h +2 -0
index ab4408c824a5..583ea67fc783 100644
@@ -123,4 +123,6 @@ void intel_pasid_tear_down_entry(struct intel_iommu
 *iommu, bool fault_ignore);
 int vcmd_alloc_pasid(struct intel_iommu *iommu, u32 *pasid);
 void vcmd_free_pasid(struct intel_iommu *iommu, u32 pasid);
+void intel_pasid_setup_page_snoop_control(struct intel_iommu *iommu,
+					  struct device *dev, u32 pasid);
 #endif /* __INTEL_PASID_H */
drivers/iommu/intel/iommu.c +6 -0
index 98050943d863..3c1c228f9031 100644
@@ -4554,13 +4554,61 @@ static phys_addr_t
 intel_iommu_iova_to_phys(struct iommu_domain *domain, return phys;
 }
 
+static bool domain_support_force_snooping(struct dmar_domain *domain)
+{
+	struct device_domain_info *info;
+	unsigned long flags;
+	bool support = true;
+

drivers/iommu/intel/iommu.c static phys_addr_t · line 4572

Quoted 10 lines
drivers/iommu/intel/iommu.c +10 -0
+	spin_lock_irqsave(&device_domain_lock, flags);
+	if (list_empty(&domain->devices))
+		goto out;
+
+	list_for_each_entry(info, &domain->devices, link) {
+		if (!ecap_sc_support(info->iommu->ecap)) {
+			support = false;
+			break;
+		}
+	}

why not just check the flag dmar_domain->force_snooping? devices wouldn't be able to attach if !ecap_sc, right?

Quoted 16 lines
drivers/iommu/intel/iommu.c +16 -0
+out:
+	spin_unlock_irqrestore(&device_domain_lock, flags);
+	return support;
+}
+
+static void domain_set_force_snooping(struct dmar_domain *domain)
+{
+	struct device_domain_info *info;
+	unsigned long flags;
+
+	/*
+	 * Second level page table supports per-PTE snoop control. The
+	 * iommu_map() interface will handle this by setting SNP bit.
+	 */
+	if (!domain_use_first_level(domain))
+		return;

drivers/iommu/intel/iommu.c static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain, · line 4597

Quoted 10 lines
drivers/iommu/intel/iommu.c +9 -0
+
+	spin_lock_irqsave(&device_domain_lock, flags);
+	if (list_empty(&domain->devices))
+		goto out_unlock;
+
+	list_for_each_entry(info, &domain->devices, link)
+		intel_pasid_setup_page_snoop_control(info->iommu,
 info->dev,
+						     PASID_RID2PASID);
+

I guess other DMA API PASIDs need to have sc bit set as well. I will keep this in mind for my DMA API PASID patch.

Quoted 5 lines
drivers/iommu/intel/iommu.c +4 -0
+out_unlock:
+	spin_unlock_irqrestore(&device_domain_lock, flags);
+}
+
 static bool intel_iommu_enforce_cache_coherency(struct iommu_domain

drivers/iommu/intel/iommu.c static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain, · line 4611

Quoted 10 lines
drivers/iommu/intel/iommu.c +4 -1
 *domain) {
 	struct dmar_domain *dmar_domain = to_dmar_domain(domain);
 
-	if (!domain_update_iommu_snooping(NULL))
+	if (!domain_support_force_snooping(dmar_domain))
 		return false;
+
+	domain_set_force_snooping(dmar_domain);
 	dmar_domain->force_snooping = true;
+

nit: spurious change

Quoted 19 lines
drivers/iommu/intel/iommu.c +0 -0
 	return true;
 }
drivers/iommu/intel/pasid.c +8 -0
index f8d215d85695..815c744e6a34 100644
@@ -762,3 +762,21 @@ int intel_pasid_setup_pass_through(struct
 intel_iommu *iommu, 
 	return 0;
 }
+
+/*
+ * Set the page snoop control for a pasid entry which has been set up.
+ */
+void intel_pasid_setup_page_snoop_control(struct intel_iommu *iommu,
+					  struct device *dev, u32 pasid)
+{
+	struct pasid_entry *pte;

drivers/iommu/intel/pasid.c int intel_pasid_setup_pass_through(struct · line 782

Quoted 10 lines
drivers/iommu/intel/pasid.c +10 -0
+	u16 did;
+
+	pte = intel_pasid_get_entry(dev, pasid);
+	if (WARN_ON(!pte || !pasid_pte_is_present(pte)))
+		return;
+
+	pasid_set_pgsnp(pte);
+	did = pasid_get_domain_id(pte);
+	pasid_flush_caches(iommu, pte, pasid, did);
+}

Thanks,

Jacob

#15 lore raw

Re: [PATCH 4/5] iommu/vt-d: Remove domain_update_iommu_snooping()

In reply to Lu Baolu

Hi BaoLu,

Quoted 52 lines · Lu Baolu

The IOMMU force snooping capability is not required to be consistent among all the IOMMUs anymore. Remove force snooping capability check in the IOMMU hot-add path and domain_update_iommu_snooping() becomes a dead code now.

---
 drivers/iommu/intel/iommu.c | 34 +---------------------------------
 1 file changed, 1 insertion(+), 33 deletions(-)
drivers/iommu/intel/iommu.c +0 -12
index 3c1c228f9031..d5808495eb64 100644
@@ -533,33 +533,6 @@ static void domain_update_iommu_coherency(struct
 dmar_domain *domain) rcu_read_unlock();
 }
 
-static bool domain_update_iommu_snooping(struct intel_iommu *skip)
-{
-	struct dmar_drhd_unit *drhd;
-	struct intel_iommu *iommu;
-	bool ret = true;
-
-	rcu_read_lock();
-	for_each_active_iommu(iommu, drhd) {
-		if (iommu != skip) {
-			/*
-			 * If the hardware is operating in the scalable
 mode,
-			 * the snooping control is always supported

since we

drivers/iommu/intel/iommu.c +0 -1
-			 * always set PASID-table-entry.PGSNP bit if the

domain

drivers/iommu/intel/iommu.c +0 -14
-			 * is managed outside (UNMANAGED).
-			 */
-			if (!sm_supported(iommu) &&
-			    !ecap_sc_support(iommu->ecap)) {
-				ret = false;
-				break;
-			}
-		}
-	}
-	rcu_read_unlock();
-
-	return ret;
-}
-
 static int domain_update_iommu_superpage(struct dmar_domain *domain,
 					 struct intel_iommu *skip)
 {

drivers/iommu/intel/iommu.c static int intel_iommu_add(struct dmar_drhd_unit · line 3569

Quoted 10 lines
drivers/iommu/intel/iommu.c +1 -6
@@ -3593,12 +3566,7 @@ static int intel_iommu_add(struct dmar_drhd_unit
 *dmaru) iommu->name);
 		return -ENXIO;
 	}
-	if (!ecap_sc_support(iommu->ecap) &&
-	    domain_update_iommu_snooping(iommu)) {
-		pr_warn("%s: Doesn't support snooping.\n",
-			iommu->name);
-		return -ENXIO;
-	}
+

Maybe I missed earlier patches, so this bit can also be deleted?

struct dmar_domain {
u8 iommu_snooping: 1; /* indicate snooping control
feature */

drivers/iommu/intel/iommu.c static int intel_iommu_add(struct dmar_drhd_unit *dmaru) · line 3572

Quoted 3 lines
drivers/iommu/intel/iommu.c +0 -0
 	sp = domain_update_iommu_superpage(NULL, iommu) - 1;
 	if (sp >= 0 && !(cap_super_page_val(iommu->cap) & (1 << sp))) {
 		pr_warn("%s: Doesn't support large page.\n",

Thanks,

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

author maintainer
#5 lore raw

Re: [PATCH 2/5] iommu/vt-d: Set SNP bit only in second-level page table entries

In reply to Jason Gunthorpe

Hi Jason,

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

The SNP bit is only valid for second-level PTEs. Setting this bit in the first-level PTEs has no functional impact because the Intel IOMMU always ignores the same bit in first-level PTEs. Anyway, let's check the page table type before setting SNP bit in PTEs to make the code more readable.

Shouldn't this be tested before setting force_snooping and not during every map?

The check is in the following patch. This just makes sure that SNP is only set in second-level page table entries.

Best regards,
baolu

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

author maintainer
#10 lore raw

Re: [PATCH 3/5] iommu/vt-d: Check domain force_snooping against attached devices

In reply to Jason Gunthorpe
Quoted 12 lines · Jason Gunthorpe

drivers/iommu/intel/iommu.c static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain, · line 4565

Quoted 9 lines · Lu Baolu
drivers/iommu/intel/iommu.c +9 -0
+static bool domain_support_force_snooping(struct dmar_domain *domain)
+{
+	struct device_domain_info *info;
+	unsigned long flags;
+	bool support = true;
+
+	spin_lock_irqsave(&device_domain_lock, flags);
+	if (list_empty(&domain->devices))
+		goto out;

Why? list_for_each_entry will just do nothing..

Yes. I will remove above two lines.

Quoted 40 lines
Quoted 40 lines
drivers/iommu/intel/iommu.c +35 -0
+	list_for_each_entry(info, &domain->devices, link) {
+		if (!ecap_sc_support(info->iommu->ecap)) {
+			support = false;
+			break;
+		}
+	}
+out:
+	spin_unlock_irqrestore(&device_domain_lock, flags);
+	return support;
+}
+
+static void domain_set_force_snooping(struct dmar_domain *domain)
+{
+	struct device_domain_info *info;
+	unsigned long flags;
+
+	/*
+	 * Second level page table supports per-PTE snoop control. The
+	 * iommu_map() interface will handle this by setting SNP bit.
+	 */
+	if (!domain_use_first_level(domain))
+		return;
+
+	spin_lock_irqsave(&device_domain_lock, flags);
+	if (list_empty(&domain->devices))
+		goto out_unlock;
+
+	list_for_each_entry(info, &domain->devices, link)
+		intel_pasid_setup_page_snoop_control(info->iommu, info->dev,
+						     PASID_RID2PASID);
+
+out_unlock:
+	spin_unlock_irqrestore(&device_domain_lock, flags);
+}
+
  static bool intel_iommu_enforce_cache_coherency(struct iommu_domain *domain)
  {
  	struct dmar_domain *dmar_domain = to_dmar_domain(domain);
Quoted 5 lines

drivers/iommu/intel/iommu.c static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain, · line 4607

Quoted 3 lines
drivers/iommu/intel/iommu.c +1 -1
-	if (!domain_update_iommu_snooping(NULL))
+	if (!domain_support_force_snooping(dmar_domain))
  		return false;

Maybe exit early if force_snooping = true?

Yes, should check "force_snooping = true" and return directly if force_snooping has already been set. As you pointed below, the new domain_attach should take care of this flag as well. Thanks!

Quoted 12 lines
Quoted 12 lines
drivers/iommu/intel/iommu.c +2 -0
+	domain_set_force_snooping(dmar_domain);
  	dmar_domain->force_snooping = true;
+
  	return true;
  }
drivers/iommu/intel/pasid.c +1 -0
index f8d215d85695..815c744e6a34 100644
+++ b/drivers/iommu/intel/pasid.c
@@ -762,3 +762,21 @@ int intel_pasid_setup_pass_through(struct intel_iommu *iommu,
 
Quoted 8 lines

drivers/iommu/intel/pasid.c int intel_pasid_setup_pass_through(struct intel_iommu *iommu, · line 768

Quoted 6 lines
drivers/iommu/intel/pasid.c +4 -0
  	return 0;
  }
+
+/*
+ * Set the page snoop control for a pasid entry which has been set up.
+ */

So the 'first level' is only used with pasid?

Yes. A fake pasid (RID2PASID in spec) is used for legacy transactions (those w/o pasid).

Quoted 16 lines

drivers/iommu/intel/pasid.c int intel_pasid_setup_pass_through(struct intel_iommu *iommu, · line 779

Quoted 12 lines
drivers/iommu/intel/pasid.c +11 -0
+void intel_pasid_setup_page_snoop_control(struct intel_iommu *iommu,
+					  struct device *dev, u32 pasid)
+{
+	struct pasid_entry *pte;
+	u16 did;
+
+	pte = intel_pasid_get_entry(dev, pasid);
+	if (WARN_ON(!pte || !pasid_pte_is_present(pte)))
+		return;
+
+	pasid_set_pgsnp(pte);

Doesn't this need to be done in other places too, like when a new attach is made? Patch 5 removed it, but should that be made if domain->force_snooping?

Yes. I missed this. Will take care of this in the next version.

Quoted 2 lines

Jason

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

author maintainer
#12 lore raw

Re: [PATCH 3/5] iommu/vt-d: Check domain force_snooping against attached devices

In reply to Jacob Pan
Quoted 1 line · Jacob Pan

Hi BaoLu,

Hi Jacob,

Quoted 47 lines
Quoted 43 lines · Lu Baolu

As domain->force_snooping only impacts the devices attached with the domain, there's no need to check against all IOMMU units. At the same time, for a brand new domain (hasn't been attached to any device), the force_snooping field could be set, but the attach_dev callback will return failure if it wants to attach to a device which IOMMU has no snoop control capability.

---
  drivers/iommu/intel/pasid.h |  2 ++
  drivers/iommu/intel/iommu.c | 50 ++++++++++++++++++++++++++++++++++++-
  drivers/iommu/intel/pasid.c | 18 +++++++++++++
  3 files changed, 69 insertions(+), 1 deletion(-)
drivers/iommu/intel/pasid.h +2 -0
index ab4408c824a5..583ea67fc783 100644
@@ -123,4 +123,6 @@ void intel_pasid_tear_down_entry(struct intel_iommu
 *iommu, bool fault_ignore);
  int vcmd_alloc_pasid(struct intel_iommu *iommu, u32 *pasid);
  void vcmd_free_pasid(struct intel_iommu *iommu, u32 pasid);
+void intel_pasid_setup_page_snoop_control(struct intel_iommu *iommu,
+					  struct device *dev, u32 pasid);
  #endif /* __INTEL_PASID_H */
drivers/iommu/intel/iommu.c +10 -0
index 98050943d863..3c1c228f9031 100644
@@ -4554,13 +4554,61 @@ static phys_addr_t
 intel_iommu_iova_to_phys(struct iommu_domain *domain, return phys;
  }
 
+static bool domain_support_force_snooping(struct dmar_domain *domain)
+{
+	struct device_domain_info *info;
+	unsigned long flags;
+	bool support = true;
+
+	spin_lock_irqsave(&device_domain_lock, flags);
+	if (list_empty(&domain->devices))
+		goto out;
+
Quoted 8 lines

drivers/iommu/intel/iommu.c static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain, · line 4572

Quoted 6 lines
drivers/iommu/intel/iommu.c +6 -0
+	list_for_each_entry(info, &domain->devices, link) {
+		if (!ecap_sc_support(info->iommu->ecap)) {
+			support = false;
+			break;
+		}
+	}

why not just check the flag dmar_domain->force_snooping? devices wouldn't be able to attach if !ecap_sc, right?

I should check "dmar_domain->force_snooping" first. If this is the first time that this flag is about to set, then check the capabilities.

Quoted 21 lines
Quoted 21 lines
drivers/iommu/intel/iommu.c +20 -0
+out:
+	spin_unlock_irqrestore(&device_domain_lock, flags);
+	return support;
+}
+
+static void domain_set_force_snooping(struct dmar_domain *domain)
+{
+	struct device_domain_info *info;
+	unsigned long flags;
+
+	/*
+	 * Second level page table supports per-PTE snoop control. The
+	 * iommu_map() interface will handle this by setting SNP bit.
+	 */
+	if (!domain_use_first_level(domain))
+		return;
+
+	spin_lock_irqsave(&device_domain_lock, flags);
+	if (list_empty(&domain->devices))
+		goto out_unlock;
Quoted 8 lines

drivers/iommu/intel/iommu.c static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain, · line 4597

Quoted 6 lines
drivers/iommu/intel/iommu.c +5 -0
+
+	list_for_each_entry(info, &domain->devices, link)
+		intel_pasid_setup_page_snoop_control(info->iommu,
 info->dev,
+						     PASID_RID2PASID);
+

I guess other DMA API PASIDs need to have sc bit set as well. I will keep this in mind for my DMA API PASID patch.

Kernel DMA don't need to set the PGSNP bit. The x86 arch is always DMA coherent. The force snooping is only needed when the device is controlled by user space, but the VMM is optimized not to support the virtualization of the wbinv instruction.

Quoted 9 lines
Quoted 9 lines
drivers/iommu/intel/iommu.c +4 -0
+out_unlock:
+	spin_unlock_irqrestore(&device_domain_lock, flags);
+}
+
  static bool intel_iommu_enforce_cache_coherency(struct iommu_domain
 *domain) {
  	struct dmar_domain *dmar_domain = to_dmar_domain(domain);
 
Quoted 8 lines

drivers/iommu/intel/iommu.c static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain, · line 4611

Quoted 7 lines
drivers/iommu/intel/iommu.c +4 -1
-	if (!domain_update_iommu_snooping(NULL))
+	if (!domain_support_force_snooping(dmar_domain))
  		return false;
+
+	domain_set_force_snooping(dmar_domain);
  	dmar_domain->force_snooping = true;
+

nit: spurious change

I expect a blank line before return in the end.

Quoted 33 lines
Quoted 19 lines
drivers/iommu/intel/iommu.c +0 -0
  	return true;
  }
drivers/iommu/intel/pasid.c +8 -0
index f8d215d85695..815c744e6a34 100644
@@ -762,3 +762,21 @@ int intel_pasid_setup_pass_through(struct
 intel_iommu *iommu,
  	return 0;
  }
+
+/*
+ * Set the page snoop control for a pasid entry which has been set up.
+ */
+void intel_pasid_setup_page_snoop_control(struct intel_iommu *iommu,
+					  struct device *dev, u32 pasid)
+{
+	struct pasid_entry *pte;

drivers/iommu/intel/pasid.c int intel_pasid_setup_pass_through(struct · line 782

Quoted 10 lines
drivers/iommu/intel/pasid.c +10 -0
+	u16 did;
+
+	pte = intel_pasid_get_entry(dev, pasid);
+	if (WARN_ON(!pte || !pasid_pte_is_present(pte)))
+		return;
+
+	pasid_set_pgsnp(pte);
+	did = pasid_get_domain_id(pte);
+	pasid_flush_caches(iommu, pte, pasid, did);
+}

Thanks,

Quoted 1 line

Jacob

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

author maintainer
#16 lore raw

Re: [PATCH 4/5] iommu/vt-d: Remove domain_update_iommu_snooping()

In reply to Jacob Pan
Quoted 69 lines · Jacob Pan

Hi BaoLu,

Quoted 52 lines · Lu Baolu

The IOMMU force snooping capability is not required to be consistent among all the IOMMUs anymore. Remove force snooping capability check in the IOMMU hot-add path and domain_update_iommu_snooping() becomes a dead code now.

---
  drivers/iommu/intel/iommu.c | 34 +---------------------------------
  1 file changed, 1 insertion(+), 33 deletions(-)
drivers/iommu/intel/iommu.c +0 -12
index 3c1c228f9031..d5808495eb64 100644
@@ -533,33 +533,6 @@ static void domain_update_iommu_coherency(struct
 dmar_domain *domain) rcu_read_unlock();
  }
 
-static bool domain_update_iommu_snooping(struct intel_iommu *skip)
-{
-	struct dmar_drhd_unit *drhd;
-	struct intel_iommu *iommu;
-	bool ret = true;
-
-	rcu_read_lock();
-	for_each_active_iommu(iommu, drhd) {
-		if (iommu != skip) {
-			/*
-			 * If the hardware is operating in the scalable
 mode,
-			 * the snooping control is always supported

since we

drivers/iommu/intel/iommu.c +0 -1
-			 * always set PASID-table-entry.PGSNP bit if the

domain

drivers/iommu/intel/iommu.c +0 -14
-			 * is managed outside (UNMANAGED).
-			 */
-			if (!sm_supported(iommu) &&
-			    !ecap_sc_support(iommu->ecap)) {
-				ret = false;
-				break;
-			}
-		}
-	}
-	rcu_read_unlock();
-
-	return ret;
-}
-
  static int domain_update_iommu_superpage(struct dmar_domain *domain,
  					 struct intel_iommu *skip)
  {

drivers/iommu/intel/iommu.c static int intel_iommu_add(struct dmar_drhd_unit · line 3569

Quoted 10 lines
drivers/iommu/intel/iommu.c +1 -6
@@ -3593,12 +3566,7 @@ static int intel_iommu_add(struct dmar_drhd_unit
 *dmaru) iommu->name);
  		return -ENXIO;
  	}
-	if (!ecap_sc_support(iommu->ecap) &&
-	    domain_update_iommu_snooping(iommu)) {
-		pr_warn("%s: Doesn't support snooping.\n",
-			iommu->name);
-		return -ENXIO;
-	}
+

Maybe I missed earlier patches, so this bit can also be deleted?

Quoted 3 lines

struct dmar_domain {
u8 iommu_snooping: 1; /* indicate snooping control
feature */

It has been cleaned up by below commit:

71cfafda9c9b vfio: Move the Intel no-snoop control off of IOMMU_CACHE

Quoted 9 lines

drivers/iommu/intel/iommu.c static int intel_iommu_add(struct dmar_drhd_unit *dmaru) · line 3572

Quoted 4 lines
drivers/iommu/intel/iommu.c +0 -0
  	sp = domain_update_iommu_superpage(NULL, iommu) - 1;
  	if (sp >= 0 && !(cap_super_page_val(iommu->cap) & (1 << sp))) {
  		pr_warn("%s: Doesn't support large page.\n",

Thanks,

Jacob

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

author maintainer
#19 lore raw

Re: [PATCH 5/5] iommu/vt-d: Remove hard coding PGSNP bit in PASID entries

In reply to Jason Gunthorpe
Quoted 13 lines · Jason Gunthorpe
Quoted 11 lines · Lu Baolu

As enforce_cache_coherency has been introduced into the iommu_domain_ops, the kernel component which owns the iommu domain is able to opt-in its requirement for force snooping support. The iommu driver has no need to hard code the page snoop control bit in the PASID table entries anymore.

---
  drivers/iommu/intel/pasid.h | 1 -
  drivers/iommu/intel/iommu.c | 3 ---
  drivers/iommu/intel/pasid.c | 6 ------
  3 files changed, 10 deletions(-)
Quoted 2 lines

It seems fine, but as in the other email where do we do pasid_set_pgsnp() for a new device attach on an already no-snopp domain?

Yes. I will take care of this in the next version.

Quoted 1 line

Jason

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

#6 lore raw

Re: [PATCH 2/5] iommu/vt-d: Set SNP bit only in second-level page table entries

In reply to Baolu Lu
Quoted 8 lines · Baolu Lu

Hi Jason,

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

The SNP bit is only valid for second-level PTEs. Setting this bit in the first-level PTEs has no functional impact because the Intel IOMMU always ignores the same bit in first-level PTEs. Anyway, let's check the page table type before setting SNP bit in PTEs to make the code more readable.

Quoted 5 lines
Quoted 2 lines

Shouldn't this be tested before setting force_snooping and not during every map?

The check is in the following patch. This just makes sure that SNP is only set in second-level page table entries.

I think you should add a 2nd flag to indicate 'set SNP bit in PTEs' and take care of computing that flag in the enforce_no_snoop function

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

author maintainer
#7 lore raw

Re: [PATCH 2/5] iommu/vt-d: Set SNP bit only in second-level page table entries

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

Hi Jason,

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

The SNP bit is only valid for second-level PTEs. Setting this bit in the first-level PTEs has no functional impact because the Intel IOMMU always ignores the same bit in first-level PTEs. Anyway, let's check the page table type before setting SNP bit in PTEs to make the code more readable.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Shouldn't this be tested before setting force_snooping and not during every map?

The check is in the following patch. This just makes sure that SNP is only set in second-level page table entries.

I think you should add a 2nd flag to indicate 'set SNP bit in PTEs' and take care of computing that flag in the enforce_no_snoop function

Yours looks better. Will add it in the next version.

Quoted 1 line

Jason

Best regards,
baolu

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