Skip to contentSkip to conversation

iommu/vt-d: Remove superfluous IOMMU IOTLB invalidations

PATCH In review Tina Zhang 2 participants 15 messages Feb 19, 2024 – Feb 26, 2024

Subsystems (2)
lore
Share & tools
lore

Tina Zhang

author
#0 lore raw

v2 cover iommu/vt-d: Remove superfluous IOMMU IOTLB invalidations

In the current VT-d driver, IOMMU IOTLB invalidation commands and device-TLB commands are performed per device, which leads to superfluous IOTLB invalidations. For example, if there are four devices behind a IOMMU are attached to one sva domain (which could be a common case in virtualization scenarios where one virtual IOMMU working for all the virtual devices), four IOTLB invalidation commands and four device-IOTLB invalidation commands will be issued. However, only one IOTLB invalidation command and four device-IOTLB invalidation commands are necessary. Superfluous IOMMU IOTLB invalidations impact run-time performance.

The first 12 patches of this patch-set are for refactoring:
1) Retire struct intel_svm_set_dev_pasid and struct intel_svm_dev to avoid duplicating code for sva domain.
2) Reuse intel_iommu_set/remove_dev_pasid functions for both sva domain and dmar_domain.

The last patch removes the superfluous IOMMU IOTLB invalidations.

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

v2:
- Rebase on v6.8-rc4.
- Sort dev_pasids list to make the devices behine the same IOMMU stay together.
- Doesn't allocate iommu_domain_info for sva domain and add comments
to tell the reason.
- Make intel_iommu_set_dev_pasid() be compatible with sva domain.
v1: https://lore.kernel.org/linux-iommu/[email protected]/
RFC: https://lore.kernel.org/linux-iommu/[email protected]/

Jason Gunthorpe (1):
iommu: Add ops->domain_alloc_sva()

Tina Zhang (12):
iommu/vt-d: Retire the treatment for revoking PASIDs with pending
pgfaults
iommu/vt-d: Remove initialization for dynamically heap-allocated
rcu_head
iommu/vt-d: Refactor intel_svm_set_dev_pasid function iommu/vt-d: Retire struct intel_svm_dev
iommu/vt-d: Retire struct intel_svm
iommu/vt-d: Use RCU for dev_pasids list updates in
set/remove_dev_pasid()
iommu/vt-d: Add domain_type_is_sva helper function iommu/vt-d: Reuse intel_iommu_set_dev_pasid function iommu/vt-d: Use dev_pasid->sid field
iommu/vt-d: Retire intel_svm_remove_dev_pasid function iommu/vt-d: Sort dev_pasids list
iommu/vt-d: Remove superfluous IOMMU IOTLB invalidations

 drivers/iommu/intel/iommu.c |  75 ++++++---
 drivers/iommu/intel/iommu.h |  31 ++--
 drivers/iommu/intel/svm.c   | 300 +++++++++---------------------------
 drivers/iommu/iommu-sva.c   |   4 +-
 drivers/iommu/iommu.c       |  12 +-
 include/linux/iommu.h       |   3 +
 6 files changed, 151 insertions(+), 274 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 1/13 iommu/vt-d: Retire the treatment for revoking PASIDs with pending pgfaults

Revoking PASIDs with pending page faults has been achieved by clearing the pasid entry and draining the pending page faults in both hardware and software. The temporary treatment can be retired now.

---
 drivers/iommu/intel/svm.c | 7 -------
 1 file changed, 7 deletions(-)
drivers/iommu/intel/svm.c +0 -7
index 40edd282903f..a815362c8e60 100644
@@ -408,13 +408,6 @@ void intel_svm_remove_dev_pasid(struct device *dev, u32 pasid)
 			if (svm->notifier.ops)
 				mmu_notifier_unregister(&svm->notifier, mm);
 			pasid_private_remove(svm->pasid);
-			/*
-			 * We mandate that no page faults may be outstanding
-			 * for the PASID when intel_svm_unbind_mm() is called.
-			 * If that is not obeyed, subtle errors will happen.
-			 * Let's make them less subtle...
-			 */
-			memset(svm, 0x6b, sizeof(*svm));
 			kfree(svm);
 		}
 	}
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH v2 2/13 iommu/vt-d: Remove initialization for dynamically heap-allocated rcu_head

In reply to Tina Zhang

The rcu_head structures allocated dynamically in the heap don't need any initialization. Therefore, remove the init_rcu_head().

---
 drivers/iommu/intel/svm.c | 1 -
 1 file changed, 1 deletion(-)
