Skip to contentSkip to conversation

IOMMU: Prevent VT-d device IOTLB operations on wrong IOMMU

PATCH In review Malcolm Crossley 2 participants 6 messages Jun 17, 2014 – Jun 18, 2014

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 IOMMU: Prevent VT-d device IOTLB operations on wrong IOMMU

PCIe ATS allows for devices to contain IOTLBs, the VT-d code was iterating around all ATS capable devices and issuing IOTLB operations for all IOMMUs, even though each ATS device is only accessible via one particular IOMMU.

Issuing an IOMMU operation to a device not accessible via that IOMMU results in an IOMMU timeout because the device does not reply. VT-d IOMMU timeouts result in a Xen panic.

Therefore this bug prevents any Intel system with 2 or more ATS enabled IOMMUs, each with an ATS device connected to them, from booting Xen.

The patch adds a IOMMU pointer to the ATS device struct so the VT-d code can ensure it does not issue IOMMU ATS operations on the wrong IOMMU. A void pointer has to be used because AMD and Intel IOMMU implementations do not have a common IOMMU structure or indexing mechanism.

xen/drivers/passthrough/amd/pci_amd_iommu.c +1 -1
diff -r 4708591d8aa8 -r 0ec71ff08df1 xen/drivers/passthrough/amd/pci_amd_iommu.c
@@ -164,7 +164,7 @@ static void amd_iommu_setup_domain_devic
          !pci_ats_enabled(iommu->seg, bus, pdev->devfn) )
     {
         if ( devfn == pdev->devfn )
-            enable_ats_device(iommu->seg, bus, devfn);
+            enable_ats_device(iommu, iommu->seg, bus, devfn);
 
         amd_iommu_flush_iotlb(devfn, pdev, INV_IOMMU_ALL_PAGES_ADDRESS, 0);
     }
xen/drivers/passthrough/ats.h +2 -1
diff -r 4708591d8aa8 -r 0ec71ff08df1 xen/drivers/passthrough/ats.h
@@ -24,6 +24,7 @@ struct pci_ats_dev {
     u8 bus;
     u8 devfn;
     u16 ats_queue_depth;    /* ATS device invalidation queue depth */
+    const void *iommu;      /* No common IOMMU struct so use void pointer */
 };
 
 #define ATS_REG_CAP    4
