Skip to contentSkip to conversation

PCI: Ensure ATS disabled via quirk before notifying IOMMU drivers

PATCH Superseded David Matlack 2 participants 3 messages Apr 3 – Jun 12 PCI

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

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

Ensure that PCI devices always have ATS disable via quirk before IOMMU drivers are notified about the device. Fix this by converting the existing quirks from final to header fixups and changing the quirk logic to set a new no_ats bit in struct pci_dev that prevents pci_dev.ats_cap from ever getting set.

Use header fixups instead of early fixups since not enough of struct pci_dev is set up in during early fixups: quirk_amd_harvest_no_ats() needs subsystem_device and subsystem_vendor to be set.

This change ensures that pci_ats_supported() always takes quirks into account during iommu_ops.probe_device(), when IOMMU drivers are notified about devices, and that pci_ats_supported() returns the same value when the device is released in iommu_ops.release_device().

Notably, the Intel IOMMU driver uses pci_ats_supported() in probe/release to determine whether to add/remove a device from a data structure, which easily leads to a use-after-free without this fix.

This change also makes disabling ATS via quirk behave the same way as the pci=noats command line option, in that pci_ats_init() bails immediately and never initializes pci_dev.ats_cap.

Note: In practice this fix only matters for PCI devices created 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")

---
v3:
- Clarify in commit message that only devices added after boot are affected (Baolu)
- Use header fixups instead of early to avoid breaking quirk_amd_harvest_no_ats() (Sashiko)
- Use u8 instead of unsigned int for no_ats to avoid affecting the offsets of existing fields in struct pci_dev (me)

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

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

Cc Raghavendra Rao Ananta <[email protected]>
Cc David Woodhouse <[email protected]>
Cc Lu Baolu <[email protected]>
Cc Andy Shevchenko <[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 ec6c8dbdc5e9..ceb6f5d3cb10 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 1c270f1d5123..f9729ee71a96 100644
@@ -539,6 +539,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: 7df48e36313029e4c0907b2023905dd7213fd678

Signature · 1 line
2.53.0.1213.gd9a14994de-goog
Reviewed-by Tested-by on v3 1/1
#1 lore raw
Quoted 47 lines · David Matlack

Ensure that PCI devices always have ATS disable via quirk before IOMMU drivers are notified about the device. Fix this by converting the existing quirks from final to header fixups and changing the quirk logic to set a new no_ats bit in struct pci_dev that prevents pci_dev.ats_cap from ever getting set.

Use header fixups instead of early fixups since not enough of struct pci_dev is set up in during early fixups: quirk_amd_harvest_no_ats() needs subsystem_device and subsystem_vendor to be set.

This change ensures that pci_ats_supported() always takes quirks into account during iommu_ops.probe_device(), when IOMMU drivers are notified about devices, and that pci_ats_supported() returns the same value when the device is released in iommu_ops.release_device().

Notably, the Intel IOMMU driver uses pci_ats_supported() in probe/release to determine whether to add/remove a device from a data structure, which easily leads to a use-after-free without this fix.

This change also makes disabling ATS via quirk behave the same way as the pci=noats command line option, in that pci_ats_init() bails immediately and never initializes pci_dev.ats_cap.

Note: In practice this fix only matters for PCI devices created 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")

---
v3:
- Clarify in commit message that only devices added after boot are affected (Baolu)
- Use header fixups instead of early to avoid breaking quirk_amd_harvest_no_ats() (Sashiko)
- Use u8 instead of unsigned int for no_ats to avoid affecting the offsets of existing fields in struct pci_dev (me)

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

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

Cc Raghavendra Rao Ananta <[email protected]>
Cc David Woodhouse <[email protected]>
Cc Lu Baolu <[email protected]>
Cc Andy Shevchenko <[email protected]>
Quoted 5 lines
 drivers/pci/ats.c    |  2 +-
 drivers/pci/quirks.c | 50 ++++++++++++++++++++++----------------------
 include/linux/pci.h  |  1 +
 3 files changed, 27 insertions(+), 26 deletions(-)

Applying ATS quirks during the HEADER phase seems correct for the IOMMU drivers to get the right view. I observed side effects due to the quirks being applied in the FINAL phase [1] which seem to go away with this fix.

Thanks,
Praan

[1] https://lore.kernel.org/all/[email protected]/

#2 lore raw
In reply to David Matlack
Quoted 24 lines · David Matlack

Ensure that PCI devices always have ATS disable via quirk before IOMMU drivers are notified about the device. Fix this by converting the existing quirks from final to header fixups and changing the quirk logic to set a new no_ats bit in struct pci_dev that prevents pci_dev.ats_cap from ever getting set.

Use header fixups instead of early fixups since not enough of struct pci_dev is set up in during early fixups: quirk_amd_harvest_no_ats() needs subsystem_device and subsystem_vendor to be set.

This change ensures that pci_ats_supported() always takes quirks into account during iommu_ops.probe_device(), when IOMMU drivers are notified about devices, and that pci_ats_supported() returns the same value when the device is released in iommu_ops.release_device().

Notably, the Intel IOMMU driver uses pci_ats_supported() in probe/release to determine whether to add/remove a device from a data structure, which easily leads to a use-after-free without this fix.

This change also makes disabling ATS via quirk behave the same way as the pci=noats command line option, in that pci_ats_init() bails immediately and never initializes pci_dev.ats_cap.

Quoted 6 lines

Note: In practice this fix only matters for PCI devices created 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")

Hi Bjorn,

Sorry for the ping but I wanted to double check if this one is still on your radar and if it can be picked up for a future release.

Thanks.