Skip to contentSkip to conversation

iommu/vt-d: Fix kdump kernels boot failure with scalable mode

PATCH In review Lu Baolu 2 participants 6 messages Aug 17, 2022 – Aug 23, 2022

Subsystems (3)
lore
Share & tools
lore

Lu Baolu

author maintainer
#0 lore raw

PATCH v2 1/1 iommu/vt-d: Fix kdump kernels boot failure with scalable mode

The translation table copying code for kdump kernels is currently based on the extended root/context entry formats of ECS mode defined in older VT-d v2.5, and doesn't handle the scalable mode formats. This causes the kexec capture kernel boot failure with DMAR faults if the IOMMU was enabled in scalable mode by the previous kernel.

The ECS mode has already been deprecated by the VT-d spec since v3.0 and Intel IOMMU driver doesn't support this mode as there's no real hardware implementation. Hence this converts ECS checking in copying table code into scalable mode.

The existing copying code consumes a bit in the context entry as a mark of copied entry. This marker needs to work for the old format as well as for extended context entries. It's hard to find such a bit for both legacy and scalable mode context entries. This replaces it with a per- IOMMU bitmap.

Fixes 7373a8cc38197 ("iommu/vt-d: Setup context and enable RID2PASID support")
---
 drivers/iommu/intel/iommu.h   | 17 ++++++--
 drivers/iommu/intel/debugfs.c |  3 +-
 drivers/iommu/intel/iommu.c   | 76 +++++++++--------------------------
 3 files changed, 35 insertions(+), 61 deletions(-)

Change log:
v2:
- Fix a compile error reported by 0day robot:
https://lore.kernel.org/linux-iommu/[email protected]/

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

drivers/iommu/intel/iommu.h +14 -3
index fae45bbb0c7f..0f7ea8559c34 100644
@@ -197,7 +197,6 @@
 #define ecap_dis(e)		(((e) >> 27) & 0x1)
 #define ecap_nest(e)		(((e) >> 26) & 0x1)
 #define ecap_mts(e)		(((e) >> 25) & 0x1)
-#define ecap_ecs(e)		(((e) >> 24) & 0x1)
 #define ecap_iotlb_offset(e) 	((((e) >> 8) & 0x3ff) * 16)
 #define ecap_max_iotlb_offset(e) (ecap_iotlb_offset(e) + 16)
 #define ecap_coherent(e)	((e) & 0x1)
@@ -265,7 +264,6 @@
 #define DMA_GSTS_CFIS (((u32)1) << 23)
 
 /* DMA_RTADDR_REG */
-#define DMA_RTADDR_RTT (((u64)1) << 11)
 #define DMA_RTADDR_SMT (((u64)1) << 10)
 
 /* CCMD_REG */
@@ -594,6 +592,7 @@ struct intel_iommu {
 	unsigned char iopfq_name[16];
 	struct q_inval  *qi;            /* Queued invalidation info */
 	u32 *iommu_state; /* Store iommu states between suspend and resume.*/
+	unsigned long *copied_tables; /* bitmap of copied tables */
 
 #ifdef CONFIG_IRQ_REMAP
 	struct ir_table *ir_table;	/* Interrupt remapping info */
@@ -701,6 +700,19 @@ static inline int nr_pte_to_next_page(struct dma_pte *pte)
 		(struct dma_pte *)ALIGN((unsigned long)pte, VTD_PAGE_SIZE) - pte;
 }
 
+static inline bool context_copied(struct intel_iommu *iommu, u8 bus, u8 devfn)
+{
+	if (!iommu->copied_tables)
+		return false;
+
+	return test_bit(((long)bus << 8) | devfn, iommu->copied_tables);
+}
+
+static inline bool context_present(struct context_entry *context)
+{
+	return (context->lo & 1);
+}
+
 extern struct dmar_drhd_unit * dmar_find_matched_drhd_unit(struct pci_dev *dev);
 
 extern int dmar_enable_qi(struct intel_iommu *iommu);
@@ -784,7 +796,6 @@ static inline void intel_iommu_debugfs_init(void) {}
 #endif /* CONFIG_INTEL_IOMMU_DEBUGFS */
 
 extern const struct attribute_group *intel_iommu_groups[];
