[PATCH] PCI/ASPM: Add missing NULL check for link in sysfs show/store callbacks
From: ZhaoJinming
Date: Mon Jul 27 2026 - 03:31:08 EST
The aspm_attr_show_common(), aspm_attr_store_common(), clkpm_show()
and clkpm_store() sysfs callbacks call pcie_aspm_get_link() without
checking the return value. A NULL return would indicate the device is
not PCIe, has no upstream bridge, or the link state has not been
initialized.
While the aspm_ctrl_attrs_are_visible() is_visible callback already
hides these attributes when link is NULL, add the NULL check for
defense in depth and consistency with other callers of
pcie_aspm_get_link() in the same file (__pci_disable_link_state,
__pci_enable_link_state, pcie_aspm_enabled).
Signed-off-by: ZhaoJinming <zhaojinming@xxxxxxxxxxxxx>
---
drivers/pci/pcie/aspm.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/drivers/pci/pcie/aspm.c b/drivers/pci/pcie/aspm.c
index 172783e7f519..108d151a786e 100644
--- a/drivers/pci/pcie/aspm.c
+++ b/drivers/pci/pcie/aspm.c
@@ -1654,6 +1654,9 @@ static ssize_t aspm_attr_show_common(struct device *dev,
struct pci_dev *pdev = to_pci_dev(dev);
struct pcie_link_state *link = pcie_aspm_get_link(pdev);
+ if (!link)
+ return 0;
+
return sysfs_emit(buf, "%d\n", (link->aspm_enabled & state) ? 1 : 0);
}
@@ -1665,6 +1668,9 @@ static ssize_t aspm_attr_store_common(struct device *dev,
struct pcie_link_state *link = pcie_aspm_get_link(pdev);
bool state_enable;
+ if (!link)
+ return -ENODEV;
+
if (kstrtobool(buf, &state_enable) < 0)
return -EINVAL;
@@ -1713,6 +1719,9 @@ static ssize_t clkpm_show(struct device *dev,
struct pci_dev *pdev = to_pci_dev(dev);
struct pcie_link_state *link = pcie_aspm_get_link(pdev);
+ if (!link)
+ return 0;
+
return sysfs_emit(buf, "%d\n", link->clkpm_enabled);
}
@@ -1724,6 +1733,9 @@ static ssize_t clkpm_store(struct device *dev,
struct pcie_link_state *link = pcie_aspm_get_link(pdev);
bool state_enable;
+ if (!link)
+ return -ENODEV;
+
if (kstrtobool(buf, &state_enable) < 0)
return -EINVAL;
--
2.20.1