Skip to contentSkip to conversation

iommu/vt-d: Several fixes for intel iommu driver

PATCH New Tina Zhang 1 participant 8 messages Apr 6, 2023

Subsystems (2)
lore
Share & tools
lore

Tina Zhang

author
#0 lore raw

v3 cover iommu/vt-d: Several fixes for intel iommu driver

This series includes several small fixes.

v3:
- Remove the unnecessary domain->pgd checkings.

v2:
- Remove two patches which are not necessary.
- Split the "Replace BUG()/BUG_ON() with WARN_ON/WARN_ON_ONCE()" patch into several small patches with each one addressing one issue.
- Use BIT_ULL in the "Fix operand size in bitwise operation" patch.
https://lore.kernel.org/linux-iommu/[email protected]/

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

Tina Zhang (7):
iommu/vt-d: Fix operand size in bitwise operation iommu/vt-d: Remove BUG_ON on checking valid pfn range iommu/vt-d: Remove BUG_ON in handling iotlb cache invalidation iommu/vt-d: Remove BUG_ON when domain->pgd is NULL iommu/vt-d: Remove BUG_ON in map/unmap()
iommu/vt-d: Remove a useless BUG_ON(dev->is_virtfn) iommu/vt-d: Remove BUG_ON in dmar_insert_dev_scope()

 drivers/iommu/intel/dmar.c  |  7 +++----
 drivers/iommu/intel/iommu.c | 39 ++++++++++++++++++++-----------------
 2 files changed, 24 insertions(+), 22 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v3 1/7 iommu/vt-d: Fix operand size in bitwise operation

The patch fixes the klocwork issues that operands in a bitwise operation have different size at line 1692 of dmar.c, line 1898 and line 1907 of iommu.c.

---
 drivers/iommu/intel/dmar.c  | 2 +-
 drivers/iommu/intel/iommu.c | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
drivers/iommu/intel/dmar.c +1 -1
index 23828d189c2a6..f0f51c957ccbc 100644
@@ -1690,7 +1690,7 @@ static void __dmar_enable_qi(struct intel_iommu *iommu)
 	 * is present.
 	 */
 	if (ecap_smts(iommu->ecap))
-		val |= (1 << 11) | 1;
+		val |= BIT_ULL(11) | BIT_ULL(0);
 
 	raw_spin_lock_irqsave(&iommu->register_lock, flags);
 
drivers/iommu/intel/iommu.c +2 -2
index 7c2f4bd335823..5d6018b819590 100644
@@ -1895,7 +1895,7 @@ context_set_sm_rid2pasid(struct context_entry *context, unsigned long pasid)
  */
 static inline void context_set_sm_dte(struct context_entry *context)
 {
-	context->lo |= (1 << 2);
+	context->lo |= BIT_ULL(2);
 }
 
 /*
@@ -1904,7 +1904,7 @@ static inline void context_set_sm_dte(struct context_entry *context)
  */
 static inline void context_set_sm_pre(struct context_entry *context)
 {
-	context->lo |= (1 << 4);
+	context->lo |= BIT_ULL(4);
 }
 
 /* Convert value to context PASID directory size field coding. */
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH v3 2/7 iommu/vt-d: Remove BUG_ON on checking valid pfn range

In reply to Tina Zhang

When encountering an unexpected invalid pfn range, the kernel should attempt recovery and proceed with execution. Therefore, using WARN_ON to replace BUG_ON to avoid halting the machine.

Besides, one redundant checking is reduced.

---
 drivers/iommu/intel/iommu.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/iommu/intel/iommu.c +6 -6