-bool context_present(struct context_entry *context);
 struct context_entry *iommu_context_addr(struct intel_iommu *iommu, u8 bus,
 					 u8 devfn, int alloc);
 
drivers/iommu/intel/debugfs.c +2 -1
index 1f925285104e..f4fd249daad9 100644
@@ -241,7 +241,8 @@ static void ctx_tbl_walk(struct seq_file *m, struct intel_iommu *iommu, u16 bus)
 		if (!context)
 			return;
 
-		if (!context_present(context))
+		if (!context_present(context) ||
+		    context_copied(iommu, bus, devfn))
 			continue;
 
 		tbl_wlk.bus = bus;
drivers/iommu/intel/iommu.c +19 -57

136 lines not on this page · raw message

Signature · 1 line
2.25.1
#1 lore raw
Quoted 9 lines

From: Lu Baolu <[email protected]>
Sent: Wednesday, August 17, 2022 9:11 AM

The translation table copying code for kdump kernels is currently based on the extended root/context entry formats of ECS mode defined in older VT-d v2.5, and doesn't handle the scalable mode formats. This causes the kexec capture kernel boot failure with DMAR faults if the IOMMU was enabled in scalable mode by the previous kernel.

Quoted 8 lines

The ECS mode has already been deprecated by the VT-d spec since v3.0 and Intel IOMMU driver doesn't support this mode as there's no real hardware implementation. Hence this converts ECS checking in copying table code into scalable mode.

The existing copying code consumes a bit in the context entry as a mark of copied entry. This marker needs to work for the old format as well as for extended context entries. It's hard to find such a bit for both

The 2nd sentence "This marker..." is misleading. better removed.

Quoted 9 lines

legacy and scalable mode context entries. This replaces it with a per- IOMMU bitmap.

