Skip to content

Commit 48991e4

Browse files
computersforpeacebjorn-helgaas
authored andcommitted
PCI/sysfs: Ensure devices are powered for config reads
The "max_link_width", "current_link_speed", "current_link_width", "secondary_bus_number", and "subordinate_bus_number" sysfs files all access config registers, but they don't check the runtime PM state. If the device is in D3cold or a parent bridge is suspended, we may see -EINVAL, bogus values, or worse, depending on implementation details. Wrap these access in pci_config_pm_runtime_{get,put}() like most of the rest of the similar sysfs attributes. Notably, "max_link_speed" does not access config registers; it returns a cached value since d2bd39c ("PCI: Store all PCIe Supported Link Speeds"). Fixes: 56c1af4 ("PCI: Add sysfs max_link_speed/width, current_link_speed/width, etc") Signed-off-by: Brian Norris <[email protected]> Signed-off-by: Brian Norris <[email protected]> Signed-off-by: Bjorn Helgaas <[email protected]> Cc: [email protected] Link: https://patch.msgid.link/20250924095711.v2.1.Ibb5b6ca1e2c059e04ec53140cd98a44f2684c668@changeid
1 parent 299fad4 commit 48991e4

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

drivers/pci/pci-sysfs.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,14 @@ static ssize_t max_link_width_show(struct device *dev,
201201
struct device_attribute *attr, char *buf)
202202
{
203203
struct pci_dev *pdev = to_pci_dev(dev);
204+
ssize_t ret;
204205

205-
return sysfs_emit(buf, "%u\n", pcie_get_width_cap(pdev));
206+
/* We read PCI_EXP_LNKCAP, so we need the device to be accessible. */
207+
pci_config_pm_runtime_get(pdev);
208+
ret = sysfs_emit(buf, "%u\n", pcie_get_width_cap(pdev));
209+
pci_config_pm_runtime_put(pdev);
210+
211+
return ret;
206212
}
207213
static DEVICE_ATTR_RO(max_link_width);
208214

@@ -214,7 +220,10 @@ static ssize_t current_link_speed_show(struct device *dev,
214220
int err;
215221
enum pci_bus_speed speed;
216222

223+
pci_config_pm_runtime_get(pci_dev);
217224
err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat);
225+
pci_config_pm_runtime_put(pci_dev);
226+
218227
if (err)
219228
return -EINVAL;
220229

@@ -231,7 +240,10 @@ static ssize_t current_link_width_show(struct device *dev,
231240
u16 linkstat;
232241
int err;
233242

243+
pci_config_pm_runtime_get(pci_dev);
234244
err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat);
245+
pci_config_pm_runtime_put(pci_dev);
246+
235247
if (err)
236248
return -EINVAL;
237249

@@ -247,7 +259,10 @@ static ssize_t secondary_bus_number_show(struct device *dev,
247259
u8 sec_bus;
248260
int err;
249261

262+
pci_config_pm_runtime_get(pci_dev);
250263
err = pci_read_config_byte(pci_dev, PCI_SECONDARY_BUS, &sec_bus);
264+
pci_config_pm_runtime_put(pci_dev);
265+
251266
if (err)
252267
return -EINVAL;
253268

@@ -263,7 +278,10 @@ static ssize_t subordinate_bus_number_show(struct device *dev,
263278
u8 sub_bus;
264279
int err;
265280

281+
pci_config_pm_runtime_get(pci_dev);
266282
err = pci_read_config_byte(pci_dev, PCI_SUBORDINATE_BUS, &sub_bus);
283+
pci_config_pm_runtime_put(pci_dev);
284+
267285
if (err)
268286
return -EINVAL;
269287

0 commit comments

Comments
 (0)