Re: [PATCH 2/4] PCI: pciehp: bail out if pci_hp_add_bridge() fails

From: Nam Cao
Date: Sat May 04 2024 - 06:56:49 EST


On Sat, May 04, 2024 at 11:51:54AM +0200, Lukas Wunner wrote:
> On Sat, May 04, 2024 at 11:35:29AM +0200, Nam Cao wrote:
> > pci_stop_and_remove_bus_device() is not necessary to prevent kernel
> > crashing. But without this, we cannot hot-plug any other devices to this
> > slot afterward, despite the bridge has already been removed. Below is what
> > happens without pci_stop_and_remove_bus_device().
> >
> > First, we hotplug a bridge. That fails, so QEMU removes this bridge:
> > (qemu) device_add pci-bridge,id=br2,bus=br1,chassis_nr=19,addr=1
> > [ 9.289609] shpchp 0000:01:00.0: Latch close on Slot(1-1)
> > [ 9.291145] shpchp 0000:01:00.0: Button pressed on Slot(1-1)
> > [ 9.292705] shpchp 0000:01:00.0: Card present on Slot(1-1)
> > [ 9.294369] shpchp 0000:01:00.0: PCI slot #1-1 - powering on due to button press
> > [ 15.529997] pci 0000:02:01.0: [1b36:0001] type 01 class 0x060400 conventional PCI bridge
> > [ 15.533907] pci 0000:02:01.0: BAR 0 [mem 0x00000000-0x000000ff 64bit]
> > [ 15.535802] pci 0000:02:01.0: PCI bridge to [bus 00]
> > [ 15.538519] pci 0000:02:01.0: bridge window [io 0x0000-0x0fff]
> > [ 15.540261] pci 0000:02:01.0: bridge window [mem 0x00000000-0x000fffff]
> > [ 15.543486] pci 0000:02:01.0: bridge window [mem 0x00000000-0x000fffff 64bit pref]
> > [ 15.547151] pci 0000:02:01.0: No bus number available for hot-added bridge
> > [ 15.549067] shpchp 0000:01:00.0: Cannot add device at 0000:02:01
> > [ 15.553104] shpchp 0000:01:00.0: Latch open on Slot(1-1)
> > [ 15.555246] shpchp 0000:01:00.0: Card not present on Slot(1-1)
>
> I'm not familiar with shpchp, I don't understand why it's thinking
> that there's no card after it failed to find a bus number.

Sorry, I got mixed up between the two.

> Could you reproduce with pciehp instead of shpchp please?

Same thing for pciehp below. I think the problem is because without
pci_stop_and_remove_bus_device(), no one cleans up the device added in
pci_scan_slot(). When another device get hot-added, pci_get_slot() wrongly
thinks another device is already there, so the hot-plug fails.

Best regards,
Nam

