Skip to contentSkip to conversation

iommu/vt-d: Misc fixes on scalable mode

PATCH Superseded Liu Yi L 2 participants 6 messages Dec 23, 2020 – Dec 25, 2020

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Liu Yi L

author
#0 lore raw

v2 cover iommu/vt-d: Misc fixes on scalable mode

This patchset aims to fix a bug regards to native SVM usage, and also several bugs around subdevice (attached to device via auxiliary manner) tracking and ineffective device_tlb flush.

Liu Yi L (3):
iommu/vt-d: Move intel_iommu info from struct intel_svm to struct
intel_svm_dev
iommu/vt-d: Track device aux-attach with subdevice_domain_info iommu/vt-d: Fix ineffective devTLB invalidation for subdevices

 drivers/iommu/intel/iommu.c | 158 +++++++++++++++++++++++++++---------
 drivers/iommu/intel/svm.c   |   9 +-
 include/linux/intel-iommu.h |  18 ++--
 3 files changed, 135 insertions(+), 50 deletions(-)
Signature · 6 lines
2.25.1

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

PATCH v2 1/3 iommu/vt-d: Move intel_iommu info from struct intel_svm to struct intel_svm_dev

Current struct intel_svm has a field to record the struct intel_iommu pointer for a PASID bind. And struct intel_svm will be shared by all the devices bind to the same process. The devices may be behind different DMAR units. As the iommu driver code uses the intel_iommu pointer stored in intel_svm struct to do cache invalidations, it may only flush the cache on a single DMAR unit, for others, the cache invalidation is missed.

As intel_svm struct already has a device list, this patch just moves the intel_iommu pointer to be a field of intel_svm_dev struct.

Fixes 1c4f88b7f1f92 ("iommu/vt-d: Shared virtual address in scalable mode")
Cc Lu Baolu <[email protected]>
Cc Jacob Pan <[email protected]>
Cc Raj Ashok <[email protected]>
Cc David Woodhouse <[email protected]>
---
 drivers/iommu/intel/svm.c   | 9 +++++----
 include/linux/intel-iommu.h | 2 +-
 2 files changed, 6 insertions(+), 5 deletions(-)
drivers/iommu/intel/svm.c +5 -4
index 3242ebd0bca3..4a10c9ff368c 100644
@@ -142,7 +142,7 @@ static void intel_flush_svm_range_dev (struct intel_svm *svm, struct intel_svm_d
 	}
 	desc.qw2 = 0;
 	desc.qw3 = 0;
-	qi_submit_sync(svm->iommu, &desc, 1, 0);
+	qi_submit_sync(sdev->iommu, &desc, 1, 0);
 
 	if (sdev->dev_iotlb) {
 		desc.qw0 = QI_DEV_EIOTLB_PASID(svm->pasid) |
@@ -166,7 +166,7 @@ static void intel_flush_svm_range_dev (struct intel_svm *svm, struct intel_svm_d
 		}
 		desc.qw2 = 0;
 		desc.qw3 = 0;
-		qi_submit_sync(svm->iommu, &desc, 1, 0);
+		qi_submit_sync(sdev->iommu, &desc, 1, 0);
 	}
 }
 
@@ -211,7 +211,7 @@ static void intel_mm_release(struct mmu_notifier *mn, struct mm_struct *mm)
 	 */
 	rcu_read_lock();
 	list_for_each_entry_rcu(sdev, &svm->devs, list)
-		intel_pasid_tear_down_entry(svm->iommu, sdev->dev,
+		intel_pasid_tear_down_entry(sdev->iommu, sdev->dev,
 					    svm->pasid, true);
 	rcu_read_unlock();
 
@@ -363,6 +363,7 @@ int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev,
 	}
 	sdev->dev = dev;
 	sdev->sid = PCI_DEVID(info->bus, info->devfn);
+	sdev->iommu = iommu;
 
 	/* Only count users if device has aux domains */
 	if (iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX))
@@ -546,6 +547,7 @@ intel_svm_bind_mm(struct device *dev, unsigned int flags,
 		goto out;
 	}
 	sdev->dev = dev;
