[PATCH RFC v3 4/6] PCI: Resume downstream ports before sysfs Link Control writes

From: Matt Thompson via B4 Relay

Date: Mon Nov 16 2026 - 04:35:58 EST


From: Matt Thompson <matt@xxxxxxxxxxxxx>

Configuration space can remain accessible while a port is runtime
suspended. A sysfs Link Control write can then reach hardware before
runtime resume restores the saved state. On RockPro64 a rescan resumed
the port and restored the saved Link Disable bit after userspace had
cleared it, leaving the endpoint absent.

Resume a runtime-suspended downstream port before writing overlapping
Link Control bytes. The configuration access already holds the runtime
PM reference across the write.

Signed-off-by: Matt Thompson <matt@xxxxxxxxxxxxx>
---
drivers/pci/pci-sysfs.c | 16 ++++++++++++++++
1 file changed, 16 insertions(+)

diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
index 6669e9a..ff47ea1 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -786,6 +786,7 @@ static ssize_t pci_write_config(struct file *filp, struct kobject *kobj,
struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
unsigned int size = count;
loff_t init_off = off;
+ loff_t lnkctl = pci_pcie_cap(dev) + PCI_EXP_LNKCTL;
u8 *data = (u8 *) buf;
int ret;

@@ -809,6 +810,21 @@ static ssize_t pci_write_config(struct file *filp, struct kobject *kobj,

pci_config_pm_runtime_get(dev);

+ /*
+ * Link Control can be written while config space remains accessible in
+ * runtime suspend. Restore saved state first so a later resume cannot
+ * replace the new link setting with the saved value.
+ */
+ if (count && pci_is_pcie(dev) && pcie_downstream_port(dev) &&
+ pm_runtime_suspended(&dev->dev) &&
+ off < lnkctl + sizeof(u16) && off + count > lnkctl) {
+ ret = pm_runtime_resume(&dev->dev);
+ if (ret < 0) {
+ pci_config_pm_runtime_put(dev);
+ return ret;
+ }
+ }
+
if ((off & 1) && size) {
pci_user_write_config_byte(dev, off, data[off - init_off]);
off++;

--
Git-155)