On Fri, Sep 27, 2024 at 11:28:54AM +0200, Lukas Wunner wrote: > I realize now that commit 9d573d19547b ("PCI: pciehp: Detect device > replacement during system sleep") is a little overzealous because it > not only reacts to *replaced* devices but also to *unplugged* devices: > If the device was unplugged, reading the vendor and device ID returns > 0xffff, which is different from the cached value, so the device is > assumed to have been replaced even though it's actually been unplugged. > > The device replacement check runs in the ->resume_noirq phase. Later on > in the ->resume phase, pciehp_resume() calls pciehp_check_presence() to > check for unplugged devices. Commit 9d573d19547b inadvertantly reacts > before pciehp_check_presence() gets a chance to react. So that's something > that we should probably change. FWIW, below is a (compile-tested only) patch which modifies pciehp_device_replaced() to return false if the device was *unplugged* during system sleep. It continues to return true if it was *replaced* during system sleep. This might avoid the issue you're seeing, though it would be good if you could also try Keith's deadlock prevention patch (without any other patch) to determine if the deadlock is the actual root cause (as I suspect). Thanks! -- >8 -- diff --git a/drivers/pci/hotplug/pciehp_core.c b/drivers/pci/hotplug/pciehp_core.c index ff458e6..174832b 100644 --- a/drivers/pci/hotplug/pciehp_core.c +++ b/drivers/pci/hotplug/pciehp_core.c @@ -287,24 +287,32 @@ static int pciehp_suspend(struct pcie_device *dev) static bool pciehp_device_replaced(struct controller *ctrl) { struct pci_dev *pdev __free(pci_dev_put); + u64 dsn; u32 reg; pdev = pci_get_slot(ctrl->pcie->port->subordinate, PCI_DEVFN(0, 0)); if (!pdev) + return false; + + if (pci_read_config_dword(pdev, PCI_VENDOR_ID, ®) == 0 && + !PCI_POSSIBLE_ERROR(reg) && + reg != (pdev->vendor | (pdev->device << 16))) return true; - if (pci_read_config_dword(pdev, PCI_VENDOR_ID, ®) || - reg != (pdev->vendor | (pdev->device << 16)) || - pci_read_config_dword(pdev, PCI_CLASS_REVISION, ®) || + if (pci_read_config_dword(pdev, PCI_CLASS_REVISION, ®) == 0 && + !PCI_POSSIBLE_ERROR(reg) && reg != (pdev->revision | (pdev->class << 8))) return true; if (pdev->hdr_type == PCI_HEADER_TYPE_NORMAL && - (pci_read_config_dword(pdev, PCI_SUBSYSTEM_VENDOR_ID, ®) || - reg != (pdev->subsystem_vendor | (pdev->subsystem_device << 16)))) + pci_read_config_dword(pdev, PCI_SUBSYSTEM_VENDOR_ID, ®) == 0 && + !PCI_POSSIBLE_ERROR(reg) && + reg != (pdev->subsystem_vendor | (pdev->subsystem_device << 16))) return true; - if (pci_get_dsn(pdev) != ctrl->dsn) + dsn = pci_get_dsn(pdev); + if (!PCI_POSSIBLE_ERROR(dsn) && + dsn != ctrl->dsn) return true; return false;