+	sdev->iommu = iommu;
 
 	ret = intel_iommu_enable_pasid(iommu, dev);
 	if (ret) {
@@ -575,7 +577,6 @@ intel_svm_bind_mm(struct device *dev, unsigned int flags,
 			kfree(sdev);
 			goto out;
 		}
-		svm->iommu = iommu;
 
 		if (pasid_max > intel_pasid_max_id)
 			pasid_max = intel_pasid_max_id;
include/linux/intel-iommu.h +1 -1
index d956987ed032..94522685a0d9 100644
@@ -758,6 +758,7 @@ struct intel_svm_dev {
 	struct list_head list;
 	struct rcu_head rcu;
 	struct device *dev;
+	struct intel_iommu *iommu;
 	struct svm_dev_ops *ops;
 	struct iommu_sva sva;
 	u32 pasid;
@@ -771,7 +772,6 @@ struct intel_svm {
 	struct mmu_notifier notifier;
 	struct mm_struct *mm;
 
-	struct intel_iommu *iommu;
 	unsigned int flags;
 	u32 pasid;
 	int gpasid; /* In case that guest PASID is different from host PASID */
Signature · 6 lines
2.25.1

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

PATCH v2 2/3 iommu/vt-d: Track device aux-attach with subdevice_domain_info

In reply to Liu Yi L

In the existing code, loop all devices attached to a domain does not include sub-devices attached via iommu_aux_attach_device().

This was found by when I'm working on the belwo patch, There is no device in the domain->devices list, thus unable to get the cap and ecap of iommu unit. But this domain actually has subdevice which is attached via aux-manner. But it is tracked by domain. This patch is going to fix it.

https://lore.kernel.org/kvm/[email protected]/

And this fix goes beyond the patch above, such sub-device tracking is necessary for other cases. For example, flushing device_iotlb for a domain which has sub-devices attached by auxiliary manner.

---
 drivers/iommu/intel/iommu.c | 95 +++++++++++++++++++++++++++----------
 include/linux/intel-iommu.h | 16 +++++--
 2 files changed, 82 insertions(+), 29 deletions(-)
drivers/iommu/intel/iommu.c +71 -24
index a49afa11673c..acfe0a5b955e 100644
@@ -1881,6 +1881,7 @@ static struct dmar_domain *alloc_domain(int flags)
 		domain->flags |= DOMAIN_FLAG_USE_FIRST_LEVEL;
 	domain->has_iotlb_device = false;
 	INIT_LIST_HEAD(&domain->devices);
+	INIT_LIST_HEAD(&domain->subdevices);
 
 	return domain;
 }
@@ -2632,7 +2633,7 @@ static struct dmar_domain *dmar_insert_one_dev_info(struct intel_iommu *iommu,
 	info->iommu = iommu;
 	info->pasid_table = NULL;
 	info->auxd_enabled = 0;
-	INIT_LIST_HEAD(&info->auxiliary_domains);
+	INIT_LIST_HEAD(&info->subdevices);
 
 	if (dev && dev_is_pci(dev)) {
 		struct pci_dev *pdev = to_pci_dev(info->dev);
@@ -5172,33 +5173,61 @@ is_aux_domain(struct device *dev, struct iommu_domain *domain)
 			domain->type == IOMMU_DOMAIN_UNMANAGED;
 }
 
-static void auxiliary_link_device(struct dmar_domain *domain,
-				  struct device *dev)
+static inline struct subdev_domain_info *
+lookup_subdev_info(struct dmar_domain *domain, struct device *dev)
+{
+	struct subdev_domain_info *sinfo;
+
+	if (!list_empty(&domain->subdevices)) {
+		list_for_each_entry(sinfo, &domain->subdevices, link_domain) {
+			if (sinfo->pdev == dev)
+				return sinfo;
+		}
+	}
+
+	return NULL;
+}
+
+static int auxiliary_link_device(struct dmar_domain *domain,
+				 struct device *dev)
 {
 	struct device_domain_info *info = get_domain_info(dev);
+	struct subdev_domain_info *sinfo = lookup_subdev_info(domain, dev);
 
 	assert_spin_locked(&device_domain_lock);
 	if (WARN_ON(!info))
-		return;
+		return -EINVAL;
+
+	if (!sinfo) {
+		sinfo = kzalloc(sizeof(*sinfo), GFP_ATOMIC);
+		sinfo->domain = domain;
+		sinfo->pdev = dev;
+		list_add(&sinfo->link_phys, &info->subdevices);
+		list_add(&sinfo->link_domain, &domain->subdevices);
+	}
 
-	domain->auxd_refcnt++;
-	list_add(&domain->auxd, &info->auxiliary_domains);
+	return ++sinfo->users;
 }
 
-static void auxiliary_unlink_device(struct dmar_domain *domain,
-				    struct device *dev)
+static int auxiliary_unlink_device(struct dmar_domain *domain,
+				   struct device *dev)
 {
 	struct device_domain_info *info = get_domain_info(dev);
+	struct subdev_domain_info *sinfo = lookup_subdev_info(domain, dev);
+	int ret;
 
 	assert_spin_locked(&device_domain_lock);
-	if (WARN_ON(!info))
-		return;
+	if (WARN_ON(!info || !sinfo || sinfo->users <= 0))
+		return -EINVAL;
 
-	list_del(&domain->auxd);
-	domain->auxd_refcnt--;
+	ret = --sinfo->users;
+	if (!ret) {
+		list_del(&sinfo->link_phys);
+		list_del(&sinfo->link_domain);
+		kfree(sinfo);
+	}
 
-	if (!domain->auxd_refcnt && domain->default_pasid > 0)
-		ioasid_free(domain->default_pasid);
+	return ret;
 }
 
 static int aux_domain_add_dev(struct dmar_domain *domain,
@@ -5227,6 +5256,19 @@ static int aux_domain_add_dev(struct dmar_domain *domain,
 	}
 
 	spin_lock_irqsave(&device_domain_lock, flags);
+	ret = auxiliary_link_device(domain, dev);
+	if (ret <= 0)
+		goto link_failed;
+
+	/*
+	 * Subdevices from the same physical device can be attached to the
+	 * same domain. For such cases, only the first subdevice attachment
+	 * needs to go through the full steps in this function. So if ret >
+	 * 1, just goto out.
+	 */
+	if (ret > 1)
+		goto out;
+
 	/*
 	 * iommu->lock must be held to attach domain to iommu and setup the
 	 * pasid entry for second level translation.
@@ -5245,10 +5287,9 @@ static int aux_domain_add_dev(struct dmar_domain *domain,
 						     domain->default_pasid);
 	if (ret)
 		goto table_failed;
-	spin_unlock(&iommu->lock);
-
-	auxiliary_link_device(domain, dev);
 
+	spin_unlock(&iommu->lock);
+out:
 	spin_unlock_irqrestore(&device_domain_lock, flags);
 
 	return 0;
@@ -5257,8 +5298,10 @@ static int aux_domain_add_dev(struct dmar_domain *domain,
 	domain_detach_iommu(domain, iommu);
 attach_failed:
 	spin_unlock(&iommu->lock);
+	auxiliary_unlink_device(domain, dev);
+link_failed:
 	spin_unlock_irqrestore(&device_domain_lock, flags);
-	if (!domain->auxd_refcnt && domain->default_pasid > 0)
+	if (list_empty(&domain->subdevices) && domain->default_pasid > 0)
 		ioasid_free(domain->default_pasid);
 
 	return ret;
@@ -5278,14 +5321,18 @@ static void aux_domain_remove_dev(struct dmar_domain *domain,
 	info = get_domain_info(dev);
 	iommu = info->iommu;
 
-	auxiliary_unlink_device(domain, dev);
-
-	spin_lock(&iommu->lock);
-	intel_pasid_tear_down_entry(iommu, dev, domain->default_pasid, false);
-	domain_detach_iommu(domain, iommu);
-	spin_unlock(&iommu->lock);
+	if (!auxiliary_unlink_device(domain, dev)) {
+		spin_lock(&iommu->lock);
+		intel_pasid_tear_down_entry(iommu, dev,
+					    domain->default_pasid, false);
+		domain_detach_iommu(domain, iommu);
+		spin_unlock(&iommu->lock);
+	}
 
 	spin_unlock_irqrestore(&device_domain_lock, flags);
+
+	if (list_empty(&domain->subdevices) && domain->default_pasid > 0)
+		ioasid_free(domain->default_pasid);
 }
 
 static int prepare_domain_attach_device(struct iommu_domain *domain,
include/linux/intel-iommu.h +11 -5
index 94522685a0d9..09c6a0bf3892 100644
@@ -533,11 +533,10 @@ struct dmar_domain {
 					/* Domain ids per IOMMU. Use u16 since
 					 * domain ids are 16 bit wide according
 					 * to VT-d spec, section 9.3 */
-	unsigned int	auxd_refcnt;	/* Refcount of auxiliary attaching */
 
 	bool has_iotlb_device;
 	struct list_head devices;	/* all devices' list */
-	struct list_head auxd;		/* link to device's auxiliary list */
+	struct list_head subdevices;	/* all subdevices' list */
 	struct iova_domain iovad;	/* iova's that belong to this domain */
 
 	struct dma_pte	*pgd;		/* virtual address */
@@ -610,14 +609,21 @@ struct intel_iommu {
 	struct dmar_drhd_unit *drhd;
 };
 
+/* Per subdevice private data */
+struct subdev_domain_info {
+	struct list_head link_phys;	/* link to phys device siblings */
+	struct list_head link_domain;	/* link to domain siblings */
+	struct device *pdev;		/* physical device derived from */
+	struct dmar_domain *domain;	/* aux-domain */
+	int users;			/* user count */
+};
+
 /* PCI domain-device relationship */
 struct device_domain_info {
 	struct list_head link;	/* link to domain siblings */
 	struct list_head global; /* link to global list */
 	struct list_head table;	/* link to pasid table */
-	struct list_head auxiliary_domains; /* auxiliary domains
-					     * attached to this device
-					     */
+	struct list_head subdevices; /* subdevices sibling */
 	u32 segment;		/* PCI segment number */
 	u8 bus;			/* PCI bus number */
 	u8 devfn;		/* PCI devfn number */
Signature · 6 lines
2.25.1

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

PATCH v2 3/3 iommu/vt-d: Fix ineffective devTLB invalidation for subdevices

In reply to Liu Yi L

iommu_flush_dev_iotlb() is called to invalidate caches on device. It only loops the devices which are full-attached to the domain. For sub-devices, this is ineffective. This results in invalid caching entries left on the device. Fix it by adding loop for subdevices as well. Also, the domain-> has_iotlb_device needs to be updated when attaching to subdevices.

Fixes 67b8e02b5e761 ("iommu/vt-d: Aux-domain specific domain attach/detach")
---
 drivers/iommu/intel/iommu.c | 63 +++++++++++++++++++++++++++----------
 1 file changed, 47 insertions(+), 16 deletions(-)
drivers/iommu/intel/iommu.c +47 -16
index acfe0a5b955e..e97c5ac1d7fc 100644
@@ -726,6 +726,8 @@ static int domain_update_device_node(struct dmar_domain *domain)
 	return nid;
 }
 
+static void domain_update_iotlb(struct dmar_domain *domain);
+
 /* Some capabilities may be different across iommus */
 static void domain_update_iommu_cap(struct dmar_domain *domain)
 {
@@ -739,6 +741,8 @@ static void domain_update_iommu_cap(struct dmar_domain *domain)
 	 */
 	if (domain->nid == NUMA_NO_NODE)
 		domain->nid = domain_update_device_node(domain);
+
+	domain_update_iotlb(domain);
 }
 
 struct context_entry *iommu_context_addr(struct intel_iommu *iommu, u8 bus,
@@ -1459,6 +1463,18 @@ iommu_support_dev_iotlb (struct dmar_domain *domain, struct intel_iommu *iommu,
 	return NULL;
 }
 
+static bool dev_iotlb_enabled(struct device_domain_info *info)
+{
+	struct pci_dev *pdev;
+
+	if (!info->dev || !dev_is_pci(info->dev))
+		return false;
+
+	pdev = to_pci_dev(info->dev);
+
+	return !!pdev->ats_enabled;
+}
+
 static void domain_update_iotlb(struct dmar_domain *domain)
 {
 	struct device_domain_info *info;
@@ -1466,17 +1482,20 @@ static void domain_update_iotlb(struct dmar_domain *domain)
 
 	assert_spin_locked(&device_domain_lock);
 
-	list_for_each_entry(info, &domain->devices, link) {
-		struct pci_dev *pdev;
-
-		if (!info->dev || !dev_is_pci(info->dev))
-			continue;
-
-		pdev = to_pci_dev(info->dev);
-		if (pdev->ats_enabled) {
+	list_for_each_entry(info, &domain->devices, link)
+		if (dev_iotlb_enabled(info)) {
 			has_iotlb_device = true;
 			break;
 		}
+
+	if (!has_iotlb_device) {
+		struct subdev_domain_info *sinfo;
+
+		list_for_each_entry(sinfo, &domain->subdevices, link_domain)
+			if (dev_iotlb_enabled(get_domain_info(sinfo->pdev))) {
+				has_iotlb_device = true;
+				break;
+			}
 	}
 
 	domain->has_iotlb_device = has_iotlb_device;
@@ -1557,25 +1576,37 @@ static void iommu_disable_dev_iotlb(struct device_domain_info *info)
 #endif
 }
 
+static void __iommu_flush_dev_iotlb(struct device_domain_info *info,
+				    u64 addr, unsigned int mask)
+{
+	u16 sid, qdep;
+
+	if (!info || !info->ats_enabled)
+		return;
+
+	sid = info->bus << 8 | info->devfn;
+	qdep = info->ats_qdep;
+	qi_flush_dev_iotlb(info->iommu, sid, info->pfsid,
+			   qdep, addr, mask);
+}
+
 static void iommu_flush_dev_iotlb(struct dmar_domain *domain,
 				  u64 addr, unsigned mask)
 {
-	u16 sid, qdep;
 	unsigned long flags;
 	struct device_domain_info *info;
+	struct subdev_domain_info *sinfo;
 
 	if (!domain->has_iotlb_device)
 		return;
 
 	spin_lock_irqsave(&device_domain_lock, flags);
-	list_for_each_entry(info, &domain->devices, link) {
-		if (!info->ats_enabled)
-			continue;
+	list_for_each_entry(info, &domain->devices, link)
+		__iommu_flush_dev_iotlb(info, addr, mask);
 
-		sid = info->bus << 8 | info->devfn;
-		qdep = info->ats_qdep;
-		qi_flush_dev_iotlb(info->iommu, sid, info->pfsid,
-				qdep, addr, mask);
+	list_for_each_entry(sinfo, &domain->subdevices, link_domain) {
+		__iommu_flush_dev_iotlb(get_domain_info(sinfo->pdev),
+					addr, mask);
 	}
 	spin_unlock_irqrestore(&device_domain_lock, flags);
 }
Signature · 6 lines
2.25.1

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

Re: [PATCH v2 3/3] iommu/vt-d: Fix ineffective devTLB invalidation for subdevices

Hi Yi,

Quoted 39 lines · Liu Yi L

iommu_flush_dev_iotlb() is called to invalidate caches on device. It only loops the devices which are full-attached to the domain. For sub-devices, this is ineffective. This results in invalid caching entries left on the device. Fix it by adding loop for subdevices as well. Also, the domain-> has_iotlb_device needs to be updated when attaching to subdevices.

Fixes 67b8e02b5e761 ("iommu/vt-d: Aux-domain specific domain attach/detach")
---
  drivers/iommu/intel/iommu.c | 63 +++++++++++++++++++++++++++----------
  1 file changed, 47 insertions(+), 16 deletions(-)
drivers/iommu/intel/iommu.c +5 -0
index acfe0a5b955e..e97c5ac1d7fc 100644
@@ -726,6 +726,8 @@ static int domain_update_device_node(struct dmar_domain *domain)
  	return nid;
  }
 
+static void domain_update_iotlb(struct dmar_domain *domain);
+
  /* Some capabilities may be different across iommus */
  static void domain_update_iommu_cap(struct dmar_domain *domain)
  {
@@ -739,6 +741,8 @@ static void domain_update_iommu_cap(struct dmar_domain *domain)
  	 */
  	if (domain->nid == NUMA_NO_NODE)
  		domain->nid = domain_update_device_node(domain);
+
+	domain_update_iotlb(domain);
  }
 
  struct context_entry *iommu_context_addr(struct intel_iommu *iommu, u8 bus,
@@ -1459,6 +1463,18 @@ iommu_support_dev_iotlb (struct dmar_domain *domain, struct intel_iommu *iommu,
  	return NULL;
  }
 
+static bool dev_iotlb_enabled(struct device_domain_info *info)

drivers/iommu/intel/iommu.c iommu_support_dev_iotlb (struct dmar_domain *domain, struct intel_iommu *iommu, · line 1476

Quoted 10 lines
drivers/iommu/intel/iommu.c +10 -0
+{
+	struct pci_dev *pdev;
+
+	if (!info->dev || !dev_is_pci(info->dev))
+		return false;
+
+	pdev = to_pci_dev(info->dev);
+
+	return !!pdev->ats_enabled;
+}

I know this is just separated from below function. But isn't "(info && info->ats_enabled)" is enough?

Quoted 17 lines
drivers/iommu/intel/iommu.c +2 -8
+
  static void domain_update_iotlb(struct dmar_domain *domain)
  {
  	struct device_domain_info *info;
@@ -1466,17 +1482,20 @@ static void domain_update_iotlb(struct dmar_domain *domain)
 
  	assert_spin_locked(&device_domain_lock);
 
-	list_for_each_entry(info, &domain->devices, link) {
-		struct pci_dev *pdev;
-
-		if (!info->dev || !dev_is_pci(info->dev))
-			continue;
-
-		pdev = to_pci_dev(info->dev);
-		if (pdev->ats_enabled) {
+	list_for_each_entry(info, &domain->devices, link)

drivers/iommu/intel/iommu.c static void domain_update_iotlb(struct dmar_domain *domain) · line 1495

Quoted 10 lines
drivers/iommu/intel/iommu.c +7 -0
+		if (dev_iotlb_enabled(info)) {
  			has_iotlb_device = true;
  			break;
  		}
+
+	if (!has_iotlb_device) {
+		struct subdev_domain_info *sinfo;
+
+		list_for_each_entry(sinfo, &domain->subdevices, link_domain)
+			if (dev_iotlb_enabled(get_domain_info(sinfo->pdev))) {

Please make the code easier for reading by:

			info = get_domain_info(sinfo->pdev);
			if (dev_iotlb_enabled(info))
				....

Best regards,
baolu

Quoted 43 lines
drivers/iommu/intel/iommu.c +20 -4
+				has_iotlb_device = true;
+				break;
+			}
  	}
 
  	domain->has_iotlb_device = has_iotlb_device;
@@ -1557,25 +1576,37 @@ static void iommu_disable_dev_iotlb(struct device_domain_info *info)
  #endif
  }
 
+static void __iommu_flush_dev_iotlb(struct device_domain_info *info,
+				    u64 addr, unsigned int mask)
+{
+	u16 sid, qdep;
+
+	if (!info || !info->ats_enabled)
+		return;
+
+	sid = info->bus << 8 | info->devfn;
+	qdep = info->ats_qdep;
+	qi_flush_dev_iotlb(info->iommu, sid, info->pfsid,
+			   qdep, addr, mask);
+}
+
  static void iommu_flush_dev_iotlb(struct dmar_domain *domain,
  				  u64 addr, unsigned mask)
  {
-	u16 sid, qdep;
  	unsigned long flags;
  	struct device_domain_info *info;
+	struct subdev_domain_info *sinfo;
 
  	if (!domain->has_iotlb_device)
  		return;
 
  	spin_lock_irqsave(&device_domain_lock, flags);
-	list_for_each_entry(info, &domain->devices, link) {
-		if (!info->ats_enabled)
-			continue;
+	list_for_each_entry(info, &domain->devices, link)
+		__iommu_flush_dev_iotlb(info, addr, mask);
 

drivers/iommu/intel/iommu.c static void iommu_disable_dev_iotlb(struct device_domain_info *info) · line 1612

Quoted 10 lines
drivers/iommu/intel/iommu.c +3 -4
-		sid = info->bus << 8 | info->devfn;
-		qdep = info->ats_qdep;
-		qi_flush_dev_iotlb(info->iommu, sid, info->pfsid,
-				qdep, addr, mask);
+	list_for_each_entry(sinfo, &domain->subdevices, link_domain) {
+		__iommu_flush_dev_iotlb(get_domain_info(sinfo->pdev),
+					addr, mask);
  	}
  	spin_unlock_irqrestore(&device_domain_lock, flags);
  }

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

author
#5 lore raw

RE: [PATCH v2 3/3] iommu/vt-d: Fix ineffective devTLB invalidation for subdevices

Hi Baolu,

Well received, all comments accepted. thanks.

Regards,
Yi Liu

Quoted 147 lines

From: Lu Baolu <[email protected]>
Sent: Wednesday, December 23, 2020 6:10 PM

Hi Yi,

Quoted 7 lines · Liu Yi L

iommu_flush_dev_iotlb() is called to invalidate caches on device. It only loops the devices which are full-attached to the domain. For sub-devices, this is ineffective. This results in invalid caching entries left on the device. Fix it by adding loop for subdevices as well. Also, the domain-> has_iotlb_device needs to be updated when attaching to subdevices.

Fixes 67b8e02b5e761 ("iommu/vt-d: Aux-domain specific domain

attach/detach")

Quoted 3 lines
---
  drivers/iommu/intel/iommu.c | 63 +++++++++++++++++++++++++++---------

-

drivers/iommu/intel/iommu.c static int domain_update_device_node(struct

Quoted 7 lines
  1 file changed, 47 insertions(+), 16 deletions(-)
drivers/iommu/intel/iommu.c +0 -0
index acfe0a5b955e..e97c5ac1d7fc 100644
@@ -726,6 +726,8 @@ static int domain_update_device_node(struct

dmar_domain *domain)

drivers/iommu/intel/iommu.c static void domain_update_iommu_cap(struct

Quoted 9 lines
drivers/iommu/intel/iommu.c +2 -0
  	return nid;
  }
 
+static void domain_update_iotlb(struct dmar_domain *domain);
+
  /* Some capabilities may be different across iommus */
  static void domain_update_iommu_cap(struct dmar_domain *domain)
  {
@@ -739,6 +741,8 @@ static void domain_update_iommu_cap(struct

dmar_domain *domain)

drivers/iommu/intel/iommu.c static void domain_update_iommu_cap(struct dmar_domain *domain)

Quoted 8 lines
drivers/iommu/intel/iommu.c +2 -0
  	 */
  	if (domain->nid == NUMA_NO_NODE)
  		domain->nid = domain_update_device_node(domain);
+
+	domain_update_iotlb(domain);
  }
 
  struct context_entry *iommu_context_addr(struct intel_iommu *iommu,

u8 bus,

drivers/iommu/intel/iommu.c iommu_support_dev_iotlb (struct dmar_domain

Quoted 1 line
drivers/iommu/intel/iommu.c +0 -0
@@ -1459,6 +1463,18 @@ iommu_support_dev_iotlb (struct dmar_domain

*domain, struct intel_iommu *iommu,

Quoted 4 lines
drivers/iommu/intel/iommu.c +1 -0
  	return NULL;
  }
 
+static bool dev_iotlb_enabled(struct device_domain_info *info)

drivers/iommu/intel/iommu.c iommu_support_dev_iotlb (struct dmar_domain *domain, struct intel_iommu *iommu, · line 1476

Quoted 10 lines
drivers/iommu/intel/iommu.c +10 -0
+{
+	struct pci_dev *pdev;
+
+	if (!info->dev || !dev_is_pci(info->dev))
+		return false;
+
+	pdev = to_pci_dev(info->dev);
+
+	return !!pdev->ats_enabled;
+}

I know this is just separated from below function. But isn't "(info && info->ats_enabled)" is enough?

drivers/iommu/intel/iommu.c static void domain_update_iotlb(struct

Quoted 5 lines
drivers/iommu/intel/iommu.c +1 -0
+
  static void domain_update_iotlb(struct dmar_domain *domain)
  {
  	struct device_domain_info *info;
@@ -1466,17 +1482,20 @@ static void domain_update_iotlb(struct

dmar_domain *domain)

Quoted 12 lines
drivers/iommu/intel/iommu.c +1 -8
  	assert_spin_locked(&device_domain_lock);
 
-	list_for_each_entry(info, &domain->devices, link) {
-		struct pci_dev *pdev;
-
-		if (!info->dev || !dev_is_pci(info->dev))
-			continue;
-
-		pdev = to_pci_dev(info->dev);
-		if (pdev->ats_enabled) {
+	list_for_each_entry(info, &domain->devices, link)

drivers/iommu/intel/iommu.c static void domain_update_iotlb(struct dmar_domain *domain)

Quoted 10 lines
drivers/iommu/intel/iommu.c +7 -0
+		if (dev_iotlb_enabled(info)) {
  			has_iotlb_device = true;
  			break;
  		}
+
+	if (!has_iotlb_device) {
+		struct subdev_domain_info *sinfo;
+
+		list_for_each_entry(sinfo, &domain->subdevices, link_domain)
+			if (dev_iotlb_enabled(get_domain_info(sinfo->pdev)))

{

Please make the code easier for reading by:

			info = get_domain_info(sinfo->pdev);
			if (dev_iotlb_enabled(info))
				....

Best regards,
baolu

drivers/iommu/intel/iommu.c static void iommu_disable_dev_iotlb(struct

Quoted 7 lines
drivers/iommu/intel/iommu.c +3 -0
+				has_iotlb_device = true;
+				break;
+			}
  	}
 
  	domain->has_iotlb_device = has_iotlb_device;
@@ -1557,25 +1576,37 @@ static void iommu_disable_dev_iotlb(struct

device_domain_info *info)

Quoted 35 lines
drivers/iommu/intel/iommu.c +17 -4
  #endif
  }
 
+static void __iommu_flush_dev_iotlb(struct device_domain_info *info,
+				    u64 addr, unsigned int mask)
+{
+	u16 sid, qdep;
+
+	if (!info || !info->ats_enabled)
+		return;
+
+	sid = info->bus << 8 | info->devfn;
+	qdep = info->ats_qdep;
+	qi_flush_dev_iotlb(info->iommu, sid, info->pfsid,
+			   qdep, addr, mask);
+}
+
  static void iommu_flush_dev_iotlb(struct dmar_domain *domain,
  				  u64 addr, unsigned mask)
  {
-	u16 sid, qdep;
  	unsigned long flags;
  	struct device_domain_info *info;
+	struct subdev_domain_info *sinfo;
 
  	if (!domain->has_iotlb_device)
  		return;
 
  	spin_lock_irqsave(&device_domain_lock, flags);
-	list_for_each_entry(info, &domain->devices, link) {
-		if (!info->ats_enabled)
-			continue;
+	list_for_each_entry(info, &domain->devices, link)
+		__iommu_flush_dev_iotlb(info, addr, mask);
 
Quoted 11 lines

drivers/iommu/intel/iommu.c static void iommu_disable_dev_iotlb(struct device_domain_info *info) · line 1612

Quoted 11 lines
drivers/iommu/intel/iommu.c +3 -4
-		sid = info->bus << 8 | info->devfn;
-		qdep = info->ats_qdep;
-		qi_flush_dev_iotlb(info->iommu, sid, info->pfsid,
-				qdep, addr, mask);
+	list_for_each_entry(sinfo, &domain->subdevices, link_domain) {
+		__iommu_flush_dev_iotlb(get_domain_info(sinfo->pdev),
+					addr, mask);
  	}
  	spin_unlock_irqrestore(&device_domain_lock, flags);
  }

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