(qemu) device_add pcie-pci-bridge,id=br1,bus=rp1
[ 19.840550] pcieport 0000:00:03.0: pciehp: pending interrupts 0x0009 from Slot Status
[ 19.842843] pcieport 0000:00:03.0: pciehp: Slot(1): Button press: will power on in 5 sec
[ 19.845289] pcieport 0000:00:03.0: pciehp: pending interrupts 0x0010 from Slot Status
[ 19.847502] pcieport 0000:00:03.0: pciehp: pciehp_set_indicators: SLOTCTRL 6c write cmd 2c0
[ 19.849876] pcieport 0000:00:03.0: pciehp: pciehp_check_link_active: lnk_status = 2011
[ 19.852094] pcieport 0000:00:03.0: pciehp: Slot(1): Card present
[ 19.853809] pcieport 0000:00:03.0: pciehp: Slot(1): Link Up
[ 19.855412] pcieport 0000:00:03.0: pciehp: pciehp_get_power_status: SLOTCTRL 6c value read 6f1
[ 19.857975] pcieport 0000:00:03.0: pciehp: pending interrupts 0x0010 from Slot Status
[ 19.860199] pcieport 0000:00:03.0: pciehp: pciehp_power_on_slot: SLOTCTRL 6c write cmd 0
[ 19.862586] pcieport 0000:00:03.0: pciehp: pending interrupts 0x0010 from Slot Status
[ 19.864806] pcieport 0000:00:03.0: pciehp: pciehp_set_indicators: SLOTCTRL 6c write cmd 200
[ 20.994936] pcieport 0000:00:03.0: pciehp: pciehp_check_link_status: lnk_status = 2011
[ 20.997463] pci 0000:01:00.0: [1b36:000e] type 01 class 0x060400 PCIe to PCI/PCI-X bridge
[ 21.001131] pci 0000:01:00.0: BAR 0 [mem 0x00000000-0x000000ff 64bit]
[ 21.003071] pci 0000:01:00.0: PCI bridge to [bus 00]
[ 21.005417] pci 0000:01:00.0: bridge window [io 0x0000-0x0fff]
[ 21.007181] pci 0000:01:00.0: bridge window [mem 0x00000000-0x000fffff]
[ 21.010084] pci 0000:01:00.0: bridge window [mem 0x00000000-0x000fffff 64bit pref]
[ 21.014162] pci 0000:01:00.0: vgaarb: pci_notify
[ 21.015900] pci 0000:01:00.0: No bus number available for hot-added bridge
[ 21.017865] pcieport 0000:00:03.0: pciehp: Cannot add device at 0000:01:00
[ 21.019931] pcieport 0000:00:03.0: pciehp: pending interrupts 0x0010 from Slot Status
[ 21.022178] pcieport 0000:00:03.0: pciehp: pciehp_power_off_slot: SLOTCTRL 6c write cmd 400
[ 22.084607] pcieport 0000:00:03.0: pciehp: pending interrupts 0x0018 from Slot Status
[ 22.086845] pcieport 0000:00:03.0: pciehp: pciehp_set_indicators: SLOTCTRL 6c write cmd 340
[ 22.089323] pcieport 0000:00:03.0: pciehp: pending interrupts 0x0010 from Slot Status
[ 22.091539] pcieport 0000:00:03.0: pciehp: pciehp_set_indicators: SLOTCTRL 6c write cmd 300
[ 22.093913] pcieport 0000:00:03.0: pciehp: pciehp_check_link_active: lnk_status = 11

(qemu) device_add e1000,bus=rp1,id=eth1
[ 58.389527] pcieport 0000:00:03.0: pciehp: pending interrupts 0x0009 from Slot Status
[ 58.391789] pcieport 0000:00:03.0: pciehp: Slot(1): Button press: will power on in 5 sec
[ 58.394175] pcieport 0000:00:03.0: pciehp: pending interrupts 0x0010 from Slot Status
[ 58.396365] pcieport 0000:00:03.0: pciehp: pciehp_set_indicators: SLOTCTRL 6c write cmd 2c0
[ 58.398681] pcieport 0000:00:03.0: pciehp: pciehp_check_link_active: lnk_status = 2011
[ 58.400871] pcieport 0000:00:03.0: pciehp: Slot(1): Card present
[ 58.402542] pcieport 0000:00:03.0: pciehp: Slot(1): Link Up
[ 58.404154] pcieport 0000:00:03.0: pciehp: pciehp_get_power_status: SLOTCTRL 6c value read 6f1
[ 58.406627] pcieport 0000:00:03.0: pciehp: pending interrupts 0x0010 from Slot Status
[ 58.408798] pcieport 0000:00:03.0: pciehp: pciehp_power_on_slot: SLOTCTRL 6c write cmd 0
[ 58.411213] pcieport 0000:00:03.0: pciehp: pending interrupts 0x0010 from Slot Status
[ 58.413386] pcieport 0000:00:03.0: pciehp: pciehp_set_indicators: SLOTCTRL 6c write cmd 200
[ 59.523011] pcieport 0000:00:03.0: pciehp: pciehp_check_link_status: lnk_status = 2011
[ 59.525256] pcieport 0000:00:03.0: pciehp: Device 0000:01:00.0 already exists at 0000:01:00, skipping hot-add
[ 59.528139] pcieport 0000:00:03.0: pciehp: pending interrupts 0x0010 from Slot Status
[ 59.530325] pcieport 0000:00:03.0: pciehp: pciehp_set_indicators: SLOTCTRL 6c write cmd 1c0