[PATCH] PCI: Only restore state that was successfully saved around a reset

From: lirongqing

Date: Tue Nov 10 2026 - 00:40:16 EST


From: Li RongQing <lirongqing@xxxxxxxxx>

pci_dev_save_and_disable() returns early when config space is inaccessible,
without saving anything, but pci_dev_restore() calls pci_restore_state()
unconditionally and pci_restore_state() does not look at state_saved. After
the reset makes config space accessible again, the all-zero or stale
saved_config_space[] array is written back to the device, clearing Command,
BARs and, for bridges, the bus number registers.

Clear state_saved when the save is skipped and only restore when a state
was saved.

Fixes: e18d1abc3bff ("PCI: Avoid saving config space state if inaccessible")
Signed-off-by: Li RongQing <lirongqing@xxxxxxxxx>
---
drivers/pci/pci.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index 67fe565..ddb86dd 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -5156,8 +5156,10 @@ static void pci_dev_save_and_disable(struct pci_dev *dev)
*/
pci_set_power_state(dev, PCI_D0);

- if (!pci_dev_config_accessible(dev, "save state"))
+ if (!pci_dev_config_accessible(dev, "save state")) {
+ dev->state_saved = false;
return;
+ }

pci_save_state(dev);
/*
@@ -5175,7 +5177,8 @@ static void pci_dev_restore(struct pci_dev *dev)
const struct pci_error_handlers *err_handler =
dev->driver ? dev->driver->err_handler : NULL;

- pci_restore_state(dev);
+ if (dev->state_saved)
+ pci_restore_state(dev);

/*
* dev->driver->err_handler->reset_done() is protected against
--
2.9.4