Re: [PATCH v2 2/3] PCI/PM: Do not save the config space of an inaccessible device
From: Bjorn Helgaas
Date: Thu Oct 08 2026 - 18:58:38 EST
[+cc Lukas, Farhan, Andreas, Mika, Yehezkel]
On Wed, Sep 30, 2026 at 11:19:13AM -0300, Francisco Beltrán Millalén wrote:
> pci_save_state() reads the standard header into dev->saved_config_space
> and marks it valid without checking that the device answered. If the
> device is not accessible, every read returns all ones, the previous
> snapshot is overwritten, and pci_restore_state() writes the all-ones
> values back once the device answers again. On a bridge that sets every
> writable bit of the Bridge Control register, Secondary Bus Reset
> included, and sets the primary, secondary and subordinate bus numbers
> to 0xff, which cuts off everything below it.
>
> On a MacBookPro14,3 this happens to the upstream bridge of a
> Thunderbolt controller that drops off the bus while the system is
> suspending: after resume the bridge answers again, but with bus numbers
> ff/ff/ff and Secondary Bus Reset asserted, and the xHCI controllers
> behind it are removed.
Apparently this is a reproducible issue on MacBookPro14,3. That makes
me a little hesitant because we're not actually dealing with the fact
that the Thunderbolt controller isn't responsive during suspend.
It seems worthwhile to me to skip pci_save_state() if the device isn't
accessible, but I don't think it's a real solution to whatever is
going on with Thunderbolt, and I don't think we should mention it here
as though it is.
> Commit e18d1abc3bff ("PCI: Avoid saving config space state if
> inaccessible") added pci_dev_config_accessible() and checks it before a
> reset. Check it in pci_save_state() itself, so that system suspend,
> where the state is saved by pci_pm_suspend_noirq() or by a driver's
> suspend_noirq callback, is covered as well. pci_dev_config_accessible()
> reads the Command and Status registers rather than the Vendor and Device
> IDs, which always read as all ones on SR-IOV VFs.
>
> Check again after reading, as the device may become inaccessible in the
> meantime, and only then replace the previous snapshot of the header and
> set state_saved. The capabilities are saved straight into their own
> buffers and are not covered by the second check, and both checks are
> racy, as pci_dev_config_accessible() notes.
>
> If the device is not accessible, return -EIO and leave state_saved as it
> was.
>
> Assisted-by: LLM
> Signed-off-by: Francisco Beltrán Millalén <fbeltranmillalen@xxxxxxxxx>
> ---
> v2:
> - Use pci_dev_config_accessible() instead of reading the Vendor ID,
> which always reads as all ones on SR-IOV VFs: v1 refused to save the
> state of every VF. In the v1 thread I said I would use
> pci_device_is_present(), but for a VF that checks the PF and cannot
> tell whether the VF itself answers.
> - Do the second check after saving the capabilities, and set
> state_saved only if both checks pass.
> - The bus numbers are set to 0xff, not cleared.
>
> drivers/pci/pci.c | 53 +++++++++++++++++++++++++++++++++++++----------------
> 1 file changed, 37 insertions(+), 16 deletions(-)
>
> diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
> index b2879a6be..94de99d31 100644
> --- a/drivers/pci/pci.c
> +++ b/drivers/pci/pci.c
> @@ -1776,31 +1776,52 @@ static void pci_restore_pcix_state(struct pci_dev *dev)
> * pci_save_state - save the PCI configuration space of a device before
> * suspending
> * @dev: PCI device that we're dealing with
> + *
> + * If the config space of @dev is not accessible, nothing is saved and the
> + * previous snapshot of the standard header is kept, as writing back the
> + * all-ones values read from such a device would corrupt it once it is
> + * accessible again.
> + *
> + * Return: 0 on success, -EIO if @dev is not accessible, or another
> + * negative errno if a capability could not be saved.
> */
> int pci_save_state(struct pci_dev *dev)
> {
> - int i;
> + u32 config[16];
> + int i, ret;
> +
> + if (!pci_dev_config_accessible(dev, "save state"))
> + return -EIO;
Maybe we should remove the check in pci_dev_save_and_disable() and
make it check the return value of pci_save_state()? I don't think
checking twice adds anything.
> /* XXX: 100% dword access ok here? */
> for (i = 0; i < 16; i++) {
> - pci_read_config_dword(dev, i * 4, &dev->saved_config_space[i]);
> - pci_dbg(dev, "save config %#04x: %#010x\n",
> - i * 4, dev->saved_config_space[i]);
> + pci_read_config_dword(dev, i * 4, &config[i]);
> + pci_dbg(dev, "save config %#04x: %#010x\n", i * 4, config[i]);
> }
> - dev->state_saved = true;
>
> - i = pci_save_pcie_state(dev);
> - if (i != 0)
> - return i;
> + ret = pci_save_pcie_state(dev);
> + if (!ret)
> + ret = pci_save_pcix_state(dev);
> + if (!ret) {
> + pci_save_dpc_state(dev);
> + pci_save_aer_state(dev);
> + pci_save_ptm_state(dev);
> + pci_save_tph_state(dev);
> + ret = pci_save_vc_state(dev);
> + }
> +
> + /*
> + * The device may have become inaccessible while it was being read.
> + * Keep the previous header snapshot in that case. The capabilities
> + * are saved directly into their buffers, so they are not protected.
> + */
> + if (!pci_dev_config_accessible(dev, "save state"))
> + return -EIO;
I bet we get 99% of the usefulness here by just adding the first
accessibility check above.
This second check only helps if the device becomes inaccessible during
the tiny window while we're saving its state, and I'm not sure that
the extra complexity here and being able to restore a valid config
header with junk in the capabilities is really a benefit.
> - i = pci_save_pcix_state(dev);
> - if (i != 0)
> - return i;
> + memcpy(dev->saved_config_space, config, sizeof(config));
> + dev->state_saved = true;
>
> - pci_save_dpc_state(dev);
> - pci_save_aer_state(dev);
> - pci_save_ptm_state(dev);
> - pci_save_tph_state(dev);
> - return pci_save_vc_state(dev);
> + return ret;
> }
> EXPORT_SYMBOL(pci_save_state);
>