Fixes 7373a8cc38197 ("iommu/vt-d: Setup context and enable RID2PASID

support")

...

drivers/iommu/intel/iommu.c static int copy_translation_tables(struct · line 2698

Quoted 9 lines
drivers/iommu/intel/iommu.c +2 -2
@@ -2735,8 +2693,8 @@ static int copy_translation_tables(struct
 intel_iommu *iommu)
 	bool new_ext, ext;
 
 	rtaddr_reg = dmar_readq(iommu->reg + DMAR_RTADDR_REG);
-	ext        = !!(rtaddr_reg & DMA_RTADDR_RTT);
-	new_ext    = !!ecap_ecs(iommu->ecap);
+	ext        = !!(rtaddr_reg & DMA_RTADDR_SMT);
+	new_ext    = !!ecap_smts(iommu->ecap);

should be !!sm_supported()

Quoted 14 lines
drivers/iommu/intel/iommu.c +4 -0
 	/*
 	 * The RTT bit can only be changed when translation is disabled,
@@ -2747,6 +2705,10 @@ static int copy_translation_tables(struct
 intel_iommu *iommu)
 	if (new_ext != ext)
 		return -EINVAL;
 
+	iommu->copied_tables = bitmap_zalloc(BIT_ULL(16), GFP_KERNEL);
+	if (!iommu->copied_tables)
+		return -ENOMEM;
+
 	old_rt_phys = rtaddr_reg & VTD_PAGE_MASK;
 	if (!old_rt_phys)
Quoted 1 line

return -EINVAL;

Out of curiosity. What is the rationale that we copy root table and context tables but not pasid tables?

author maintainer
#2 lore raw
Quoted 14 lines · Kevin
Quoted 14 lines

From: Lu Baolu <[email protected]>
Sent: Wednesday, August 17, 2022 9:11 AM

The translation table copying code for kdump kernels is currently based on the extended root/context entry formats of ECS mode defined in older VT-d v2.5, and doesn't handle the scalable mode formats. This causes the kexec capture kernel boot failure with DMAR faults if the IOMMU was enabled in scalable mode by the previous kernel.

The ECS mode has already been deprecated by the VT-d spec since v3.0 and Intel IOMMU driver doesn't support this mode as there's no real hardware implementation. Hence this converts ECS checking in copying table code into scalable mode.

Quoted 5 lines
Quoted 3 lines

The existing copying code consumes a bit in the context entry as a mark of copied entry. This marker needs to work for the old format as well as for extended context entries. It's hard to find such a bit for both

The 2nd sentence "This marker..." is misleading. better removed.

Okay. I will make it like "It needs to work for ...".

Quoted 15 lines
Quoted 10 lines

legacy and scalable mode context entries. This replaces it with a per- IOMMU bitmap.

Fixes 7373a8cc38197 ("iommu/vt-d: Setup context and enable RID2PASID

support")

...

Quoted 4 lines
drivers/iommu/intel/iommu.c +0 -0
@@ -2735,8 +2693,8 @@ static int copy_translation_tables(struct
 intel_iommu *iommu)
  	bool new_ext, ext;
 
Quoted 7 lines

drivers/iommu/intel/iommu.c static int copy_translation_tables(struct intel_iommu *iommu) · line 2697

Quoted 5 lines
drivers/iommu/intel/iommu.c +2 -2
  	rtaddr_reg = dmar_readq(iommu->reg + DMAR_RTADDR_REG);
-	ext        = !!(rtaddr_reg & DMA_RTADDR_RTT);
-	new_ext    = !!ecap_ecs(iommu->ecap);
+	ext        = !!(rtaddr_reg & DMA_RTADDR_SMT);
+	new_ext    = !!ecap_smts(iommu->ecap);

should be !!sm_supported()

Not really. The IOMMU was setup by the previous kernel. Here we just check whether the scalable mode was enabled there.

Quoted 9 lines
Quoted 9 lines
drivers/iommu/intel/iommu.c +0 -0
  	/*
  	 * The RTT bit can only be changed when translation is disabled,
@@ -2747,6 +2705,10 @@ static int copy_translation_tables(struct
 intel_iommu *iommu)
  	if (new_ext != ext)
  		return -EINVAL;
 
Quoted 10 lines
Quoted 7 lines
drivers/iommu/intel/iommu.c +4 -0
+	iommu->copied_tables = bitmap_zalloc(BIT_ULL(16), GFP_KERNEL);
+	if (!iommu->copied_tables)
+		return -ENOMEM;
+
  	old_rt_phys = rtaddr_reg & VTD_PAGE_MASK;
  	if (!old_rt_phys)

return -EINVAL;

Out of curiosity. What is the rationale that we copy root table and context tables but not pasid tables?

We only copy the context table and reconstruct it when the default domain is attached. Before that, there's no need to reconstruct the pasid table, hence it's safe to use the previous pasid tables.

Best regards,
baolu

#3 lore raw
Quoted 10 lines

From: Baolu Lu <[email protected]>
Sent: Thursday, August 18, 2022 7:13 PM

Quoted 7 lines

drivers/iommu/intel/iommu.c static int copy_translation_tables(struct intel_iommu *iommu) · line 2697

Quoted 5 lines
  	rtaddr_reg = dmar_readq(iommu->reg + DMAR_RTADDR_REG);
drivers/iommu/intel/iommu.c +2 -2
-	ext        = !!(rtaddr_reg & DMA_RTADDR_RTT);
-	new_ext    = !!ecap_ecs(iommu->ecap);
+	ext        = !!(rtaddr_reg & DMA_RTADDR_SMT);
+	new_ext    = !!ecap_smts(iommu->ecap);

should be !!sm_supported()

Quoted 2 lines

Not really. The IOMMU was setup by the previous kernel. Here we just check whether the scalable mode was enabled there.

You want to compare whether old kernel and new kernel enable the same mode. ecap_smts is only about the capability. only sm_supported() can tell the mode which is actually used by the new kernel.

Quoted 21 lines
Quoted 10 lines
Quoted 10 lines

/*
* The RTT bit can only be changed when translation is disabled,

drivers/iommu/intel/iommu.c +0 -0
@@ -2747,6 +2705,10 @@ static int copy_translation_tables(struct
 intel_iommu *iommu)
  	if (new_ext != ext)
  		return -EINVAL;
 
Quoted 10 lines
Quoted 7 lines
drivers/iommu/intel/iommu.c +4 -0
+	iommu->copied_tables = bitmap_zalloc(BIT_ULL(16), GFP_KERNEL);
+	if (!iommu->copied_tables)
+		return -ENOMEM;
+
  	old_rt_phys = rtaddr_reg & VTD_PAGE_MASK;
  	if (!old_rt_phys)

return -EINVAL;

Out of curiosity. What is the rationale that we copy root table and context tables but not pasid tables?

Quoted 4 lines

We only copy the context table and reconstruct it when the default domain is attached. Before that, there's no need to reconstruct the pasid table, hence it's safe to use the previous pasid tables.

I still didn't get why context table must be reconstructed but not pasid table...

author maintainer
#4 lore raw
Quoted 10 lines · Kevin
Quoted 10 lines

From: Baolu Lu <[email protected]>
Sent: Thursday, August 18, 2022 7:13 PM

Quoted 7 lines

drivers/iommu/intel/iommu.c static int copy_translation_tables(struct intel_iommu *iommu) · line 2697

Quoted 5 lines
   	rtaddr_reg = dmar_readq(iommu->reg + DMAR_RTADDR_REG);
drivers/iommu/intel/iommu.c +2 -2
-	ext        = !!(rtaddr_reg & DMA_RTADDR_RTT);
-	new_ext    = !!ecap_ecs(iommu->ecap);
+	ext        = !!(rtaddr_reg & DMA_RTADDR_SMT);
+	new_ext    = !!ecap_smts(iommu->ecap);

should be !!sm_supported()

Quoted 7 lines
Quoted 2 lines

Not really. The IOMMU was setup by the previous kernel. Here we just check whether the scalable mode was enabled there.

You want to compare whether old kernel and new kernel enable the same mode. ecap_smts is only about the capability. only sm_supported() can tell the mode which is actually used by the new kernel.

Oh, yes! You are right. I will update this.

Quoted 22 lines
Quoted 22 lines
Quoted 21 lines
Quoted 17 lines

/*
* The RTT bit can only be changed when translation is disabled,

drivers/iommu/intel/iommu.c +4 -0
@@ -2747,6 +2705,10 @@ static int copy_translation_tables(struct
 intel_iommu *iommu)
   	if (new_ext != ext)
   		return -EINVAL;
 
+	iommu->copied_tables = bitmap_zalloc(BIT_ULL(16), GFP_KERNEL);
+	if (!iommu->copied_tables)
+		return -ENOMEM;
+
   	old_rt_phys = rtaddr_reg & VTD_PAGE_MASK;
   	if (!old_rt_phys)
Quoted 1 line
   		return -EINVAL;

Out of curiosity. What is the rationale that we copy root table and context tables but not pasid tables?

Quoted 7 lines
Quoted 4 lines

We only copy the context table and reconstruct it when the default domain is attached. Before that, there's no need to reconstruct the pasid table, hence it's safe to use the previous pasid tables.

I still didn't get why context table must be reconstructed but not pasid table...

The pasid table is also reconstructed. The context table entry and the pasid tables are reconstructed together, hence there's no need to copy the pasid table.

Best regards,
baolu

author maintainer
#5 lore raw
In reply to Lu Baolu
Quoted 17 lines · Lu Baolu

The translation table copying code for kdump kernels is currently based on the extended root/context entry formats of ECS mode defined in older VT-d v2.5, and doesn't handle the scalable mode formats. This causes the kexec capture kernel boot failure with DMAR faults if the IOMMU was enabled in scalable mode by the previous kernel.

The ECS mode has already been deprecated by the VT-d spec since v3.0 and Intel IOMMU driver doesn't support this mode as there's no real hardware implementation. Hence this converts ECS checking in copying table code into scalable mode.

The existing copying code consumes a bit in the context entry as a mark of copied entry. This marker needs to work for the old format as well as for extended context entries. It's hard to find such a bit for both legacy and scalable mode context entries. This replaces it with a per- IOMMU bitmap.

Quoted 5 lines
Fixes 7373a8cc38197 ("iommu/vt-d: Setup context and enable RID2PASID support")

This patch has been queued:

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

Best regards,
baolu