All of lore.kernel.org
 help / color / mirror / Atom feed
* [v3 0/7] iommu/vt-d: Several fixes for intel iommu driver
@ 2023-04-06  6:59 Tina Zhang
  2023-04-06  6:59 ` [v3 1/7] iommu/vt-d: Fix operand size in bitwise operation Tina Zhang
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: Tina Zhang @ 2023-04-06  6:59 UTC (permalink / raw)
  To: iommu; +Cc: Lu Baolu, Tina Zhang

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/20230331024236.2716001-1-tina.zhang@intel.com/
    
v1:
  https://lore.kernel.org/linux-iommu/20230329124654.2698853-1-tina.zhang@intel.com/

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(-)

-- 
2.34.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [v3 1/7] iommu/vt-d: Fix operand size in bitwise operation
  2023-04-06  6:59 [v3 0/7] iommu/vt-d: Several fixes for intel iommu driver Tina Zhang
@ 2023-04-06  6:59 ` Tina Zhang
  2023-04-06  6:59 ` [v3 2/7] iommu/vt-d: Remove BUG_ON on checking valid pfn range Tina Zhang
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Tina Zhang @ 2023-04-06  6:59 UTC (permalink / raw)
  To: iommu; +Cc: Lu Baolu, Tina Zhang, Yongwei Ma

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.

Reported-by: Yongwei Ma <yongwei.ma@intel.com>
Signed-off-by: Tina Zhang <tina.zhang@intel.com>
---
 drivers/iommu/intel/dmar.c  | 2 +-
 drivers/iommu/intel/iommu.c | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/iommu/intel/dmar.c b/drivers/iommu/intel/dmar.c
index 23828d189c2a6..f0f51c957ccbc 100644
--- a/drivers/iommu/intel/dmar.c
+++ b/drivers/iommu/intel/dmar.c
@@ -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);
 
diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
index 7c2f4bd335823..5d6018b819590 100644
--- a/drivers/iommu/intel/iommu.c
+++ b/drivers/iommu/intel/iommu.c
@@ -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. */
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [v3 2/7] iommu/vt-d: Remove BUG_ON on checking valid pfn range
  2023-04-06  6:59 [v3 0/7] iommu/vt-d: Several fixes for intel iommu driver Tina Zhang
  2023-04-06  6:59 ` [v3 1/7] iommu/vt-d: Fix operand size in bitwise operation Tina Zhang
@ 2023-04-06  6:59 ` Tina Zhang
  2023-04-06  6:59 ` [v3 3/7] iommu/vt-d: Remove BUG_ON in handling iotlb cache invalidation Tina Zhang
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Tina Zhang @ 2023-04-06  6:59 UTC (permalink / raw)
  To: iommu; +Cc: Lu Baolu, 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.

Signed-off-by: Tina Zhang <tina.zhang@intel.com>
---
 drivers/iommu/intel/iommu.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
index 5d6018b819590..1426df19e7607 100644
--- a/drivers/iommu/intel/iommu.c
+++ b/drivers/iommu/intel/iommu.c
@@ -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),
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [v3 3/7] iommu/vt-d: Remove BUG_ON in handling iotlb cache invalidation
  2023-04-06  6:59 [v3 0/7] iommu/vt-d: Several fixes for intel iommu driver Tina Zhang
  2023-04-06  6:59 ` [v3 1/7] iommu/vt-d: Fix operand size in bitwise operation Tina Zhang
  2023-04-06  6:59 ` [v3 2/7] iommu/vt-d: Remove BUG_ON on checking valid pfn range Tina Zhang
@ 2023-04-06  6:59 ` Tina Zhang
  2023-04-06  6:59 ` [v3 4/7] iommu/vt-d: Remove BUG_ON when domain->pgd is NULL Tina Zhang
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Tina Zhang @ 2023-04-06  6:59 UTC (permalink / raw)
  To: iommu; +Cc: Lu Baolu, 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.

Signed-off-by: Tina Zhang <tina.zhang@intel.com>
---
 drivers/iommu/intel/iommu.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
index 1426df19e7607..de7fe7dcbc5ce 100644
--- a/drivers/iommu/intel/iommu.c
+++ b/drivers/iommu/intel/iommu.c
@@ -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;
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [v3 4/7] iommu/vt-d: Remove BUG_ON when domain->pgd is NULL
  2023-04-06  6:59 [v3 0/7] iommu/vt-d: Several fixes for intel iommu driver Tina Zhang
                   ` (2 preceding siblings ...)
  2023-04-06  6:59 ` [v3 3/7] iommu/vt-d: Remove BUG_ON in handling iotlb cache invalidation Tina Zhang
@ 2023-04-06  6:59 ` Tina Zhang
  2023-04-06  6:59 ` [v3 5/7] iommu/vt-d: Remove BUG_ON in map/unmap() Tina Zhang
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Tina Zhang @ 2023-04-06  6:59 UTC (permalink / raw)
  To: iommu; +Cc: Lu Baolu, 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.

Signed-off-by: Tina Zhang <tina.zhang@intel.com>
---
 drivers/iommu/intel/iommu.c | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
