From: Nam Cao <namcao@linutronix.de>
To: "Bjorn Helgaas" <bhelgaas@google.com>,
"Yinghai Lu" <yinghai@kernel.org>,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org,
"Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
"Mika Westerberg" <mika.westerberg@linux.intel.com>,
"Lukas Wunner" <lukas@wunner.de>
Cc: Nam Cao <namcao@linutronix.de>, stable@vger.kernel.org
Subject: [PATCH v3 1/2] PCI: shpchp: Abort hot-plug if pci_hp_add_bridge() fails
Date: Mon, 13 May 2024 16:56:46 +0200 [thread overview]
Message-ID: <fdd7e1c44ab4bfc54271f89e258eea6851dd42dd.1715609848.git.namcao@linutronix.de> (raw)
In-Reply-To: <cover.1715609848.git.namcao@linutronix.de>
If a bridge is hot-added without any bus number available for its
downstream bus, pci_hp_add_bridge() will fail. However, the driver
proceeds regardless, and the kernel crashes.
This crash can be reproduced with the QEMU command:
qemu-system-x86_64 -machine pc-q35-2.10 \
-kernel bzImage \
-drive "file=img,format=raw" \
-m 2048 -smp 2 -enable-kvm \
-append "console=ttyS0 root=/dev/sda" \
-nographic \
-device pcie-root-port,bus=pcie.0,id=rp1,slot=1,bus-reserve=0 \
-device pcie-pci-bridge,id=br1,bus=rp1
then hot-plug a bridge at runtime with the QEMU command:
device_add pci-bridge,id=br2,bus=br1,chassis_nr=1,addr=1
and the kernel crashes:
shpchp 0000:01:00.0: Latch close on Slot(1-1)
shpchp 0000:01:00.0: Button pressed on Slot(1-1)
shpchp 0000:01:00.0: Card present on Slot(1-1)
shpchp 0000:01:00.0: PCI slot #1-1 - powering on due to button press
pci 0000:02:01.0: [1b36:0001] type 01 class 0x060400 conventional PCI bridge
pci 0000:02:01.0: BAR 0 [mem 0x00000000-0x000000ff 64bit]
pci 0000:02:01.0: PCI bridge to [bus 00]
pci 0000:02:01.0: bridge window [io 0x0000-0x0fff]
pci 0000:02:01.0: bridge window [mem 0x00000000-0x000fffff]
pci 0000:02:01.0: bridge window [mem 0x00000000-0x000fffff 64bit pref]
pci 0000:02:01.0: No bus number available for hot-added bridge
(note: kernel should abort hot-plugging right here)
pci 0000:02:01.0: BAR 0 [mem 0xfe600000-0xfe6000ff 64bit]: assigned
shpchp 0000:01:00.0: PCI bridge to [bus 02]
shpchp 0000:01:00.0: bridge window [io 0xc000-0xcfff]
shpchp 0000:01:00.0: bridge window [mem 0xfe600000-0xfe7fffff]
shpchp 0000:01:00.0: bridge window [mem 0xfe000000-0xfe1fffff 64bit pref]
shpchp 0000:02:01.0: HPC vendor_id 1b36 device_id 1 ss_vid 0 ss_did 0
shpchp 0000:02:01.0: enabling device (0000 -> 0002)
ACPI: \_SB_.GSIE: Enabled at IRQ 20
BUG: kernel NULL pointer dereference, address: 00000000000000da
PGD 0 P4D 0
Oops: 0002 [#1] PREEMPT SMP NOPTI
CPU: 1 PID: 66 Comm: kworker/1:2 Not tainted 6.9.0-rc1-00001-g2e0239d47d75 #33
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-debian-1.16.2-1 04/01/2014
Workqueue: shpchp-1 shpchp_pushbutton_thread
RIP: 0010:shpc_init+0x3fb/0x9d0
[stack dump and register dump cut out]
Fix this by aborting the hot-plug if pci_hp_add_bridge() fails.
Fixes: 7d01f70ac6f4 ("PCI: shpchp: use generic pci_hp_add_bridge()")
Signed-off-by: Nam Cao <namcao@linutronix.de>
Cc: Yinghai Lu <yinghai@kernel.org>
Cc: <stable@vger.kernel.org>
---
v3: revert back to the solution in v1 (calling
pci_stop_and_remove_bus_device() and returning negative error code)
v2:
- add more information to commit description
- return 0 instead of -EINVAL
drivers/pci/hotplug/shpchp_pci.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/drivers/pci/hotplug/shpchp_pci.c b/drivers/pci/hotplug/shpchp_pci.c
index 36db0c3c4ea6..2ac98bdc83d9 100644
--- a/drivers/pci/hotplug/shpchp_pci.c
+++ b/drivers/pci/hotplug/shpchp_pci.c
@@ -48,8 +48,13 @@ int shpchp_configure_device(struct slot *p_slot)
}
for_each_pci_bridge(dev, parent) {
- if (PCI_SLOT(dev->devfn) == p_slot->device)
- pci_hp_add_bridge(dev);
+ if (PCI_SLOT(dev->devfn) == p_slot->device) {
+ if (pci_hp_add_bridge(dev)) {
+ pci_stop_and_remove_bus_device(dev);
+ ret = -EINVAL;
+ goto out;
+ }
+ }
}
pci_assign_unassigned_bridge_resources(bridge);
--
2.39.2
next prev parent reply other threads:[~2024-05-13 14:57 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-13 14:56 [PATCH v3 0/2] abort hot-plug if pci_hp_add_bridge() fails Nam Cao
2024-05-13 14:56 ` Nam Cao [this message]
2024-05-13 14:56 ` [PATCH v3 2/2] PCI: pciehp: Abort " Nam Cao
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=fdd7e1c44ab4bfc54271f89e258eea6851dd42dd.1715609848.git.namcao@linutronix.de \
--to=namcao@linutronix.de \
--cc=bhelgaas@google.com \
--cc=gregkh@linuxfoundation.org \
--cc=ilpo.jarvinen@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=lukas@wunner.de \
--cc=mika.westerberg@linux.intel.com \
--cc=stable@vger.kernel.org \
--cc=yinghai@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).