Skip to contentSkip to conversation

iommu/dmar: ignore devices with out-of-spec domain number

PATCH Applied Daniel Drake 2 participants 2 messages Feb 11, 2020 – Feb 21, 2020 IOMMU

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iommu/dmar: ignore devices with out-of-spec domain number

VMD subdevices are created with a PCI domain ID of 0x10000 or higher.

These subdevices are also handled like all other PCI devices by dmar_pci_bus_notifier().

However, when dmar_alloc_pci_notify_info() take records of such devices, it will truncate the domain ID to a u16 value (in info->seg). The device at (e.g.) 10000:00:02.0 is then treated by the DMAR code as if it is 0000:00:02.0.

In the unlucky event that a real device also exists at 0000:00:02.0 and also has a device-specific entry in the DMAR table, dmar_insert_dev_scope() will crash on:
  BUG_ON(i >= devices_cnt);

That's basically a sanity check that only one PCI device matches a single DMAR entry; in this case we seem to have two matching devices.

Fix this by ignoring devices that have a domain number higher than what can be looked up in the DMAR table.

This problem was carefully diagnosed by Jian-Hong Pan.

---

Notes:
This problem was detected with a non-upstream patch "PCI: Add Intel remapped NVMe device support"
(https://marc.info/?l=linux-ide&m=156015271021615&w=2)

This patch creates PCI devices in the same way as VMD, and hence I believe VMD would hit the same problem that we encountered here, when a VMD-using product comes along that meets the mentioned conditions.

However this hasn't actually been tested on VMD (don't have the hardware) so if I've missed anything and/or it's not a real issue then feel free to drop this patch.

 drivers/iommu/dmar.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/iommu/dmar.c +8 -0
index 071bb42bbbc5..8f94c817a7b5 100644
@@ -28,6 +28,7 @@
 #include <linux/slab.h>
 #include <linux/iommu.h>
 #include <linux/numa.h>
+#include <linux/limits.h>
 #include <asm/irq_remapping.h>
 #include <asm/iommu_table.h>
 
@@ -128,6 +129,13 @@ dmar_alloc_pci_notify_info(struct pci_dev *dev, unsigned long event)
 
 	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
+	 */
+	if (pci_domain_nr(dev->bus) > U16_MAX)
+		return NULL;
+
 	/* Only generate path[] for device addition event */
 	if (event == BUS_NOTIFY_ADD_DEVICE)
 		for (tmp = dev; tmp; tmp = tmp->bus->self)
Signature · 6 lines
2.20.1

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Applied
#1 lore raw

Hi,

Quoted 19 lines · Daniel Drake

VMD subdevices are created with a PCI domain ID of 0x10000 or higher.

These subdevices are also handled like all other PCI devices by dmar_pci_bus_notifier().

However, when dmar_alloc_pci_notify_info() take records of such devices, it will truncate the domain ID to a u16 value (in info->seg). The device at (e.g.) 10000:00:02.0 is then treated by the DMAR code as if it is 0000:00:02.0.

In the unlucky event that a real device also exists at 0000:00:02.0 and also has a device-specific entry in the DMAR table, dmar_insert_dev_scope() will crash on:
  BUG_ON(i >= devices_cnt);

That's basically a sanity check that only one PCI device matches a single DMAR entry; in this case we seem to have two matching devices.

Quoted 6 lines

Fix this by ignoring devices that have a domain number higher than what can be looked up in the DMAR table.

This problem was carefully diagnosed by Jian-Hong Pan.

Queued for v5.7. Thanks!

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