@@ -34,7 +35,7 @@ struct pci_ats_dev {
 extern struct list_head ats_devices;
 extern bool_t ats_enabled;
 
-int enable_ats_device(int seg, int bus, int devfn);
+int enable_ats_device(const void *iommu, int seg, int bus, int devfn);
 void disable_ats_device(int seg, int bus, int devfn);
 struct pci_ats_dev *get_ats_device(int seg, int bus, int devfn);
 
xen/drivers/passthrough/vtd/iommu.c +2 -2
diff -r 4708591d8aa8 -r 0ec71ff08df1 xen/drivers/passthrough/vtd/iommu.c
@@ -1442,7 +1442,7 @@ static int domain_context_mapping(
         ret = domain_context_mapping_one(domain, drhd->iommu, bus, devfn,
                                          pdev);
         if ( !ret && devfn == pdev->devfn && ats_device(pdev, drhd) > 0 )
-            enable_ats_device(seg, bus, devfn);
+            enable_ats_device(drhd->iommu, seg, bus, devfn);
 
         break;
 
@@ -1930,7 +1930,7 @@ static int intel_iommu_enable_device(str
     if ( ret <= 0 )
         return ret;
 
-    ret = enable_ats_device(pdev->seg, pdev->bus, pdev->devfn);
+    ret = enable_ats_device(drhd->iommu, pdev->seg, pdev->bus, pdev->devfn);
 
     return ret >= 0 ? 0 : ret;
 }
xen/drivers/passthrough/vtd/x86/ats.c +4 -0
diff -r 4708591d8aa8 -r 0ec71ff08df1 xen/drivers/passthrough/vtd/x86/ats.c
@@ -120,6 +120,10 @@ int dev_invalidate_iotlb(struct iommu *i
     {
         sid = (pdev->bus << 8) | pdev->devfn;
 
+        /* Only invalidate devices that belong to this IOMMU */
+        if ( !pdev->iommu || pdev->iommu != iommu )
+            continue;
+
         switch ( type ) {
         case DMA_TLB_DSI_FLUSH:
             if ( !device_in_domain(iommu, pdev, did) )
xen/drivers/passthrough/x86/ats.c +2 -1
diff -r 4708591d8aa8 -r 0ec71ff08df1 xen/drivers/passthrough/x86/ats.c
@@ -23,7 +23,7 @@ LIST_HEAD(ats_devices);
 bool_t __read_mostly ats_enabled = 1;
 boolean_param("ats", ats_enabled);
 
-int enable_ats_device(int seg, int bus, int devfn)
+int enable_ats_device(const void *iommu, int seg, int bus, int devfn)
 {
     struct pci_ats_dev *pdev = NULL;
     u32 value;
@@ -66,6 +66,7 @@ int enable_ats_device(int seg, int bus, 
         pdev->seg = seg;
         pdev->bus = bus;
         pdev->devfn = devfn;
+        pdev->iommu = iommu;
         value = pci_conf_read16(seg, bus, PCI_SLOT(devfn),
                                 PCI_FUNC(devfn), pos + ATS_REG_CAP);
         pdev->ats_queue_depth = value & ATS_QUEUE_DEPTH_MASK ?:
#1 lore raw

xen/drivers/passthrough/vtd/x86/ats.c int dev_invalidate_iotlb(struct iommu *i · line 125

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 17.06.14 at 16:37, <[email protected]> wrote:
xen/drivers/passthrough/vtd/x86/ats.c +3 -0
@@ -120,6 +120,10 @@ int dev_invalidate_iotlb(struct iommu *i
     {
         sid = (pdev->bus << 8) | pdev->devfn;
 
+        /* Only invalidate devices that belong to this IOMMU */
+        if ( !pdev->iommu || pdev->iommu != iommu )
+            continue;

I meant to ask before and then forgot: What is the first half of this condition good/needed for?

Jan

#2 lore raw
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 17.06.14 at 16:37, <[email protected]> wrote:
xen/drivers/passthrough/vtd/x86/ats.c +0 -0
@@ -120,6 +120,10 @@ int dev_invalidate_iotlb(struct iommu *i
     {
         sid = (pdev->bus << 8) | pdev->devfn;
 
Quoted 7 lines

xen/drivers/passthrough/vtd/x86/ats.c int dev_invalidate_iotlb(struct iommu *i · line 125

Quoted 3 lines
xen/drivers/passthrough/vtd/x86/ats.c +3 -0
+        /* Only invalidate devices that belong to this IOMMU */
+        if ( !pdev->iommu || pdev->iommu != iommu )
+            continue;

I meant to ask before and then forgot: What is the first half of this condition good/needed for?

Defensive coding to prevent a NULL pointer deference if the Intel or core ATS code goes wrong.

Malcolm

Quoted 2 lines

Jan

#3 lore raw
Quoted 16 lines
Quoted 1 line
Quoted 1 line

Malcolm Crossley <[email protected]> 06/17/14 6:15 PM >>>

Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 17.06.14 at 16:37, <[email protected]> wrote:
xen/drivers/passthrough/vtd/x86/ats.c +0 -0
@@ -120,6 +120,10 @@ int dev_invalidate_iotlb(struct iommu *i
     {
         sid = (pdev->bus << 8) | pdev->devfn;
 
Quoted 7 lines

xen/drivers/passthrough/vtd/x86/ats.c int dev_invalidate_iotlb(struct iommu *i · line 125

Quoted 3 lines
xen/drivers/passthrough/vtd/x86/ats.c +3 -0
+        /* Only invalidate devices that belong to this IOMMU */
+        if ( !pdev->iommu || pdev->iommu != iommu )
+            continue;

I meant to ask before and then forgot: What is the first half of this condition good/needed for?

Quoted 2 lines

Defensive coding to prevent a NULL pointer deference if the Intel or core ATS code goes wrong.

But there is no de-reference of the pointer anywhere - you added it just as a token for comparison purposes. If pdev->iommu was NULL, the right side comparison should still produce "false"...

Jan

#4 lore raw
Quoted 16 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line

Malcolm Crossley <[email protected]> 06/17/14 6:15 PM >>>

Quoted 14 lines · Jan Beulich

xen/drivers/passthrough/vtd/x86/ats.c int dev_invalidate_iotlb(struct iommu *i · line 125

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 17.06.14 at 16:37, <[email protected]> wrote:
xen/drivers/passthrough/vtd/x86/ats.c +3 -0
@@ -120,6 +120,10 @@ int dev_invalidate_iotlb(struct iommu *i
     {
         sid = (pdev->bus << 8) | pdev->devfn;
 
+        /* Only invalidate devices that belong to this IOMMU */
+        if ( !pdev->iommu || pdev->iommu != iommu )
+            continue;

I meant to ask before and then forgot: What is the first half of this condition good/needed for?

Quoted 7 lines
Quoted 2 lines

Defensive coding to prevent a NULL pointer deference if the Intel or core ATS code goes wrong.

But there is no de-reference of the pointer anywhere - you added it just as a token for comparison purposes. If pdev->iommu was NULL, the right side comparison should still produce "false"...

Your right, I'm going crazy and thinking I'm dereferencing the IOMMU pointer when I'm not. (A non submitted version of the code was looking at the index field of struct IOMMU) Do you want me to resubmit an updated patch or are you happy to fix it up on commit?

Malcolm

Quoted 2 lines

Jan

#5 lore raw
Quoted 5 lines
Quoted 1 line
Quoted 1 line

Malcolm Crossley <[email protected]> 06/18/14 12:24 PM >>>

Your right, I'm going crazy and thinking I'm dereferencing the IOMMU pointer when I'm not. (A non submitted version of the code was looking at the index field of struct IOMMU) Do you want me to resubmit an updated patch or are you happy to fix it up on commit?

No need to re-submit, I'll take care of this while committing.

Jan