index de7fe7dcbc5ce..883991bd93e7a 100644
--- a/drivers/iommu/intel/iommu.c
+++ b/drivers/iommu/intel/iommu.c
@@ -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);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [v3 5/7] iommu/vt-d: Remove BUG_ON in map/unmap()
  2023-04-06  6:59 [v3 0/7] iommu/vt-d: Several fixes for intel iommu driver Tina Zhang
                   ` (3 preceding siblings ...)
  2023-04-06  6:59 ` [v3 4/7] iommu/vt-d: Remove BUG_ON when domain->pgd is NULL Tina Zhang
@ 2023-04-06  6:59 ` Tina Zhang
  2023-04-06  6:59 ` [v3 6/7] iommu/vt-d: Remove a useless BUG_ON(dev->is_virtfn) Tina Zhang
  2023-04-06  6:59 ` [v3 7/7] iommu/vt-d: Remove BUG_ON in dmar_insert_dev_scope() Tina Zhang
  6 siblings, 0 replies; 8+ messages in thread
From: Tina Zhang @ 2023-04-06  6:59 UTC (permalink / raw)
  To: iommu; +Cc: Lu Baolu, Tina Zhang

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

Signed-off-by: Tina Zhang <tina.zhang@intel.com>
---
 drivers/iommu/intel/iommu.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
index 883991bd93e7a..c5c8d19695a84 100644
--- a/drivers/iommu/intel/iommu.c
+++ b/drivers/iommu/intel/iommu.c
@@ -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);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [v3 6/7] iommu/vt-d: Remove a useless BUG_ON(dev->is_virtfn)
  2023-04-06  6:59 [v3 0/7] iommu/vt-d: Several fixes for intel iommu driver Tina Zhang
                   ` (4 preceding siblings ...)
  2023-04-06  6:59 ` [v3 5/7] iommu/vt-d: Remove BUG_ON in map/unmap() Tina Zhang
@ 2023-04-06  6:59 ` Tina Zhang
  2023-04-06  6:59 ` [v3 7/7] iommu/vt-d: Remove BUG_ON in dmar_insert_dev_scope() Tina Zhang
  6 siblings, 0 replies; 8+ messages in thread
From: Tina Zhang @ 2023-04-06  6:59 UTC (permalink / raw)
  To: iommu; +Cc: Lu Baolu, 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().

Signed-off-by: Tina Zhang <tina.zhang@intel.com>
---
 drivers/iommu/intel/dmar.c | 2 --
 1 file changed, 2 deletions(-)

diff --git a/drivers/iommu/intel/dmar.c b/drivers/iommu/intel/dmar.c
index f0f51c957ccbc..9346c6e7ebae9 100644
--- a/drivers/iommu/intel/dmar.c
+++ b/drivers/iommu/intel/dmar.c
@@ -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
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [v3 7/7] iommu/vt-d: Remove BUG_ON in dmar_insert_dev_scope()
  2023-04-06  6:59 [v3 0/7] iommu/vt-d: Several fixes for intel iommu driver Tina Zhang
                   ` (5 preceding siblings ...)
  2023-04-06  6:59 ` [v3 6/7] iommu/vt-d: Remove a useless BUG_ON(dev->is_virtfn) Tina Zhang
@ 2023-04-06  6:59 ` Tina Zhang
  6 siblings, 0 replies; 8+ messages in thread
From: Tina Zhang @ 2023-04-06  6:59 UTC (permalink / raw)
  To: iommu; +Cc: Lu Baolu, 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.

Signed-off-by: Tina Zhang <tina.zhang@intel.com>
---
 drivers/iommu/intel/dmar.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/iommu/intel/dmar.c b/drivers/iommu/intel/dmar.c
index 9346c6e7ebae9..e35be3786bde6 100644
--- a/drivers/iommu/intel/dmar.c
+++ b/drivers/iommu/intel/dmar.c
@@ -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;
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2023-04-06  7:31 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-06  6:59 [v3 0/7] iommu/vt-d: Several fixes for intel iommu driver Tina Zhang
2023-04-06  6:59 ` [v3 1/7] iommu/vt-d: Fix operand size in bitwise operation Tina Zhang
2023-04-06  6:59 ` [v3 2/7] iommu/vt-d: Remove BUG_ON on checking valid pfn range Tina Zhang
2023-04-06  6:59 ` [v3 3/7] iommu/vt-d: Remove BUG_ON in handling iotlb cache invalidation Tina Zhang
2023-04-06  6:59 ` [v3 4/7] iommu/vt-d: Remove BUG_ON when domain->pgd is NULL Tina Zhang
2023-04-06  6:59 ` [v3 5/7] iommu/vt-d: Remove BUG_ON in map/unmap() Tina Zhang
2023-04-06  6:59 ` [v3 6/7] iommu/vt-d: Remove a useless BUG_ON(dev->is_virtfn) Tina Zhang
2023-04-06  6:59 ` [v3 7/7] iommu/vt-d: Remove BUG_ON in dmar_insert_dev_scope() Tina Zhang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.