Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

PCI/ACPI: Fix runtime PM ref imbalance on Hot-Plug Capable ports

pci_bridge_d3_possible() is called from both pcie_portdrv_probe() and
pcie_portdrv_remove() to determine whether runtime power management shall
be enabled (on probe) or disabled (on remove) on a PCIe port.

The underlying assumption is that pci_bridge_d3_possible() always returns
the same value, else a runtime PM reference imbalance would occur. That
assumption is not given if the PCIe port is inaccessible on remove due to
hot-unplug: pci_bridge_d3_possible() calls pciehp_is_native(), which
accesses Config Space to determine whether the port is Hot-Plug Capable.
An inaccessible port returns "all ones", which is converted to "all
zeroes" by pcie_capability_read_dword(). Hence the port no longer seems
Hot-Plug Capable on remove even though it was on probe.

The resulting runtime PM ref imbalance causes warning messages such as:

pcieport 0000:02:04.0: Runtime PM usage count underflow!

Avoid the Config Space access (and thus the runtime PM ref imbalance) by
caching the Hot-Plug Capable bit in struct pci_dev.

The struct already contains an "is_hotplug_bridge" flag, which however is
not only set on Hot-Plug Capable PCIe ports, but also Conventional PCI
Hot-Plug bridges and ACPI slots. The flag identifies bridges which are
allocated additional MMIO and bus number resources to allow for hierarchy
expansion.

The kernel is somewhat sloppily using "is_hotplug_bridge" in a number of
places to identify Hot-Plug Capable PCIe ports, even though the flag
encompasses other devices. Subsequent commits replace these occurrences
with the new flag to clearly delineate Hot-Plug Capable PCIe ports from
other kinds of hotplug bridges.

Document the existing "is_hotplug_bridge" and the new "is_pciehp" flag
and document the (non-obvious) requirement that pci_bridge_d3_possible()
always returns the same value across the entire lifetime of a bridge,
including its hot-removal.

Fixes: 5352a44a561d ("PCI: pciehp: Make pciehp_is_native() stricter")
Reported-by: Laurent Bigonville <bigon@bigon.be>
Closes: https://bugzilla.kernel.org/show_bug.cgi?id=220216
Reported-by: Mario Limonciello <mario.limonciello@amd.com>
Closes: https://lore.kernel.org/r/20250609020223.269407-3-superm1@kernel.org/
Link: https://lore.kernel.org/all/20250620025535.3425049-3-superm1@kernel.org/T/#u
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Acked-by: Rafael J. Wysocki <rafael@kernel.org>
Cc: stable@vger.kernel.org # v4.18+
Link: https://patch.msgid.link/fe5dcc3b2e62ee1df7905d746bde161eb1b3291c.1752390101.git.lukas@wunner.de

authored by

Lukas Wunner and committed by
Bjorn Helgaas
6cff20ce 19272b37

+13 -5
+1 -3
drivers/pci/pci-acpi.c
··· 816 816 bool pciehp_is_native(struct pci_dev *bridge) 817 817 { 818 818 const struct pci_host_bridge *host; 819 - u32 slot_cap; 820 819 821 820 if (!IS_ENABLED(CONFIG_HOTPLUG_PCI_PCIE)) 822 821 return false; 823 822 824 - pcie_capability_read_dword(bridge, PCI_EXP_SLTCAP, &slot_cap); 825 - if (!(slot_cap & PCI_EXP_SLTCAP_HPC)) 823 + if (!bridge->is_pciehp) 826 824 return false; 827 825 828 826 if (pcie_ports_native)
+5 -1
drivers/pci/pci.c
··· 3030 3030 * pci_bridge_d3_possible - Is it possible to put the bridge into D3 3031 3031 * @bridge: Bridge to check 3032 3032 * 3033 - * This function checks if it is possible to move the bridge to D3. 3034 3033 * Currently we only allow D3 for some PCIe ports and for Thunderbolt. 3034 + * 3035 + * Return: Whether it is possible to move the bridge to D3. 3036 + * 3037 + * The return value is guaranteed to be constant across the entire lifetime 3038 + * of the bridge, including its hot-removal. 3035 3039 */ 3036 3040 bool pci_bridge_d3_possible(struct pci_dev *bridge) 3037 3041 {
+1 -1
drivers/pci/probe.c
··· 1678 1678 1679 1679 pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32); 1680 1680 if (reg32 & PCI_EXP_SLTCAP_HPC) 1681 - pdev->is_hotplug_bridge = 1; 1681 + pdev->is_hotplug_bridge = pdev->is_pciehp = 1; 1682 1682 } 1683 1683 1684 1684 static void set_pcie_thunderbolt(struct pci_dev *dev)
+6
include/linux/pci.h
··· 328 328 * determined (e.g., for Root Complex Integrated 329 329 * Endpoints without the relevant Capability 330 330 * Registers). 331 + * @is_hotplug_bridge: Hotplug bridge of any kind (e.g. PCIe Hot-Plug Capable, 332 + * Conventional PCI Hot-Plug, ACPI slot). 333 + * Such bridges are allocated additional MMIO and bus 334 + * number resources to allow for hierarchy expansion. 335 + * @is_pciehp: PCIe Hot-Plug Capable bridge. 331 336 */ 332 337 struct pci_dev { 333 338 struct list_head bus_list; /* Node in per-bus list */ ··· 456 451 unsigned int is_physfn:1; 457 452 unsigned int is_virtfn:1; 458 453 unsigned int is_hotplug_bridge:1; 454 + unsigned int is_pciehp:1; 459 455 unsigned int shpc_managed:1; /* SHPC owned by shpchp */ 460 456 unsigned int is_thunderbolt:1; /* Thunderbolt controller */ 461 457 /*