Skip to contentSkip to conversation

PCI: Ensure ATS disabled via quirk before notifying IOMMU drivers

PATCH New David Matlack 2 participants 2 messages Nov 10 PCI

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 PCI: Ensure ATS disabled via quirk before notifying IOMMU drivers

Apply the quirks that disable ATS before IOMMU drivers are notified about the device. Convert the quirks from final to header fixups and have them set a new no_ats bit in struct pci_dev, which keeps pci_ats_init() from setting pci_dev.ats_cap.

Header fixups run in pci_device_add() before pci_init_capabilities() initializes ATS and before device_add() notifies IOMMU drivers. Final fixups run later, in pci_bus_add_device(). Use header fixups instead of early fixups because quirk_amd_harvest_no_ats() needs subsystem_vendor and subsystem_device, which are not yet set during early fixups.

This ensures that pci_ats_supported() returns the same value in iommu_ops.probe_device() and iommu_ops.release_device(). Since commit 89436f4f5412 ("iommu/vt-d: Fix WARN_ON in iommu probe path"), the Intel IOMMU driver relies on this to decide whether to add a device to, and remove it from, iommu->device_rbtree. With final fixups, pci_ats_supported() returns true at probe but false at release, so the device is never removed from the rbtree, leading to a later use-after-free.

This only affects PCI devices added after IOMMU bus notifiers are set up, e.g. hot-plugged devices and VFs.

Fixes a18615b1cfc0 ("PCI: Disable ATS for specific Intel IPU E2000 devices")
Fixes 9b44b0b09dec ("PCI: Mark AMD Stoney GPU ATS as broken")
Fixes 3f1271b54edc ("PCI: Mark all AMD Navi10 and Navi14 GPU ATS as broken")

---
v5:
- Rebase onto v7.4-rc2
- Add Lu Baolu's Reviewed-by tag
- Reword commit message to explain fixup ordering and fix typos

v4: https://lore.kernel.org/linux-pci/[email protected]/
v3: https://lore.kernel.org/linux-pci/[email protected]/
v2: https://lore.kernel.org/linux-pci/[email protected]/
v1: https://lore.kernel.org/linux-pci/[email protected]/

 drivers/pci/ats.c    |  2 +-
 drivers/pci/quirks.c | 50 ++++++++++++++++++++++----------------------
 include/linux/pci.h  |  1 +
 3 files changed, 27 insertions(+), 26 deletions(-)
drivers/pci/ats.c +1 -1
index 9cb23780093d..af0257790016 100644
@@ -21,7 +21,7 @@ void pci_ats_init(struct pci_dev *dev)
 {
 	int pos;
 
-	if (pci_ats_disabled())
+	if (pci_ats_disabled() || dev->no_ats)
 		return;
 
 	pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ATS);
drivers/pci/quirks.c +25 -25

72 lines not on this page · raw message

include/linux/pci.h +1 -0
index aa3765a20a96..f125dac17757 100644
@@ -551,6 +551,7 @@ struct pci_dev {
 	};
 	u16		ats_cap;	/* ATS Capability offset */
 	u8		ats_stu;	/* ATS Smallest Translation Unit */
+	u8		no_ats:1;	/* ATS disabled via quirk */
 #endif
 #ifdef CONFIG_PCI_PRI
 	u16		pri_cap;	/* PRI Capability offset */

base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb

Signature · 1 line
2.56.0.506.g506a7eabc5-goog
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 20 lines

Apply the quirks that disable ATS before IOMMU drivers are notified about the device. Convert the quirks from final to header fixups and have them set a new no_ats bit in struct pci_dev, which keeps pci_ats_init() from setting pci_dev.ats_cap.

Header fixups run in pci_device_add() before pci_init_capabilities() initializes ATS and before device_add() notifies IOMMU drivers. Final fixups run later, in pci_bus_add_device(). Use header fixups instead of early fixups because quirk_amd_harvest_no_ats() needs subsystem_vendor and subsystem_device, which are not yet set during early fixups.

This ensures that pci_ats_supported() returns the same value in iommu_ops.probe_device() and iommu_ops.release_device(). Since commit 89436f4f5412 ("iommu/vt-d: Fix WARN_ON in iommu probe path"), the Intel IOMMU driver relies on this to decide whether to add a device to, and remove it from, iommu->device_rbtree. With final fixups, pci_ats_supported() returns true at probe but false at release, so the device is never removed from the rbtree, leading to a later use-after-free.

Quoted 8 lines

This only affects PCI devices added after IOMMU bus notifiers are set up, e.g. hot-plugged devices and VFs.

Fixes a18615b1cfc0 ("PCI: Disable ATS for specific Intel IPU E2000 devices")
Fixes 9b44b0b09dec ("PCI: Mark AMD Stoney GPU ATS as broken")
Fixes 3f1271b54edc ("PCI: Mark all AMD Navi10 and Navi14 GPU ATS as broken")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1