PCI/ATS: Disable ATS when ACS TB is set upstream
v3 cover PCI/ATS: Disable ATS when ACS TB is set upstream
This series disables ATS on endpoints when an upstream bridge has ACS Translation Blocking (PCI_ACS_TB) enabled, preventing ACS Violations on translated DMA requests.
Patch 1/2 stops pci_dev_reset_iommu_done() from re-evaluating pci_ats_supported() and dev_has_iommu() independently from pci_dev_reset_iommu_prepare(), tracking whether state needs to be restored via a @restore output flag instead. It can be merged independently.
Patch 2/2 updates pci_ats_supported() to walk upstream bridges and return false if any bridge has PCI_ACS_TB set in PCI_ACS_CTRL.
Changes since v2:
- Add patch 1/2 to track IOMMU reset restore state between pci_dev_reset_iommu_prepare() and pci_dev_reset_iommu_done()
- Move the upstream ACS TB check back into pci_ats_supported(), dropping pci_ats_check_acs() and changes to pci-driver.c and pci.h
- Read PCI_ACS_CTRL directly and check the return value and PCI_POSSIBLE_ERROR() so inaccessible bridges are not treated as having PCI_ACS_TB set
- Remove the pci_warn_once() logging and helper function
Changes since v1 (v2, based on Sashiko review):
- Move the upstream ACS TB check from pci_ats_supported() into pci_ats_check_acs() called after pci_enable_acs()
- Use pci_acs_enabled() instead of reading PCI_ACS_CTRL directly
Danny Gonzalez (2):
iommu: Track whether pci_dev_reset_iommu_done() needs to restore state PCI/ATS: Disable ATS when ACS TB is set upstream
drivers/iommu/iommu.c | 14 ++++++++++---- drivers/pci/ats.c | 19 +++++++++++++++++++ drivers/pci/pci-acpi.c | 5 +++-- drivers/pci/pci.c | 25 +++++++++++++++---------- drivers/pci/quirks.c | 5 +++-- include/linux/iommu.h | 9 +++++---- 6 files changed, 55 insertions(+), 22 deletions(-)
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH v3 1/2 iommu: Track whether pci_dev_reset_iommu_done() needs to restore state
Currently, pci_dev_reset_iommu_done() relies on the assumption that pci_ats_supported() and dev_has_iommu() always return the same values they returned in pci_dev_reset_iommu_prepare(). While that may be true today, relying on both calls evaluating identically is fragile: if they ever return different values, gdev->reset_depth, group->recovery_cnt, group->domain, and gdev->blocked will get out of sync.
Have pci_dev_reset_iommu_prepare() track whether pci_dev_reset_iommu_done() needs to restore IOMMU state via a @restore output flag, and pass @restore to pci_dev_reset_iommu_done() instead of re-evaluating pci_ats_supported() and dev_has_iommu().
---
v3:
- New patch based on review feedback to avoid relying on pci_ats_supported() and dev_has_iommu() returning identical values across pci_dev_reset_iommu_prepare() and pci_dev_reset_iommu_done()
drivers/iommu/iommu.c | 14 ++++++++++---- drivers/pci/pci-acpi.c | 5 +++-- drivers/pci/pci.c | 25 +++++++++++++++---------- drivers/pci/quirks.c | 5 +++-- include/linux/iommu.h | 9 +++++---- 5 files changed, 36 insertions(+), 22 deletions(-)
drivers/iommu/iommu.c +10 -4
@@ -4010,6 +4010,7 @@ EXPORT_SYMBOL_NS_GPL(iommu_replace_group_handle, "IOMMUFD_INTERNAL");/** * pci_dev_reset_iommu_prepare() - Block IOMMU to prepare for a PCI device reset * @pdev: PCI device that is going to enter a reset routine +* @restore: set to true if pci_dev_reset_iommu_done() needs to restore state * * The PCIe r6.0, sec 10.3.1 IMPLEMENTATION NOTE recommends to disable and block * ATS before initiating a reset. This means that a PCIe device during the reset
@@ -4031,7 +4032,7 @@ EXPORT_SYMBOL_NS_GPL(iommu_replace_group_handle, "IOMMUFD_INTERNAL");* case, callers must ensure there will be no racy iommu_release_device() call, * which otherwise would UAF the dev->iommu_group pointer. */ -int pci_dev_reset_iommu_prepare (struct pci_dev *pdev) +int pci_dev_reset_iommu_prepare (struct pci_dev *pdev,bool *restore) {struct iommu_group *group = pdev->dev.iommu_group;struct group_device *gdev;
@@ -4039,8 +4040,12 @@ int pci_dev_reset_iommu_prepare(struct pci_dev *pdev)void *entry;int ret; -if (!pci_ats_supported (pdev) || !dev_has_iommu (&pdev->dev)) +if (!pci_ats_supported (pdev) || !dev_has_iommu (&pdev->dev)) { + *restore = false;return 0 ; + } + + *restore = true;guard (mutex)(&group->mutex);
@@ -4132,6 +4137,7 @@ static bool group_device_dma_alias_is_blocked(struct iommu_group *group,/** * pci_dev_reset_iommu_done() - Restore IOMMU after a PCI device reset is done * @pdev: PCI device that has finished a reset routine +* @restore: whether pci_dev_reset_iommu_prepare() blocked the IOMMU * * After a PCIe device finishes a reset routine, it wants to restore its IOMMU * activity, including new translation and cache invalidation, by re-attaching
@@ -4143,14 +4149,14 @@ static bool group_device_dma_alias_is_blocked(struct iommu_group *group,* Note that, although unlikely, there is a risk that re-attaching domains might * fail due to some unexpected happening like OOM. */ -void pci_dev_reset_iommu_done (struct pci_dev *pdev) +void pci_dev_reset_iommu_done (struct pci_dev *pdev,bool restore) {struct iommu_group *group = pdev->dev.iommu_group;struct group_device *gdev;unsigned long pasid;void *entry; -if (!pci_ats_supported (pdev) || !dev_has_iommu (&pdev->dev)) +if (!restore)return ;guard (mutex)(&group->mutex);
drivers/pci/pci-acpi.c +3 -2
@@ -955,6 +955,7 @@ void pci_set_acpi_fwnode(struct pci_dev *dev)int pci_dev_acpi_reset (struct pci_dev *dev,bool probe) {acpi_handle handle =ACPI_HANDLE (&dev->dev); +bool restore;int ret;if (!handle || !acpi_has_method (handle,"_RST" ))
@@ -963,7 +964,7 @@ int pci_dev_acpi_reset(struct pci_dev *dev, bool probe)if (probe)return 0 ; - ret =pci_dev_reset_iommu_prepare (dev); + ret =pci_dev_reset_iommu_prepare (dev, &restore);if (ret) {pci_err (dev,"failed to stop IOMMU for a PCI reset: %d\n" , ret);return ret;
@@ -974,7 +975,7 @@ int pci_dev_acpi_reset(struct pci_dev *dev, bool probe) ret = -ENOTTY ; } -pci_dev_reset_iommu_done (dev); +pci_dev_reset_iommu_done (dev, restore);return ret; }
drivers/pci/pci.c +15 -10
@@ -4440,6 +4440,7 @@ EXPORT_SYMBOL(pci_wait_for_pending_transaction);*/ int pcie_flr (struct pci_dev *dev) { +bool restore;int ret;if (!pci_dev_config_accessible (dev,"FLR" ))
@@ -4449,7 +4450,7 @@ int pcie_flr(struct pci_dev *dev)pci_err (dev,"timed out waiting for pending transaction; performing function level reset anyway\n" );/* Have to call it after waiting for pending DMA transaction */ - ret =pci_dev_reset_iommu_prepare (dev); + ret =pci_dev_reset_iommu_prepare (dev, &restore);if (ret) {pci_err (dev,"failed to stop IOMMU for a PCI reset: %d\n" , ret);return ret;
@@ -4469,7 +4470,7 @@ int pcie_flr(struct pci_dev *dev) ret =pci_dev_wait (dev,"FLR" ,PCIE_RESET_READY_POLL_MS ); done: -pci_dev_reset_iommu_done (dev); +pci_dev_reset_iommu_done (dev, restore);return ret; }EXPORT_SYMBOL_GPL (pcie_flr);
@@ -4498,6 +4499,7 @@ EXPORT_SYMBOL_GPL(pcie_reset_flr);static int pci_af_flr (struct pci_dev *dev,bool probe) { +bool restore;int ret;int pos;u8 cap;
@@ -4526,7 +4528,7 @@ static int pci_af_flr(struct pci_dev *dev, bool probe)pci_err (dev,"timed out waiting for pending transaction; performing AF function level reset anyway\n" );/* Have to call it after waiting for pending DMA transaction */ - ret =pci_dev_reset_iommu_prepare (dev); + ret =pci_dev_reset_iommu_prepare (dev, &restore);if (ret) {pci_err (dev,"failed to stop IOMMU for a PCI reset: %d\n" , ret);return ret;
@@ -4547,7 +4549,7 @@ static int pci_af_flr(struct pci_dev *dev, bool probe) ret =pci_dev_wait (dev,"AF_FLR" ,PCIE_RESET_READY_POLL_MS ); done: -pci_dev_reset_iommu_done (dev); +pci_dev_reset_iommu_done (dev, restore);return ret; }
@@ -4568,6 +4570,7 @@ static int pci_af_flr(struct pci_dev *dev, bool probe)*/ static int pci_pm_reset (struct pci_dev *dev,bool probe) { +bool restore;u16 csr;int ret;
@@ -4584,7 +4587,7 @@ static int pci_pm_reset(struct pci_dev *dev, bool probe)if (dev->current_state !=PCI_D0 )return -EINVAL ; - ret =pci_dev_reset_iommu_prepare (dev); + ret =pci_dev_reset_iommu_prepare (dev, &restore);if (ret) {pci_err (dev,"failed to stop IOMMU for a PCI reset: %d\n" , ret);return ret;
@@ -4601,7 +4604,7 @@ static int pci_pm_reset(struct pci_dev *dev, bool probe)pci_dev_d3_sleep (dev); ret =pci_dev_wait (dev,"PM D3hot->D0" ,PCIE_RESET_READY_POLL_MS ); -pci_dev_reset_iommu_done (dev); +pci_dev_reset_iommu_done (dev, restore);return ret; }
@@ -5033,6 +5036,7 @@ static bool cxl_sbr_masked(struct pci_dev *dev)static int pci_reset_bus_function (struct pci_dev *dev,bool probe) {struct pci_dev *bridge =pci_upstream_bridge (dev); +bool restore;int rc;/*
@@ -5042,7 +5046,7 @@ static int pci_reset_bus_function(struct pci_dev *dev, bool probe)if (bridge &&pcie_is_cxl (bridge) &&cxl_sbr_masked (bridge))return -ENOTTY ; - rc =pci_dev_reset_iommu_prepare (dev); + rc =pci_dev_reset_iommu_prepare (dev, &restore);if (rc) {pci_err (dev,"failed to stop IOMMU for a PCI reset: %d\n" , rc);return rc;
@@ -5054,7 +5058,7 @@ static int pci_reset_bus_function(struct pci_dev *dev, bool probe) rc =pci_parent_bus_reset (dev, probe); done: -pci_dev_reset_iommu_done (dev); +pci_dev_reset_iommu_done (dev, restore);return rc; }
@@ -5062,6 +5066,7 @@ static int cxl_reset_bus_function(struct pci_dev *dev, bool probe) {struct pci_dev *bridge;u16 dvsec, reg, val; +bool restore;int rc; bridge =pci_upstream_bridge (dev);
@@ -5079,7 +5084,7 @@ static int cxl_reset_bus_function(struct pci_dev *dev, bool probe)if (rc)return -ENOTTY ; - rc =pci_dev_reset_iommu_prepare (dev); + rc =pci_dev_reset_iommu_prepare (dev, &restore);if (rc) {pci_err (dev,"failed to stop IOMMU for a PCI reset: %d\n" , rc);return rc;
@@ -5099,7 +5104,7 @@ static int cxl_reset_bus_function(struct pci_dev *dev, bool probe)pci_write_config_word (bridge, dvsec +PCI_DVSEC_CXL_PORT_CTL , reg); -pci_dev_reset_iommu_done (dev); +pci_dev_reset_iommu_done (dev, restore);return rc; }
drivers/pci/quirks.c +3 -2
@@ -4404,16 +4404,17 @@ static const struct pci_dev_reset_methods pci_dev_reset_methods[] = {static int __pci_dev_specific_reset (struct pci_dev *dev,bool probe,const struct pci_dev_reset_methods *i) { +bool restore;int ret; - ret =pci_dev_reset_iommu_prepare (dev); + ret =pci_dev_reset_iommu_prepare (dev, &restore);if (ret) {pci_err (dev,"failed to stop IOMMU for a PCI reset: %d\n" , ret);return ret; } ret = i->reset (dev, probe); -pci_dev_reset_iommu_done (dev); +pci_dev_reset_iommu_done (dev, restore);return ret; }
include/linux/iommu.h +5 -4
@@ -1223,8 +1223,8 @@ ioasid_t iommu_alloc_global_pasid(struct device *dev);void iommu_free_global_pasid (ioasid_t pasid);/* PCI device reset functions */ -int pci_dev_reset_iommu_prepare (struct pci_dev *pdev); -void pci_dev_reset_iommu_done (struct pci_dev *pdev); +int pci_dev_reset_iommu_prepare (struct pci_dev *pdev,bool *restore); +void pci_dev_reset_iommu_done (struct pci_dev *pdev,bool restore);#else /* CONFIG_IOMMU_API */ struct iommu_ops {};
@@ -1549,12 +1549,13 @@ static inline ioasid_t iommu_alloc_global_pasid(struct device *dev)static inline void iommu_free_global_pasid (ioasid_t pasid) {} -static inline int pci_dev_reset_iommu_prepare (struct pci_dev *pdev) +static inline int pci_dev_reset_iommu_prepare (struct pci_dev *pdev,bool *restore) { + *restore = false;return 0 ; } -static inline void pci_dev_reset_iommu_done (struct pci_dev *pdev) +static inline void pci_dev_reset_iommu_done (struct pci_dev *pdev,bool restore) { }#endif /* CONFIG_IOMMU_API */
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
[email protected]
AI reviewRe: [PATCH v3 1/2] iommu: Track whether pci_dev_reset_iommu_done() needs to restore state
Sashiko has reviewed this patch and found no issues.
Quoted 7 lines
Currently,
pci_dev_reset_iommu_done()relies on the assumption thatpci_ats_supported()anddev_has_iommu()always return the same values they returned inpci_dev_reset_iommu_prepare(). While that may be true today, relying on both calls evaluating identically is fragile: if they ever return different values,gdev->reset_depth,group->recovery_cnt,group->domain, andgdev->blockedwill get out of sync.
Quoted 6 lines
Have
pci_dev_reset_iommu_prepare()track whetherpci_dev_reset_iommu_done()needs to restore IOMMU state via a @restore output flag, and pass @restore topci_dev_reset_iommu_done()instead of re-evaluatingpci_ats_supported()anddev_has_iommu().Signed-off-by Danny Gonzalez <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v3 2/2 PCI/ATS: Disable ATS when ACS TB is set upstream
In reply to Danny GonzalezEnabling ATS allows an endpoint to cache translations and initiate DMA requests with Translated addresses, which an upstream bridge with ACS Translation Blocking (PCI_ACS_TB) set will block as ACS Violations. Because pci_enable_acs() preserves existing PCI_ACS_CTRL bits (e.g., from firmware, a kexec kernel, or "pci=config_acs=") without relaxing them, PCI_ACS_TB may remain set on upstream bridges.
Update pci_ats_supported() to walk upstream bridges and return false if any bridge has PCI_ACS_TB set, leaving the endpoint to use Untranslated addresses. Ignore bridges whose config read fails or returns an error response (PCI_POSSIBLE_ERROR()) so inaccessible bridges are not treated as having PCI_ACS_TB set.
Tested by booting a machine with an endpoint advertising the ATS Capability behind a Root Port with PCI_ACS_TB set; verified ATS is disabled and the system boots cleanly.
---
v3:
- Move the upstream ACS TB check back into pci_ats_supported(), dropping pci_ats_check_acs() and changes to pci-driver.c and pci.h
- Read PCI_ACS_CTRL directly and check the return value and PCI_POSSIBLE_ERROR() so inaccessible bridges are not treated as having PCI_ACS_TB set
- Remove the pci_warn_once() logging and separate helper function
v2 (based on sashiko review):
- Move the upstream ACS TB check from pci_ats_supported() into pci_ats_check_acs() called after pci_enable_acs()
- Use pci_acs_enabled() instead of reading PCI_ACS_CTRL directly
drivers/pci/ats.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
drivers/pci/ats.c +19 -0
@@ -40,12 +40,31 @@ void pci_ats_init(struct pci_dev *dev)*/ bool pci_ats_supported (struct pci_dev *dev) { +struct pci_dev *bridge; +if (!dev->ats_cap || dev->untrusted)return false;if (dev->is_virtfn)return pci_ats_supported (pci_physfn (dev)); +/* Do not allow ATS if any upstream bridge blocks translation */ +for (bridge =pci_upstream_bridge (dev); bridge; + bridge =pci_upstream_bridge (bridge)) { +u16 ctrl; +int ret; + +if (!bridge->acs_cap) +continue ; + ret =pci_read_config_word (bridge, + bridge->acs_cap +PCI_ACS_CTRL , + &ctrl); +if (ret ||PCI_POSSIBLE_ERROR (ctrl)) +continue ; +if (ctrl &PCI_ACS_TB ) +return false; + } +return true; }EXPORT_SYMBOL_GPL (pci_ats_supported);
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 13 lines
Enabling ATS allows an endpoint to cache translations and initiate DMA requests with Translated addresses, which an upstream bridge with ACS Translation Blocking (
PCI_ACS_TB) set will block as ACS Violations. Becausepci_enable_acs()preserves existingPCI_ACS_CTRLbits (e.g., from firmware, a kexec kernel, or "pci=config_acs=") without relaxing them,PCI_ACS_TBmay remain set on upstream bridges.Update
pci_ats_supported()to walk upstream bridges and return false if any bridge hasPCI_ACS_TBset, leaving the endpoint to use Untranslated addresses. Ignore bridges whose config read fails or returns an error response (PCI_POSSIBLE_ERROR()) so inaccessible bridges are not treated as havingPCI_ACS_TBset.
Quoted 5 lines
Tested by booting a machine with an endpoint advertising the ATS Capability behind a Root Port with
PCI_ACS_TBset; verified ATS is disabled and the system boots cleanly.Signed-off-by Danny Gonzalez <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2