index 5d6018b819590..1426df19e7607 100644
@@ -1005,9 +1005,9 @@ static void dma_pte_clear_range(struct dmar_domain *domain,
 	unsigned int large_page;
 	struct dma_pte *first_pte, *pte;
 
-	BUG_ON(!domain_pfn_supported(domain, start_pfn));
-	BUG_ON(!domain_pfn_supported(domain, last_pfn));
-	BUG_ON(start_pfn > last_pfn);
+	if (WARN_ON(!domain_pfn_supported(domain, last_pfn)) ||
+	    WARN_ON(start_pfn > last_pfn))
+		return;
 
 	/* we don't need lock here; nobody else touches the iova range */
 	do {
@@ -1166,9 +1166,9 @@ static void dma_pte_clear_level(struct dmar_domain *domain, int level,
 static void domain_unmap(struct dmar_domain *domain, unsigned long start_pfn,
 			 unsigned long last_pfn, struct list_head *freelist)
 {
-	BUG_ON(!domain_pfn_supported(domain, start_pfn));
-	BUG_ON(!domain_pfn_supported(domain, last_pfn));
-	BUG_ON(start_pfn > last_pfn);
+	if (WARN_ON(!domain_pfn_supported(domain, last_pfn)) ||
+	    WARN_ON(start_pfn > last_pfn))
+		return;
 
 	/* we don't need lock here; nobody else touches the iova range */
 	dma_pte_clear_level(domain, agaw_to_level(domain->agaw),
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH v3 3/7 iommu/vt-d: Remove BUG_ON in handling iotlb cache invalidation

In reply to Tina Zhang

VT-d iotlb cache invalidation request with unexpected type is considered as a bug to developers, which can be fixed. So, when such kind of issue comes out, it needs to be reported through the kernel log, instead of halting the system. Replacing BUG_ON with warning reporting.

---
 drivers/iommu/intel/iommu.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/iommu/intel/iommu.c +8 -3
index 1426df19e7607..de7fe7dcbc5ce 100644
@@ -1272,7 +1272,9 @@ static void __iommu_flush_context(struct intel_iommu *iommu,
 			| DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
 		break;
 	default:
-		BUG();
+		pr_warn("%s: Unexpected context-cache invalidation type 0x%llx\n",
+			iommu->name, type);
+		return;
 	}
 	val |= DMA_CCMD_ICC;
 
@@ -1308,7 +1310,9 @@ static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
 		val_iva = size_order | addr;
 		break;
 	default:
-		BUG();
+		pr_warn("%s: Unexpected iotlb invalidation type 0x%llx\n",
+			iommu->name, type);
+		return;
 	}
 	/* Note: set drain read/write */
 #if 0
@@ -1508,7 +1512,8 @@ static void iommu_flush_iotlb_psi(struct intel_iommu *iommu,
 	uint64_t addr = (uint64_t)pfn << VTD_PAGE_SHIFT;
 	u16 did = domain_id_iommu(domain, iommu);
 
-	BUG_ON(pages == 0);
+	if (WARN_ON(!pages))
+		return;
 
 	if (ih)
 		ih = 1 << 6;
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH v3 4/7 iommu/vt-d: Remove BUG_ON when domain->pgd is NULL

In reply to Tina Zhang

When performing domain_context_mapping or getting dma_pte of a pfn, the availability of the domain page table directory is ensured. Therefore, the domain->pgd checkings are unnecessary.

---
 drivers/iommu/intel/iommu.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/iommu/intel/iommu.c +0 -4
index de7fe7dcbc5ce..883991bd93e7a 100644
@@ -915,8 +915,6 @@ static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain,
 	int level = agaw_to_level(domain->agaw);
 	int offset;
 
-	BUG_ON(!domain->pgd);
-
 	if (!domain_pfn_supported(domain, pfn))
 		/* Address beyond IOMMU's addressing capabilities. */
 		return NULL;
@@ -1935,8 +1933,6 @@ static int domain_context_mapping_one(struct dmar_domain *domain,
 	pr_debug("Set context mapping for %02x:%02x.%d\n",
 		bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
 
-	BUG_ON(!domain->pgd);
-
 	spin_lock(&iommu->lock);
 	ret = -ENOMEM;
 	context = iommu_context_addr(iommu, bus, devfn, 1);
Signature · 1 line
2.34.1
author
#5 lore raw

PATCH v3 5/7 iommu/vt-d: Remove BUG_ON in map/unmap()

In reply to Tina Zhang

Domain map/unmap with invalid parameters shouldn't crash the kernel. Therefore, using if() replaces the BUG_ON.

---
 drivers/iommu/intel/iommu.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/iommu/intel/iommu.c +5 -3
index 883991bd93e7a..c5c8d19695a84 100644
@@ -2184,7 +2184,8 @@ __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
 	phys_addr_t pteval;
 	u64 attr;
 
-	BUG_ON(!domain_pfn_supported(domain, iov_pfn + nr_pages - 1));
+	if (unlikely(!domain_pfn_supported(domain, iov_pfn + nr_pages - 1)))
+		return -EINVAL;
 
 	if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
 		return -EINVAL;
@@ -4341,8 +4342,9 @@ static size_t intel_iommu_unmap(struct iommu_domain *domain,
 
 	/* Cope with horrid API which requires us to unmap more than the
 	   size argument if it happens to be a large-page mapping. */
-	BUG_ON(!pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level,
-			       GFP_ATOMIC));
+	if (unlikely(!pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT,
+					 &level, GFP_ATOMIC)))
+		return 0;
 
 	if (size < VTD_PAGE_SIZE << level_to_offset_bits(level))
 		size = VTD_PAGE_SIZE << level_to_offset_bits(level);
Signature · 1 line
2.34.1
author
#6 lore raw

PATCH v3 6/7 iommu/vt-d: Remove a useless BUG_ON(dev->is_virtfn)

In reply to Tina Zhang

When dmar_alloc_pci_notify_info() is being invoked, the invoker has ensured the dev->is_virtfn is false. So, remove the useless BUG_ON in dmar_alloc_pci_notify_info().

---
 drivers/iommu/intel/dmar.c | 2 --
 1 file changed, 2 deletions(-)
drivers/iommu/intel/dmar.c +0 -2
index f0f51c957ccbc..9346c6e7ebae9 100644
@@ -127,8 +127,6 @@ dmar_alloc_pci_notify_info(struct pci_dev *dev, unsigned long event)
 	struct pci_dev *tmp;
 	struct dmar_pci_notify_info *info;
 
-	BUG_ON(dev->is_virtfn);
-
 	/*
 	 * Ignore devices that have a domain number higher than what can
 	 * be looked up in DMAR, e.g. VMD subdevices with domain 0x10000
Signature · 1 line
2.34.1
author
#7 lore raw

PATCH v3 7/7 iommu/vt-d: Remove BUG_ON in dmar_insert_dev_scope()

In reply to Tina Zhang

The dmar_insert_dev_scope() could fail if any unexpected condition is encountered. However, in this situation, the kernel should attempt recovery and proceed with execution. Remove BUG_ON with WARN_ON, so that kernel can avoid being crashed when an unexpected condition occurs.

---
 drivers/iommu/intel/dmar.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iommu/intel/dmar.c +2 -1
index 9346c6e7ebae9..e35be3786bde6 100644
@@ -262,7 +262,8 @@ int dmar_insert_dev_scope(struct dmar_pci_notify_info *info,
 						   get_device(dev));
 				return 1;
 			}
-		BUG_ON(i >= devices_cnt);
+		if (WARN_ON(i >= devices_cnt))
+			return -EINVAL;
 	}
 
 	return 0;
Signature · 1 line
2.34.1