Skip to contentSkip to conversation

iommu/vt-d: Serialize IOMMU GCMD register modifications

PATCH Superseded Lu Baolu 2 participants 3 messages Aug 27, 2020 – Aug 28, 2020

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Lu Baolu

author maintainer
#0 lore raw

PATCH v2 1/1 iommu/vt-d: Serialize IOMMU GCMD register modifications

The VT-d spec requires (10.4.4 Global Command Register, GCMD_REG General Description) that:

If multiple control fields in this register need to be modified, software must serialize the modifications through multiple writes to this register.

However, in irq_remapping.c, modifications of IRE and CFI are done in one write. We need to do two separate writes with STS checking after each.

Fixes af8d102f999a4 ("x86/intel/irq_remapping: Clean up x2apic opt-out security warning mess")
Cc Andy Lutomirski <[email protected]>
Cc Jacob Pan <[email protected]>
Cc Kevin Tian <[email protected]>
Cc Ashok Raj <[email protected]>
---
 drivers/iommu/intel/irq_remapping.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

Change log:
v1->v2:
- v1 posted here

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

- Add status check before disabling CFI. (Kevin)

drivers/iommu/intel/irq_remapping.c +9 -2
index 9564d23d094f..7552bb7e92c8 100644
@@ -507,12 +507,19 @@ static void iommu_enable_irq_remapping(struct intel_iommu *iommu)
 
 	/* Enable interrupt-remapping */
 	iommu->gcmd |= DMA_GCMD_IRE;
-	iommu->gcmd &= ~DMA_GCMD_CFI;  /* Block compatibility-format MSIs */
 	writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
-
 	IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
 		      readl, (sts & DMA_GSTS_IRES), sts);
 
+	/* Block compatibility-format MSIs */
+	sts = readl(iommu->reg + DMAR_GSTS_REG);
+	if (sts & DMA_GSTS_CFIS) {
+		iommu->gcmd &= ~DMA_GCMD_CFI;
+		writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
+		IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
+			      readl, !(sts & DMA_GSTS_CFIS), sts);
+	}
+
 	/*
 	 * With CFI clear in the Global Command register, we should be
 	 * protected from dangerous (i.e. compatibility) interrupts
Signature · 1 line
2.17.1
#1 lore raw
Quoted 43 lines

From: Lu Baolu <[email protected]>
Sent: Thursday, August 27, 2020 12:25 PM

The VT-d spec requires (10.4.4 Global Command Register, GCMD_REG General
Description) that:

If multiple control fields in this register need to be modified, software must serialize the modifications through multiple writes to this register.

However, in irq_remapping.c, modifications of IRE and CFI are done in one write. We need to do two separate writes with STS checking after each.

Fixes af8d102f999a4 ("x86/intel/irq_remapping: Clean up x2apic opt-out

security warning mess")

Cc Andy Lutomirski <[email protected]>
Cc Jacob Pan <[email protected]>
Cc Kevin Tian <[email protected]>
Cc Ashok Raj <[email protected]>
---
 drivers/iommu/intel/irq_remapping.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

Change log:
v1->v2:
- v1 posted here

    https://lore.kernel.org/linux-iommu/20200826025825.2322-1-

[email protected]/;
- Add status check before disabling CFI. (Kevin)

drivers/iommu/intel/irq_remapping.c +0 -1
index 9564d23d094f..7552bb7e92c8 100644
@@ -507,12 +507,19 @@ static void iommu_enable_irq_remapping(struct
 intel_iommu *iommu)
 
 	/* Enable interrupt-remapping */
 	iommu->gcmd |= DMA_GCMD_IRE;
-	iommu->gcmd &= ~DMA_GCMD_CFI;  /* Block compatibility-format

MSIs */

drivers/iommu/intel/irq_remapping.c static void iommu_enable_irq_remapping(struct intel_iommu *iommu) · line 515

Quoted 7 lines
drivers/iommu/intel/irq_remapping.c +2 -1
 	writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
-
 	IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
 		      readl, (sts & DMA_GSTS_IRES), sts);
 
+	/* Block compatibility-format MSIs */
+	sts = readl(iommu->reg + DMAR_GSTS_REG);

no need of this readl as the status is already three in IOMMU_WAIT_OP.

Quoted 11 lines
drivers/iommu/intel/irq_remapping.c +7 -1
+	if (sts & DMA_GSTS_CFIS) {
+		iommu->gcmd &= ~DMA_GCMD_CFI;
+		writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
+		IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
+			      readl, !(sts & DMA_GSTS_CFIS), sts);
+	}
+
 	/*
 	 * With CFI clear in the Global Command register, we should be
 	 * protected from dangerous (i.e. compatibility) interrupts
--
Quoted 1 line

2.17.1

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

author maintainer
#2 lore raw

Hi,

Quoted 48 lines · Kevin
Quoted 48 lines

From: Lu Baolu <[email protected]>
Sent: Thursday, August 27, 2020 12:25 PM

The VT-d spec requires (10.4.4 Global Command Register, GCMD_REG General
Description) that:

If multiple control fields in this register need to be modified, software must serialize the modifications through multiple writes to this register.

However, in irq_remapping.c, modifications of IRE and CFI are done in one write. We need to do two separate writes with STS checking after each.

Fixes af8d102f999a4 ("x86/intel/irq_remapping: Clean up x2apic opt-out

security warning mess")

Cc Andy Lutomirski <[email protected]>
Cc Jacob Pan <[email protected]>
Cc Kevin Tian <[email protected]>
Cc Ashok Raj <[email protected]>
---
  drivers/iommu/intel/irq_remapping.c | 11 +++++++++--
  1 file changed, 9 insertions(+), 2 deletions(-)

Change log:
v1->v2:
- v1 posted here

     https://lore.kernel.org/linux-iommu/20200826025825.2322-1-

[email protected]/;
- Add status check before disabling CFI. (Kevin)

drivers/iommu/intel/irq_remapping.c +0 -1
index 9564d23d094f..7552bb7e92c8 100644
@@ -507,12 +507,19 @@ static void iommu_enable_irq_remapping(struct
 intel_iommu *iommu)
 
  	/* Enable interrupt-remapping */
  	iommu->gcmd |= DMA_GCMD_IRE;
-	iommu->gcmd &= ~DMA_GCMD_CFI;  /* Block compatibility-format

MSIs */
writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
-
IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
readl, (sts & DMA_GSTS_IRES), sts);

Quoted 4 lines

drivers/iommu/intel/irq_remapping.c static void iommu_enable_irq_remapping(struct intel_iommu *iommu) · line 515

Quoted 2 lines
drivers/iommu/intel/irq_remapping.c +2 -0
+	/* Block compatibility-format MSIs */
+	sts = readl(iommu->reg + DMAR_GSTS_REG);

no need of this readl as the status is already three in IOMMU_WAIT_OP.

Yes. Really!

Best regards,
baolu

Quoted 14 lines
Quoted 13 lines
drivers/iommu/intel/irq_remapping.c +7 -1
+	if (sts & DMA_GSTS_CFIS) {
+		iommu->gcmd &= ~DMA_GCMD_CFI;
+		writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
+		IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
+			      readl, !(sts & DMA_GSTS_CFIS), sts);
+	}
+
  	/*
  	 * With CFI clear in the Global Command register, we should be
  	 * protected from dangerous (i.e. compatibility) interrupts
--

2.17.1