drivers/iommu/intel/svm.c +0 -1
index a815362c8e60..a92a9e2239e2 100644
@@ -360,7 +360,6 @@ static int intel_svm_bind_mm(struct intel_iommu *iommu, struct device *dev,
 	sdev->iommu = iommu;
 	sdev->did = FLPT_DEFAULT_DID;
 	sdev->sid = PCI_DEVID(info->bus, info->devfn);
-	init_rcu_head(&sdev->rcu);
 	if (info->ats_enabled) {
 		sdev->qdep = info->ats_qdep;
 		if (sdev->qdep >= QI_DEV_EIOTLB_MAX_INVS)
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH v2 3/13 iommu/vt-d: Refactor intel_svm_set_dev_pasid function

In reply to Tina Zhang

Refactor intel_svm_set_dev_pasid() by moving the logic from intel_svm_bind_mm() into intel_svm_set_dev_pasid() and retire intel_svm_bind_mm(). No functional change intended.

The motivation is to enable intel_iommu_set_dev_pasid() to serve for both sva domain's and default domain's set_dev_pasid operations, instead of duplicating code for them.

---
 drivers/iommu/intel/svm.c | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)
drivers/iommu/intel/svm.c +3 -11
index a92a9e2239e2..1dd56d4eb88c 100644
@@ -315,10 +315,11 @@ static int pasid_to_svm_sdev(struct device *dev, unsigned int pasid,
 	return 0;
 }
 
-static int intel_svm_bind_mm(struct intel_iommu *iommu, struct device *dev,
-			     struct iommu_domain *domain, ioasid_t pasid)
+static int intel_svm_set_dev_pasid(struct iommu_domain *domain,
+				   struct device *dev, ioasid_t pasid)
 {
 	struct device_domain_info *info = dev_iommu_priv_get(dev);
+	struct intel_iommu *iommu = info->iommu;
 	struct mm_struct *mm = domain->mm;
 	struct intel_svm_dev *sdev;
 	struct intel_svm *svm;
@@ -796,15 +797,6 @@ int intel_svm_page_response(struct device *dev,
 	return ret;
 }
 
-static int intel_svm_set_dev_pasid(struct iommu_domain *domain,
-				   struct device *dev, ioasid_t pasid)
-{
-	struct device_domain_info *info = dev_iommu_priv_get(dev);
-	struct intel_iommu *iommu = info->iommu;
-
-	return intel_svm_bind_mm(iommu, dev, domain, pasid);
-}
-
 static void intel_svm_domain_free(struct iommu_domain *domain)
 {
 	kfree(to_dmar_domain(domain));
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH v2 4/13 iommu/vt-d: Retire struct intel_svm_dev

In reply to Tina Zhang

The struct dev_pasid_info is used by IOMMU domain to keep pasid info of attached device. For sva domain, there is another structure which keeps info of attached device, named intel_svm_dev. Instead of using two structs for the same purpose, the struct dev_pasid_info can work for both sva domain and IOMMU domain. As a result, struct intel_svm_dev gets retired.

The rcu/sid/qdep fields of struct intel_svm_dev are moved from struct intel_svm_dev into struct dev_pasid_info. Besides, the names of functions which are related to struct intel_svm_dev get changed to dev_pasid_info.

---
 drivers/iommu/intel/iommu.h |  11 +---
 drivers/iommu/intel/svm.c   | 107 ++++++++++++++++++------------------
 2 files changed, 55 insertions(+), 63 deletions(-)
drivers/iommu/intel/iommu.h +2 -9
index d02f916d8e59..de60c0ab3ade 100644
@@ -761,6 +761,8 @@ struct dev_pasid_info {
 #ifdef CONFIG_INTEL_IOMMU_DEBUGFS
 	struct dentry *debugfs_dentry; /* pointer to pasid directory dentry */
 #endif
+	struct rcu_head rcu;
+	u16 sid, qdep;
 };
 
 static inline void __iommu_flush_cache(
@@ -1085,15 +1087,6 @@ struct iommu_domain *intel_svm_domain_alloc(void);
 void intel_svm_remove_dev_pasid(struct device *dev, ioasid_t pasid);
 void intel_drain_pasid_prq(struct device *dev, u32 pasid);
 
-struct intel_svm_dev {
-	struct list_head list;
-	struct rcu_head rcu;
-	struct device *dev;
-	struct intel_iommu *iommu;
-	u16 did;
-	u16 sid, qdep;
-};
-
 struct intel_svm {
 	struct mmu_notifier notifier;
 	struct mm_struct *mm;
drivers/iommu/intel/svm.c +53 -54

238 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#5 lore raw

PATCH v2 5/13 iommu: Add ops->domain_alloc_sva()

In reply to Tina Zhang

From: Jason Gunthorpe <[email protected]>

Make a new op that receives the device and the mm_struct that the SVA domain should be created for. Unlike domain_alloc_paging() the dev argument is never NULL here.

This allows drivers to fully initialize the SVA domain and allocate the mmu_notifier during allocation. It allows the notifier lifetime to follow the lifetime of the iommu_domain.

Since we have only one call site, upgrade the new op to return ERR_PTR instead of NULL.

[Removed smmu3 related changes - Vasant]
---
 drivers/iommu/iommu-sva.c |  4 ++--
 drivers/iommu/iommu.c     | 12 +++++++++---
 include/linux/iommu.h     |  3 +++
 3 files changed, 14 insertions(+), 5 deletions(-)
drivers/iommu/iommu-sva.c +2 -2
index c3fc9201d0be..446ce5a0cae8 100644
@@ -99,8 +99,8 @@ struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm
 
 	/* Allocate a new domain and set it on device pasid. */
 	domain = iommu_sva_domain_alloc(dev, mm);
-	if (!domain) {
-		ret = -ENOMEM;
+	if (IS_ERR(domain)) {
+		ret = PTR_ERR(domain);
 		goto out_free_handle;
 	}
 
drivers/iommu/iommu.c +9 -3
index d14413916f93..f9a2b8363ad9 100644
@@ -3629,9 +3629,15 @@ struct iommu_domain *iommu_sva_domain_alloc(struct device *dev,
 	const struct iommu_ops *ops = dev_iommu_ops(dev);
 	struct iommu_domain *domain;
 
-	domain = ops->domain_alloc(IOMMU_DOMAIN_SVA);
-	if (!domain)
-		return NULL;
+	if (ops->domain_alloc_sva) {
+		domain = ops->domain_alloc_sva(dev, mm);
+		if (IS_ERR(domain))
+			return domain;
+	} else {
+		domain = ops->domain_alloc(IOMMU_DOMAIN_SVA);
+		if (!domain)
+			return ERR_PTR(-ENOMEM);
+	}
 
 	domain->type = IOMMU_DOMAIN_SVA;
 	mmgrab(mm);
include/linux/iommu.h +3 -0
index 1ea2a820e1eb..be5b51fc3af4 100644
@@ -419,6 +419,7 @@ static inline int __iommu_copy_struct_from_user_array(
  *                     Upon failure, ERR_PTR must be returned.
  * @domain_alloc_paging: Allocate an iommu_domain that can be used for
  *                       UNMANAGED, DMA, and DMA_FQ domain types.
+ * @domain_alloc_sva: Allocate an iommu_domain for Shared Virtual Addressing.
  * @probe_device: Add device to iommu driver handling
  * @release_device: Remove device from iommu driver handling
  * @probe_finalize: Do final setup work after the device is added to an IOMMU
@@ -459,6 +460,8 @@ struct iommu_ops {
 		struct device *dev, u32 flags, struct iommu_domain *parent,
 		const struct iommu_user_data *user_data);
 	struct iommu_domain *(*domain_alloc_paging)(struct device *dev);
+	struct iommu_domain *(*domain_alloc_sva)(struct device *dev,
+						 struct mm_struct *mm);
 
 	struct iommu_device *(*probe_device)(struct device *dev);
 	void (*release_device)(struct device *dev);
Signature · 1 line
2.34.1
author
#6 lore raw

PATCH v2 6/13 iommu/vt-d: Retire struct intel_svm

In reply to Tina Zhang

The struct intel_svm was used for keeping attached devices info for sva domain. Since sva domain is a kind of iommu_domain, the struct dmar_domain should centralize all info of a sva domain, including the info of attached devices. Therefore, retire struct intel_svm to clean up the code.

Besides, allocate sva domain in domain_alloc_sva() callback which allows the memory management notifier lifetime to follow the lifetime of the iommu_domain.

---
 drivers/iommu/intel/iommu.c |   3 +-
 drivers/iommu/intel/iommu.h |  14 +--
 drivers/iommu/intel/svm.c   | 180 +++++++++++-------------------------
 3 files changed, 61 insertions(+), 136 deletions(-)
drivers/iommu/intel/iommu.c +1 -2
index 6fb5f6fceea1..98a23a3a6447 100644
@@ -3865,8 +3865,6 @@ static struct iommu_domain *intel_iommu_domain_alloc(unsigned type)
 		return domain;
 	case IOMMU_DOMAIN_IDENTITY:
 		return &si_domain->domain;
-	case IOMMU_DOMAIN_SVA:
-		return intel_svm_domain_alloc();
 	default:
 		return NULL;
 	}
@@ -4746,6 +4744,7 @@ const struct iommu_ops intel_iommu_ops = {
 	.capable		= intel_iommu_capable,
 	.hw_info		= intel_iommu_hw_info,
 	.domain_alloc		= intel_iommu_domain_alloc,
+	.domain_alloc_sva	= intel_svm_domain_alloc,
 	.domain_alloc_user	= intel_iommu_domain_alloc_user,
 	.probe_device		= intel_iommu_probe_device,
 	.probe_finalize		= intel_iommu_probe_finalize,
drivers/iommu/intel/iommu.h +5 -9
index de60c0ab3ade..452d293bb1be 100644
@@ -642,6 +642,7 @@ struct dmar_domain {
 
 	struct iommu_domain domain;	/* generic domain data structure for
 					   iommu core */
+	struct mmu_notifier notifier;
 };
 
 /*
@@ -1083,20 +1084,15 @@ int intel_svm_enable_prq(struct intel_iommu *iommu);
 int intel_svm_finish_prq(struct intel_iommu *iommu);
 int intel_svm_page_response(struct device *dev, struct iommu_fault_event *evt,
 			    struct iommu_page_response *msg);
-struct iommu_domain *intel_svm_domain_alloc(void);
+struct iommu_domain *intel_svm_domain_alloc(struct device *dev,
+					    struct mm_struct *mm);
 void intel_svm_remove_dev_pasid(struct device *dev, ioasid_t pasid);
 void intel_drain_pasid_prq(struct device *dev, u32 pasid);
-
-struct intel_svm {
-	struct mmu_notifier notifier;
-	struct mm_struct *mm;
-	u32 pasid;
-	struct list_head devs;
-};
 #else
 static inline void intel_svm_check(struct intel_iommu *iommu) {}
 static inline void intel_drain_pasid_prq(struct device *dev, u32 pasid) {}
-static inline struct iommu_domain *intel_svm_domain_alloc(void)
+static inline struct iommu_domain *intel_svm_domain_alloc(struct device *dev,
+							  struct mm_struct *mm)
 {
 	return NULL;
 }
drivers/iommu/intel/svm.c +55 -125

322 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#7 lore raw

PATCH v2 7/13 iommu/vt-d: Use RCU for dev_pasids list updates in set/remove_dev_pasid()

In reply to Tina Zhang

Extend intel_iommu_remove_dev_pasid() and intel_iommu_set_dev_pasid() to support updating dev_pasids list concurrently with readers.

This extension doesn't have any effect on the default domain operations. Because for default domain operations, the dev_pasids list accesses are protected by domain->lock and therefore all read/write accesses of default domain operations to dev_pasids list are performed sequentially.

However, for sva domain, the dev_pasids list accesses are expected to perform concurrently. For example, the callbacks invoked by memory management notifier may run in a process which runs concurrently with another process wherein the intel_iommu_set/remove_dev_pasid operations are performed. Using RCU can give good read-side performance.

---
 drivers/iommu/intel/iommu.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/iommu/intel/iommu.c +9 -3
index 98a23a3a6447..b7e1f0be880b 100644
@@ -4569,7 +4569,7 @@ static void intel_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid)
 	spin_lock_irqsave(&dmar_domain->lock, flags);
 	list_for_each_entry(curr, &dmar_domain->dev_pasids, link_domain) {
 		if (curr->dev == dev && curr->pasid == pasid) {
-			list_del(&curr->link_domain);
+			list_del_rcu(&curr->link_domain);
 			dev_pasid = curr;
 			break;
 		}
@@ -4579,7 +4579,7 @@ static void intel_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid)
 
 	domain_detach_iommu(dmar_domain, iommu);
 	intel_iommu_debugfs_remove_dev_pasid(dev_pasid);
-	kfree(dev_pasid);
+	kfree_rcu(dev_pasid, rcu);
 out_tear_down:
 	intel_pasid_tear_down_entry(iommu, dev, pasid, false);
 	intel_drain_pasid_prq(dev, pasid);
@@ -4629,8 +4629,14 @@ static int intel_iommu_set_dev_pasid(struct iommu_domain *domain,
 
 	dev_pasid->dev = dev;
 	dev_pasid->pasid = pasid;
+
+	/*
+	 * Spin lock protects dev_pasids list from being updated concurrently with
+	 * multiple updaters, while rcu ensures concurrency between one updater
+	 * and multiple readers
+	 */
 	spin_lock_irqsave(&dmar_domain->lock, flags);
-	list_add(&dev_pasid->link_domain, &dmar_domain->dev_pasids);
+	list_add_rcu(&dev_pasid->link_domain, &dmar_domain->dev_pasids);
 	spin_unlock_irqrestore(&dmar_domain->lock, flags);
 
 	if (domain->type & __IOMMU_DOMAIN_PAGING)
Signature · 1 line
2.34.1
author
#8 lore raw

PATCH v2 8/13 iommu/vt-d: Add domain_type_is_sva helper function

In reply to Tina Zhang

Introduce a helper function to tell if the type of a dmar_domain is sva or not. In general, comparing with normal iommu_domain, a sva domain shouldn't be special other than having a different source for the page table. Thus, this helper function should be used as little as possible (i.e. only in the cases related to handling page table source).

---
 drivers/iommu/intel/iommu.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/iommu/intel/iommu.c +6 -1
index b7e1f0be880b..07ca7cfd86c0 100644
@@ -245,6 +245,11 @@ static int domain_type_is_si(struct dmar_domain *domain)
 	return domain->domain.type == IOMMU_DOMAIN_IDENTITY;
 }
 
+static inline bool domain_type_is_sva(struct dmar_domain *domain)
+{
+	return domain->domain.type == IOMMU_DOMAIN_SVA;
+}
+
 static int domain_pfn_supported(struct dmar_domain *domain, unsigned long pfn)
 {
 	int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
@@ -4560,7 +4565,7 @@ static void intel_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid)
 	 * notification. Before consolidating that code into iommu core, let
 	 * the intel sva code handle it.
 	 */
-	if (domain->type == IOMMU_DOMAIN_SVA) {
+	if (domain_type_is_sva(dmar_domain)) {
 		intel_svm_remove_dev_pasid(dev, pasid);
 		goto out_tear_down;
 	}
Signature · 1 line
2.34.1
author
#9 lore raw

PATCH v2 9/13 iommu/vt-d: Reuse intel_iommu_set_dev_pasid function

In reply to Tina Zhang

Refactor intel_iommu_set_dev_pasid() and make it be reused by sva domain as the set_dev_pasid operation.

---
 drivers/iommu/intel/iommu.c | 32 +++++++++++++++++++++++++++--
 drivers/iommu/intel/iommu.h |  2 ++
 drivers/iommu/intel/svm.c   | 41 +------------------------------------
 3 files changed, 33 insertions(+), 42 deletions(-)
drivers/iommu/intel/iommu.c +30 -2
index 07ca7cfd86c0..c16ff5c64264 100644
@@ -1644,6 +1644,21 @@ int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu)
 	unsigned long ndomains;
 	int num, ret = -ENOSPC;
 
+	/*
+	 * SVA domain can get the iommu_domain_info bits from struct dev_pasid_info:
+	 * 1) SVA domain uses FLPT_DEFAULT_DID as the did;
+	 * 2) SVA domain can dereference a pointer of struct intel_iommu using
+	 *    dev_iommu_priv_get(dev_pasid->dev)->iommu.
+	 * 3) Since SVA domain doesn't need to use struct iommu_domain_info, it
+	 *    certainly doesn't need to keep struct iommu_domain_info instance's
+	 *    refcount.
+	 *
+	 * Therefore, hold off allocating struct iommu_domain_info for SVA domain
+	 * until we see it's necessary.
+	 */
+	if (domain_type_is_sva(domain))
+		return 0;
+
 	info = kzalloc(sizeof(*info), GFP_KERNEL);
 	if (!info)
 		return -ENOMEM;
@@ -1691,6 +1706,10 @@ void domain_detach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu)
 {
 	struct iommu_domain_info *info;
 
+	/* See the comment in domain_attach_iommu() */
+	if (domain_type_is_sva(domain))
+		return;
+
 	spin_lock(&iommu->lock);
 	info = xa_load(&domain->iommu_array, iommu->seq_id);
 	if (--info->refcnt == 0) {
@@ -4590,7 +4609,7 @@ static void intel_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid)
 	intel_drain_pasid_prq(dev, pasid);
 }
 
-static int intel_iommu_set_dev_pasid(struct iommu_domain *domain,
+int intel_iommu_set_dev_pasid(struct iommu_domain *domain,
 				     struct device *dev, ioasid_t pasid)
 {
 	struct device_domain_info *info = dev_iommu_priv_get(dev);
@@ -4623,6 +4642,10 @@ static int intel_iommu_set_dev_pasid(struct iommu_domain *domain,
 
 	if (domain_type_is_si(dmar_domain))
 		ret = intel_pasid_setup_pass_through(iommu, dev, pasid);
+	else if (domain_type_is_sva(dmar_domain))
+		ret = intel_pasid_setup_first_level(iommu, dev,
+			domain->mm->pgd, pasid, FLPT_DEFAULT_DID,
+			cpu_feature_enabled(X86_FEATURE_LA57) ? PASID_FLAG_FL5LP : 0);
 	else if (dmar_domain->use_first_level)
 		ret = domain_setup_first_level(iommu, dmar_domain,
 					       dev, pasid);
@@ -4634,7 +4657,12 @@ static int intel_iommu_set_dev_pasid(struct iommu_domain *domain,
 
 	dev_pasid->dev = dev;
 	dev_pasid->pasid = pasid;
-
+	dev_pasid->sid = PCI_DEVID(info->bus, info->devfn);
+	if (info->ats_enabled) {
+		dev_pasid->qdep = info->ats_qdep;
+		if (dev_pasid->qdep >= QI_DEV_EIOTLB_MAX_INVS)
+			dev_pasid->qdep = 0;
+	}
 	/*
 	 * Spin lock protects dev_pasids list from being updated concurrently with
 	 * multiple updaters, while rcu ensures concurrency between one updater
drivers/iommu/intel/iommu.h +2 -0
index 452d293bb1be..ec071a6a56f0 100644
@@ -1077,6 +1077,8 @@ void free_pgtable_page(void *vaddr);
 void iommu_flush_write_buffer(struct intel_iommu *iommu);
 struct iommu_domain *intel_nested_domain_alloc(struct iommu_domain *parent,
 					       const struct iommu_user_data *user_data);
+int intel_iommu_set_dev_pasid(struct iommu_domain *domain,
+			      struct device *dev, ioasid_t pasid);
 
 #ifdef CONFIG_INTEL_IOMMU_SVM
 void intel_svm_check(struct intel_iommu *iommu);
drivers/iommu/intel/svm.c +1 -40

53 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#10 lore raw

PATCH v2 10/13 iommu/vt-d: Use dev_pasid->sid field

In reply to Tina Zhang

Use dev_pasid->sid, since it has been assigned the value of PCI_DEVID(info->bus, info->devfn).

---
 drivers/iommu/intel/iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/intel/iommu.c +1 -1
index c16ff5c64264..0d4d8cee1639 100644
@@ -1348,7 +1348,7 @@ static void iommu_flush_dev_iotlb(struct dmar_domain *domain,
 			continue;
 
 		qi_flush_dev_iotlb_pasid(info->iommu,
-					 PCI_DEVID(info->bus, info->devfn),
+					 dev_pasid->sid,
 					 info->pfsid, dev_pasid->pasid,
 					 info->ats_qdep, addr,
 					 mask);
Signature · 1 line
2.34.1
author
#11 lore raw

PATCH v2 11/13 iommu/vt-d: Retire intel_svm_remove_dev_pasid function

In reply to Tina Zhang

IOMMU core has added a dedicated callback for allocating sva domain. So intel driver doesn't need intel_svm_remove_dev_pasid function to handle mm notification any more.

---
 drivers/iommu/intel/iommu.c | 12 +-----------
 drivers/iommu/intel/iommu.h |  4 ----
 drivers/iommu/intel/svm.c   | 34 ----------------------------------
 3 files changed, 1 insertion(+), 49 deletions(-)
drivers/iommu/intel/iommu.c +1 -11
index 0d4d8cee1639..ebbdbeefc402 100644
@@ -4576,19 +4576,9 @@ static void intel_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid)
 	unsigned long flags;
 
 	domain = iommu_get_domain_for_dev_pasid(dev, pasid, 0);
-	if (WARN_ON_ONCE(!domain))
+	if (WARN_ON_ONCE(IS_ERR_OR_NULL(domain)))
 		goto out_tear_down;
 
-	/*
-	 * The SVA implementation needs to handle its own stuffs like the mm
-	 * notification. Before consolidating that code into iommu core, let
-	 * the intel sva code handle it.
-	 */
-	if (domain_type_is_sva(dmar_domain)) {
-		intel_svm_remove_dev_pasid(dev, pasid);
-		goto out_tear_down;
-	}
-
 	dmar_domain = to_dmar_domain(domain);
 	spin_lock_irqsave(&dmar_domain->lock, flags);
 	list_for_each_entry(curr, &dmar_domain->dev_pasids, link_domain) {
drivers/iommu/intel/iommu.h +0 -4
index ec071a6a56f0..e09aa4ee609d 100644
@@ -1088,7 +1088,6 @@ int intel_svm_page_response(struct device *dev, struct iommu_fault_event *evt,
 			    struct iommu_page_response *msg);
 struct iommu_domain *intel_svm_domain_alloc(struct device *dev,
 					    struct mm_struct *mm);
-void intel_svm_remove_dev_pasid(struct device *dev, ioasid_t pasid);
 void intel_drain_pasid_prq(struct device *dev, u32 pasid);
 #else
 static inline void intel_svm_check(struct intel_iommu *iommu) {}
@@ -1099,9 +1098,6 @@ static inline struct iommu_domain *intel_svm_domain_alloc(struct device *dev,
 	return NULL;
 }
 
-static inline void intel_svm_remove_dev_pasid(struct device *dev, ioasid_t pasid)
-{
-}
 #endif
 
 #ifdef CONFIG_INTEL_IOMMU_DEBUGFS
drivers/iommu/intel/svm.c +0 -34
index 4cf7f5445e1a..38e9ff95029a 100644
@@ -27,23 +27,6 @@
 
 static irqreturn_t prq_event_thread(int irq, void *d);
 
-static struct dev_pasid_info *
-domain_lookup_dev_pasid_info_by_dev(struct dmar_domain *domain, struct device *dev)
-{
-	struct dev_pasid_info *dev_pasid = NULL, *t;
-
-	rcu_read_lock();
-	list_for_each_entry_rcu(t, &domain->dev_pasids, link_domain) {
-		if (t->dev == dev) {
-			dev_pasid = t;
-			break;
-		}
-	}
-	rcu_read_unlock();
-
-	return dev_pasid;
-}
-
 int intel_svm_enable_prq(struct intel_iommu *iommu)
 {
 	struct iopf_queue *iopfq;
@@ -270,23 +253,6 @@ static const struct mmu_notifier_ops intel_mmuops = {
 	.arch_invalidate_secondary_tlbs = intel_arch_invalidate_secondary_tlbs,
 };
 
-void intel_svm_remove_dev_pasid(struct device *dev, u32 pasid)
-{
-	struct iommu_domain *domain;
-	struct dev_pasid_info *dev_pasid;
-
-	domain = iommu_get_domain_for_dev_pasid(dev, pasid,
-						IOMMU_DOMAIN_SVA);
-	if (WARN_ON_ONCE(IS_ERR_OR_NULL(domain)))
-		return;
-
-	dev_pasid = domain_lookup_dev_pasid_info_by_dev(to_dmar_domain(domain), dev);
-	if (dev_pasid) {
-		list_del_rcu(&dev_pasid->link_domain);
-		kfree_rcu(dev_pasid, rcu);
-	}
-}
-
 /* Page request queue descriptor */
 struct page_req_dsc {
 	union {
Signature · 1 line
2.34.1
author
#12 lore raw

PATCH v2 12/13 iommu/vt-d: Sort dev_pasids list

In reply to Tina Zhang

Sort dev_pasids list to make the devices behind same IOMMU stay together.

---
 drivers/iommu/intel/iommu.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/iommu/intel/iommu.c +11 -2
index ebbdbeefc402..f62094fc7512 100644
@@ -4605,7 +4605,7 @@ int intel_iommu_set_dev_pasid(struct iommu_domain *domain,
 	struct device_domain_info *info = dev_iommu_priv_get(dev);
 	struct dmar_domain *dmar_domain = to_dmar_domain(domain);
 	struct intel_iommu *iommu = info->iommu;
-	struct dev_pasid_info *dev_pasid;
+	struct dev_pasid_info *dev_pasid, *curr;
 	unsigned long flags;
 	int ret;
 
@@ -4645,6 +4645,7 @@ int intel_iommu_set_dev_pasid(struct iommu_domain *domain,
 	if (ret)
 		goto out_detach_iommu;
 
+	INIT_LIST_HEAD(&dev_pasid->link_domain);
 	dev_pasid->dev = dev;
 	dev_pasid->pasid = pasid;
 	dev_pasid->sid = PCI_DEVID(info->bus, info->devfn);
@@ -4659,7 +4660,15 @@ int intel_iommu_set_dev_pasid(struct iommu_domain *domain,
 	 * and multiple readers
 	 */
 	spin_lock_irqsave(&dmar_domain->lock, flags);
-	list_add_rcu(&dev_pasid->link_domain, &dmar_domain->dev_pasids);
+	list_for_each_entry(curr, &dmar_domain->dev_pasids, link_domain) {
+		info = dev_iommu_priv_get(curr->dev);
+		if (info->iommu == iommu) {
+			list_add_rcu(&dev_pasid->link_domain, &curr->link_domain);
+			break;
+		}
+	}
+	if (list_empty(&dev_pasid->link_domain))
+		list_add_rcu(&dev_pasid->link_domain, &dmar_domain->dev_pasids);
 	spin_unlock_irqrestore(&dmar_domain->lock, flags);
 
 	if (domain->type & __IOMMU_DOMAIN_PAGING)
Signature · 1 line
2.34.1
author
#13 lore raw

PATCH v2 13/13 iommu/vt-d: Remove superfluous IOMMU IOTLB invalidations

In reply to Tina Zhang

Devices behind different IOMMUs can be bound to one sva domain. When a range of a sva domain address is being invalidated, VT-d driver needs to issue IOMMU IOTLB and Dev-IOTLB invalidation commands to ask IOMMU hardware and related devices to invalidate their caches.

The current logic issues both IOTLB invalidation command and device-TLB command per device, which leads to superfluous IOTLB invalidation (e.g., if there are four devices behind a IOMMU are attached to one sva domain. In the current logic, during handing intel_invalidate_range(), four IOTLB invalidation commands and four Dev-IOTLB invalidation commands will be issued. However, only one IOTLB invalidation command and four Dev-IOTLB invalidation command are necessary.), and therefore impacts run-time performance.

The patch removes the redundant IOMMU IOTLB invalidations by allowing issuing IOMMU IOTLB invalidation command per iommu instead of per device.

---
 drivers/iommu/intel/svm.c | 70 +++++++++++++++++++++------------------
 1 file changed, 37 insertions(+), 33 deletions(-)
drivers/iommu/intel/svm.c +37 -33
index 38e9ff95029a..0c4c0e4011d2 100644
@@ -135,31 +135,40 @@ void intel_svm_check(struct intel_iommu *iommu)
 	iommu->flags |= VTD_FLAG_SVM_CAPABLE;
 }
 
-static void __flush_svm_range_dev(struct dmar_domain *domain,
-				  struct dev_pasid_info *dev_pasid,
+static void __flush_svm_range(struct dmar_domain *domain,
 				  unsigned long address,
 				  unsigned long pages, int ih)
 {
-	struct device_domain_info *info = dev_iommu_priv_get(dev_pasid->dev);
+	struct device_domain_info *dev_info;
+	struct intel_iommu *iommu = NULL;
+	struct dev_pasid_info *dev_pasid;
 	u32 pasid = mm_get_enqcmd_pasid(domain->domain.mm);
 
 	if (WARN_ON(!pages))
 		return;
 
-	qi_flush_piotlb(info->iommu, FLPT_DEFAULT_DID, pasid, address, pages, ih);
-	if (info->ats_enabled) {
-		qi_flush_dev_iotlb_pasid(info->iommu, dev_pasid->sid, info->pfsid,
-					 pasid, dev_pasid->qdep, address,
-					 order_base_2(pages));
-		quirk_extra_dev_tlb_flush(info, address, order_base_2(pages),
-					  pasid, dev_pasid->qdep);
+	rcu_read_lock();
+	list_for_each_entry_rcu(dev_pasid, &domain->dev_pasids, link_domain) {
+		dev_info = dev_iommu_priv_get(dev_pasid->dev);
+		if (iommu != dev_info->iommu) {
+			iommu = dev_info->iommu;
+			qi_flush_piotlb(iommu, FLPT_DEFAULT_DID,
+					pasid, address, pages, ih);
+		}
+
+		if (dev_info->ats_enabled) {
+			qi_flush_dev_iotlb_pasid(iommu, dev_pasid->sid, dev_info->pfsid,
+						 pasid, dev_pasid->qdep, address,
+						 order_base_2(pages));
+			quirk_extra_dev_tlb_flush(dev_info, address, order_base_2(pages),
+						  pasid, dev_pasid->qdep);
+		}
 	}
+	rcu_read_unlock();
 }
 
-static void intel_flush_svm_range_dev(struct dmar_domain *domain,
-				      struct dev_pasid_info *dev_pasid,
-				      unsigned long address,
-				      unsigned long pages, int ih)
+static void intel_flush_svm_range(struct dmar_domain *domain, unsigned long address,
+				unsigned long pages, int ih)
 {
 	unsigned long shift = ilog2(__roundup_pow_of_two(pages));
 	unsigned long align = (1ULL << (VTD_PAGE_SHIFT + shift));
@@ -167,37 +176,32 @@ static void intel_flush_svm_range_dev(struct dmar_domain *domain,
 	unsigned long end = ALIGN(address + (pages << VTD_PAGE_SHIFT), align);
 
 	while (start < end) {
-		__flush_svm_range_dev(domain, dev_pasid, start, align >> VTD_PAGE_SHIFT, ih);
+		__flush_svm_range(domain, start, align >> VTD_PAGE_SHIFT, ih);
 		start += align;
 	}
 }
 
-static void intel_flush_svm_range(struct dmar_domain *domain, unsigned long address,
-				unsigned long pages, int ih)
-{
-	struct dev_pasid_info *dev_pasid;
-
-	rcu_read_lock();
-	list_for_each_entry_rcu(dev_pasid, &domain->dev_pasids, link_domain)
-		intel_flush_svm_range_dev(domain, dev_pasid, address, pages, ih);
-	rcu_read_unlock();
-}
-
 static void intel_flush_svm_all(struct dmar_domain *domain)
 {
-	struct device_domain_info *info;
-	struct dev_pasid_info *dev_pasid = NULL;
+	struct device_domain_info *dev_info;
+	struct intel_iommu *iommu = NULL;
+	struct dev_pasid_info *dev_pasid;
 	u32 pasid = mm_get_enqcmd_pasid(domain->domain.mm);
 
 	rcu_read_lock();
 	list_for_each_entry_rcu(dev_pasid, &domain->dev_pasids, link_domain) {
-		info = dev_iommu_priv_get(dev_pasid->dev);
-		qi_flush_piotlb(info->iommu, FLPT_DEFAULT_DID, pasid, 0, -1UL, 0);
-		if (info->ats_enabled) {
-			qi_flush_dev_iotlb_pasid(info->iommu, dev_pasid->sid, info->pfsid,
+		dev_info = dev_iommu_priv_get(dev_pasid->dev);
+		if (iommu != dev_info->iommu) {
+			iommu = dev_info->iommu;
+			qi_flush_piotlb(iommu, FLPT_DEFAULT_DID,
+					pasid, 0, -1UL, 0);
+		}
+
+		if (dev_info->ats_enabled) {
+			qi_flush_dev_iotlb_pasid(dev_info->iommu, dev_pasid->sid, dev_info->pfsid,
 						 pasid, dev_pasid->qdep,
 						 0, 64 - VTD_PAGE_SHIFT);
-			quirk_extra_dev_tlb_flush(info, 0, 64 - VTD_PAGE_SHIFT,
+			quirk_extra_dev_tlb_flush(dev_info, 0, 64 - VTD_PAGE_SHIFT,
 						  pasid, dev_pasid->qdep);
 		}
 	}
Signature · 1 line
2.34.1
maintainer
#14 lore raw
In reply to Tina Zhang
Quoted 6 lines · Tina Zhang

Tina Zhang (12):
iommu/vt-d: Retire the treatment for revoking PASIDs with pending
pgfaults
iommu/vt-d: Remove initialization for dynamically heap-allocated
rcu_head
iommu/vt-d: Refactor intel_svm_set_dev_pasid function

Above three cleanup patches have been queued for v6.9.

Best regards,
baolu