[PATCH 4/5] PCI/sysfs: Use user config accessors for link attributes

From: Matthew Wood

Date: Sat Oct 24 2026 - 20:09:03 EST


max_link_width, current_link_speed and current_link_width read the PCIe
Link Capabilities and Link Status registers with the kernel config
accessors. Those don't check block_cfg_access so a read that races
with a reset goes to the device mid-reset and can fail or return a bogus
value.

Use pcie_capability_user_read_{word,dword}() instead so the read waits for
any pci_cfg_access_lock() holder (e.g. pci_reset_function()) to finish. For
max_link_width, this means inlining the pcie_get_width_cap() logic
instead of creating an additional helper with this single calling location.

Signed-off-by: Matthew Wood <thepacketgeek@xxxxxxxxx>
Tested-by: Nikita Vetoshkin <nekto0n@xxxxxxxx>
---
drivers/pci/pci-sysfs.c | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
index b37a285d717d..a272f8583a8f 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -208,14 +208,16 @@ static ssize_t max_link_width_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct pci_dev *pdev = to_pci_dev(dev);
- ssize_t ret;
+ u32 lnkcap;

/* We read PCI_EXP_LNKCAP, so we need the device to be accessible. */
pci_config_pm_runtime_get(pdev);
- ret = sysfs_emit(buf, "%u\n", pcie_get_width_cap(pdev));
+ pcie_capability_user_read_dword(pdev, PCI_EXP_LNKCAP, &lnkcap);
pci_config_pm_runtime_put(pdev);

- return ret;
+ return sysfs_emit(buf, "%u\n", lnkcap ?
+ FIELD_GET(PCI_EXP_LNKCAP_MLW, lnkcap) :
+ PCIE_LNK_WIDTH_UNKNOWN);
}
static DEVICE_ATTR_RO(max_link_width);

@@ -228,7 +230,8 @@ static ssize_t current_link_speed_show(struct device *dev,
enum pci_bus_speed speed;

pci_config_pm_runtime_get(pci_dev);
- err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat);
+ err = pcie_capability_user_read_word(pci_dev, PCI_EXP_LNKSTA,
+ &linkstat);
pci_config_pm_runtime_put(pci_dev);

if (err)
@@ -248,7 +251,8 @@ static ssize_t current_link_width_show(struct device *dev,
int err;

pci_config_pm_runtime_get(pci_dev);
- err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat);
+ err = pcie_capability_user_read_word(pci_dev, PCI_EXP_LNKSTA,
+ &linkstat);
pci_config_pm_runtime_put(pci_dev);

if (err)
--
2.53.0-Meta