mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
To: linux-pci@vger.kernel.org, "Bjorn Helgaas" <bhelgaas@google.com>,
	"Lorenzo Pieralisi" <lorenzo.pieralisi@arm.com>,
	"Rob Herring" <robh@kernel.org>,
	"Krzysztof Wilczyński" <kw@linux.com>,
	"David Matlack" <dmatlack@google.com>,
	"Rafael J . Wysocki" <rafael@kernel.org>,
	"Christian König" <christian.koenig@amd.com>,
	"Alex Williamson" <alex@shazbot.org>,
	linux-kernel@vger.kernel.org
Cc: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>, stable@vger.kernel.org
Subject: [PATCH v3 08/17] PCI: Serialize bridge window sizing and resource assignment
Date: Thu, 26 Nov 2026 01:09:07 +0200	[thread overview]
Message-ID: <20261125230925.2441-9-ilpo.jarvinen@linux.intel.com> (raw)
In-Reply-To: <20261125230925.2441-1-ilpo.jarvinen@linux.intel.com>

Bridge window sizing and resource assignment are started from multiple
entrypoints: rescan and hotplug under pci_rescan_remove_lock, and BAR
resize from sysfs or driver probe under device_lock() only. The
entrypoints hold pci_bus_sem only for read, so nothing prevents two
sizing and assignment passes from running concurrently on the same
bridge windows and BARs. E.g., a rescan may assign a BAR that a
concurrent BAR resize has released, or restore a resource the resize has
already assigned.

Looping BAR resize through sysfs against a rescan of the parent bus on a
system with a BMG GPU triggers the add_list sanity check. The rescan
sizes the Downstream Port's prefetchable window while the resize assigns
it, and the sizing values overwrite the assigned window. The resize then
programs these values into the Downstream Port, outside the root bus
resources and overlapping System RAM [mem 0x100000000-0x4075ffffff]. The
rescan skips assigning the window as it looks assigned, which leaves its
add_list entry unused:

  pci_bus 0000:b7: root bus resource [mem 0xd6000000-0xddfbffff window]
  pci_bus 0000:b7: root bus resource [mem 0x2e0000000000-0x2effffffffff window]
  pcieport 0000:b9:01.0: bridge window [mem 0x800000000-0x1000ffffff 64bit pref] to [bus ba] add_size 4000000 add_align 800000000
  pcieport 0000:b9:01.0: PCI bridge to [bus ba]
  pcieport 0000:b9:01.0:   bridge window [mem 0xdda00000-0xddbfffff]
  pcieport 0000:b9:01.0:   bridge window [mem 0x800000000-0x1000ffffff 64bit pref]
  add_list && !list_empty(add_list)
  WARNING: drivers/pci/setup-bus.c:1546 at pci_assign_unassigned_bus_resources+0x9e/0x130, CPU#58: srf8-resize-rac/2861
  CPU: 58 UID: 0 PID: 2861 Comm: srf8-resize-rac Tainted: G S                  7.4.0-rc1-g6628e7d4c6b2 #59 PREEMPT(full)
  RIP: 0010:pci_assign_unassigned_bus_resources+0x9e/0x130
  Call Trace:
   pci_rescan_bus+0x20/0x40
   dev_rescan_store+0x6e/0x90

Other trigger vectors are possible as well. Hotplug takes the same locks
as the sysfs rescan. BAR resize walks the bridge windows up to the Root
Port to make room for the resized BAR, and then sizes and assigns the
whole hierarchy below the Root Port. Thus, hotplug anywhere below the
same Root Port can race with the resize, e.g., when connecting a
Thunderbolt device. Instead of the sysfs resize, a driver can resize a
BAR during probe, e.g., after unbind and bind.

pci_rescan_remove_lock cannot be taken under device_lock() because the
device removal path takes them in the opposite order.

Add res_setup_lock into struct pci_host_bridge and hold it across sizing
and assignment in all entrypoints. Sizing and assignment only operate
within the hierarchy of a single host bridge, so serializing them per
host bridge is enough. Assert the lock is held in the internal functions
the entrypoints call. In pci_do_resource_release_and_resize(), take the
lock before selecting the bridge window and setting the BAR size as both
depend on or change resource state.

The race was also reproduced in QEMU, which found more symptoms.
Looping BAR resize through sysfs against a rescan of the parent bus on
a guest with 512M of RAM, using a synthetic host bridge with a Root Port
and an Endpoint with a Resizable BAR, triggers the following failures:

1) Both the rescan and the resize release the Root Port's prefetchable
   window. Both find it assigned, but the rescan releases it first, and
   the resize then dereferences the NULL parent in __release_resource():

     pcieport 10000:00:00.0: bridge window [mem 0x940000000-0x97fffffff 64bit pref]: releasing
     pcieport 10000:00:00.0: bridge window [mem size 0x40000000 64bit pref]: releasing
     BUG: kernel NULL pointer dereference, address: 0000000000000038
     Oops: Oops: 0000 [#1] SMP
     CPU: 0 UID: 0 PID: 49 Comm: init Not tainted 7.4.0-rc1-00001-g4aa3acaaeb08 #1 PREEMPTLAZY
     RIP: 0010:__release_resource+0x8/0x70
     Call Trace:
      release_resource+0x22/0x40
      pci_release_resource+0x54/0x90
      pci_do_resource_release_and_resize+0x1f3/0x3d0
      __resource_resize_store+0xf0/0x180

2) The rescan restores a resource that has already been assigned:

     WARNING: drivers/pci/setup-bus.c:136 at pci_dev_res_restore+0x32/0x90, CPU#0: init/1
     CPU: 0 UID: 0 PID: 1 Comm: init Not tainted 7.4.0-rc1-dirty #6 PREEMPTLAZY
     RIP: 0010:pci_dev_res_restore+0x32/0x90
     Call Trace:
      __assign_resources_sorted+0x2db/0x570
      __pci_bus_assign_resources_one+0x57/0x190
      __pci_bus_assign_resources_one+0x12d/0x190
      __pci_bus_assign_resources+0xd/0x30
      pci_assign_unassigned_bus_resources+0x6f/0x80
      pci_rescan_bus+0x1a/0x30
      dev_rescan_store+0x46/0x60

3) The rescan assigns the BAR while the resize is assigning it, failing
   the resize:

     pci 10000:01:00.0: BAR 0 [mem 0x900000000-0x90fffffff 64bit pref]: releasing
     pcieport 10000:00:00.0: bridge window [mem 0x900000000-0x90fffffff 64bit pref]: releasing
     pci 10000:01:00.0: BAR 0 [mem size 0x40000000 64bit pref]: can't assign; no space
     pcieport 10000:00:00.0: bridge window [mem 0x900000000-0x93fffffff 64bit pref]: assigned
     pci 10000:01:00.0: BAR 0 [mem size 0x40000000 64bit pref]: failed to assign
     pci 10000:01:00.0: BAR 0 [mem 0x900000000-0x93fffffff 64bit pref]: releasing
     pci 10000:01:00.0: BAR 0 [mem 0x900000000-0x93fffffff 64bit pref]: assigned
     pci 10000:01:00.0: BAR 0 [mem size 0x40000000 64bit pref]: resource restored
     pci 10000:01:00.0: BAR 0 [mem 0x900000000-0x90fffffff 64bit pref]: old value restored
     pci 10000:01:00.0: Failed to resize BAR 0: -28

4) While one pass is sorting a released bridge window for assignment,
   the other pass assigns it, which clears IORESOURCE_STARTALIGN. The
   window then has zero alignment:

     pci 10000:01:00.0: BAR 0 [mem 0x900000000-0x93fffffff 64bit pref]: releasing
     pcieport 10000:00:00.0: bridge window [mem 0x900000000-0x93fffffff 64bit pref]: releasing
     pcieport 10000:00:00.0: bridge window [mem 0x900000000-0x90fffffff 64bit pref]: assigned
     pcieport 10000:00:00.0: bridge window [mem 0x900000000-0x90fffffff 64bit pref]: alignment must not be zero
     pci 10000:01:00.0: BAR 0 [mem 0x900000000-0x90fffffff 64bit pref]: assigned

5) BAR 0 loses its flags and the Root Port's prefetchable window gets
   programmed over RAM, after which every BAR resize fails with -EINVAL:

     pci 10000:01:00.0: BAR 0 [mem size 0x10000000 64bit pref]: can't assign; no space
     resource: remaining [mem 0x000000001ffdf000-0x000000001fffffff] available
     resource: avoiding allocation from e820 entry [mem 0x1ffdf000-0x1fffffff]
     pci 10000:01:00.0: BAR 0 [mem size 0x10000000 64bit pref]: failed to assign
     pci 10000:01:00.0: BAR 0 [mem size 0x10000000 64bit pref]: can't assign; no space
     pci 10000:01:00.0: BAR 0 [mem size 0x10000000 64bit pref]: resetting resource
     pci 10000:01:00.0: BAR 0 [??? 0x00000000 flags 0x0]: failed to assign
     pci 10000:01:00.0: BAR 0 [??? 0x00000000 flags 0x0]: resetting resource
     pcieport 10000:00:00.0: PCI bridge to [bus 01]
     pcieport 10000:00:00.0:   bridge window [mem 0x20000000-0x200fffff]
     pcieport 10000:00:00.0:   bridge window [mem 0x10000000-0x1fffffff 64bit pref]
     pci 10000:01:00.0: Failed to resize BAR 0: -22

Fixes: 8bb705e3e79d ("PCI: Add pci_resize_resource() for resizing BARs")
Fixes: 91fa127794ac ("PCI: Expose PCIe Resizable BAR support via sysfs")
Suggested-by: David Matlack <dmatlack@google.com> # put the new lock to pci_host_bridge
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Acked-by: Rafael J. Wysocki (Intel) <rafael@kernel.org>
Cc: stable@vger.kernel.org
---

Bjorn,

It's up to you if you want to keep all those qemu reproduced cases in the
changelog. You usually seem to prefer the log messages for easier googling
in case somebody happens to hit the issue so I added them all.

I couldn't get the rest to trigger on a real host but that could be due to
serial output that was too slow to keep up with the test loops which might
have affected timing.


 drivers/pci/probe.c     |  2 ++
 drivers/pci/setup-bus.c | 20 ++++++++++++++++++++
 include/linux/pci.h     | 12 ++++++++++++
 3 files changed, 34 insertions(+)

diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
index 9a178e0e5f7f..4aff60ef66d5 100644
--- a/drivers/pci/probe.c
+++ b/drivers/pci/probe.c
@@ -641,6 +641,7 @@ static void pci_release_host_bridge_dev(struct device *dev)
 	if (bridge->domain_nr != PCI_DOMAIN_NR_NOT_SET)
 		pci_bus_release_emul_domain_nr(bridge->domain_nr);
 
+	mutex_destroy(&bridge->res_setup_lock);
 	kfree(bridge);
 }
 
@@ -663,6 +664,7 @@ static void pci_init_host_bridge(struct pci_host_bridge *bridge)
 	INIT_LIST_HEAD(&bridge->windows);
 	INIT_LIST_HEAD(&bridge->dma_ranges);
 	INIT_LIST_HEAD(&bridge->ports);
+	mutex_init(&bridge->res_setup_lock);
 
 	/*
 	 * We assume we can manage these PCIe features.  Some systems may
diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
index 966e0f243032..b2173e1d9db0 100644
--- a/drivers/pci/setup-bus.c
+++ b/drivers/pci/setup-bus.c
@@ -27,6 +27,7 @@
 #include <linux/cache.h>
 #include <linux/limits.h>
 #include <linux/lockdep.h>
+#include <linux/mutex.h>
 #include <linux/rwsem.h>
 #include <linux/sizes.h>
 #include <linux/slab.h>
@@ -40,6 +41,11 @@
 unsigned int pci_flags;
 EXPORT_SYMBOL_GPL(pci_flags);
 
+static struct mutex *pci_res_setup_lock(const struct pci_bus *bus)
+{
+	return &pci_find_host_bridge(bus)->res_setup_lock;
+}
+
 struct pci_dev_resource {
 	struct list_head list;
 	struct resource *res;
@@ -1396,6 +1402,8 @@ void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head)
 	struct pci_host_bridge *host;
 	int hdr_type;
 
+	lockdep_assert_held(pci_res_setup_lock(bus));
+
 	list_for_each_entry(dev, &bus->devices, bus_list) {
 		struct pci_bus *b = dev->subordinate;
 		if (!b)
@@ -1459,6 +1467,7 @@ void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head)
 
 void pci_bus_size_bridges(struct pci_bus *bus)
 {
+	guard(mutex)(pci_res_setup_lock(bus));
 	guard(rwsem_read)(&pci_bus_sem);
 	__pci_bus_size_bridges(bus, NULL);
 }
@@ -1544,6 +1553,8 @@ void __pci_bus_assign_resources(const struct pci_bus *bus,
 				struct list_head *add_list,
 				struct list_head *fail_head)
 {
+	lockdep_assert_held(pci_res_setup_lock(bus));
+
 	__pci_bus_assign_resources_one(bus, add_list, fail_head);
 
 	if (WARN_ON_ONCE(add_list && !list_empty(add_list)))
@@ -1552,6 +1563,7 @@ void __pci_bus_assign_resources(const struct pci_bus *bus,
 
 void pci_bus_assign_resources(const struct pci_bus *bus)
 {
+	guard(mutex)(pci_res_setup_lock(bus));
 	guard(rwsem_read)(&pci_bus_sem);
 	__pci_bus_assign_resources(bus, NULL, NULL);
 }
@@ -2194,6 +2206,7 @@ void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus)
 	int pci_try_num = 1;
 	enum enable_type enable_local;
 
+	guard(mutex)(pci_res_setup_lock(bus));
 	guard(rwsem_read)(&pci_bus_sem);
 
 	/* Don't realloc if asked to do so */
@@ -2272,6 +2285,7 @@ static void pci_bridge_size_and_assign_resources(struct pci_dev *bridge)
 	int tried_times = 0;
 	LIST_HEAD(fail_head);
 
+	guard(mutex)(pci_res_setup_lock(bridge->bus));
 	guard(rwsem_read)(&pci_bus_sem);
 
 	while (1) {
@@ -2330,6 +2344,8 @@ static int pbus_reassign_bridge_resources(struct pci_bus *bus, struct resource *
 	unsigned int i;
 	int ret = 0;
 
+	lockdep_assert_held(pci_res_setup_lock(bus));
+
 	while (!pci_is_root_bus(bus)) {
 		bridge = bus->self;
 		res = pbus_select_window(bus, res);
@@ -2401,6 +2417,7 @@ static int __pci_do_resource_release_and_resize(struct pci_dev *pdev,
 	unsigned int i;
 	int old, ret;
 
+	lockdep_assert_held(pci_res_setup_lock(bus));
 	lockdep_assert_held(&pci_bus_sem);
 
 	b_win = pbus_select_window(bus, res);
@@ -2495,6 +2512,7 @@ static int __pci_do_resource_release_and_resize(struct pci_dev *pdev,
 int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size,
 				       int exclude_bars)
 {
+	guard(mutex)(pci_res_setup_lock(pdev->bus));
 	guard(rwsem_read)(&pci_bus_sem);
 
 	return __pci_do_resource_release_and_resize(pdev, resno, size,
@@ -2516,6 +2534,7 @@ void pci_assign_unassigned_slot_resources(struct pci_bus *bus, u16 slot)
 	/* List of resources that want additional resources */
 	LIST_HEAD(add_list);
 
+	guard(mutex)(pci_res_setup_lock(bus));
 	guard(rwsem_read)(&pci_bus_sem);
 	for_each_pci_bridge(dev, bus) {
 		if (slot != PCI_SLOT_ALL_DEVICES &&
@@ -2536,6 +2555,7 @@ EXPORT_SYMBOL_GPL(pci_assign_unassigned_bus_resources);
 
 void pci_bus_size_and_assign_resources(struct pci_bus *bus)
 {
+	guard(mutex)(pci_res_setup_lock(bus));
 	guard(rwsem_read)(&pci_bus_sem);
 	__pci_bus_size_bridges(bus, NULL);
 	__pci_bus_assign_resources(bus, NULL, NULL);
diff --git a/include/linux/pci.h b/include/linux/pci.h
index d03a2e277013..f535ab12b04e 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -31,6 +31,7 @@
 #include <linux/init.h>
 #include <linux/ioport.h>
 #include <linux/list.h>
+#include <linux/mutex.h>
 #include <linux/compiler.h>
 #include <linux/errno.h>
 #include <linux/kobject.h>
@@ -646,6 +647,17 @@ struct pci_host_bridge {
 	struct list_head windows;	/* resource_entry */
 	struct list_head dma_ranges;	/* dma ranges resource list */
 	struct list_head ports;		/* Root Port list (pci_host_port) */
+	/*
+	 * Serializes bridge window sizing, resource fitting, and assignment
+	 * below the host bridge. pci_rescan_remove_lock cannot be used for
+	 * this because these operations also run under device_lock(), e.g.,
+	 * BAR resize from sysfs or probe, and VMD probe.
+	 *
+	 * Context:
+	 *  - Nests inside pci_rescan_remove_lock and device_lock().
+	 *  - Nests outside pci_bus_sem.
+	 */
+	struct mutex	res_setup_lock;
 #ifdef CONFIG_PCI_IDE
 	u16 nr_ide_streams; /* Max streams possibly active in @ide_stream_ida */
 	struct ida ide_stream_ida;
-- 
2.47.3


  parent reply	other threads:[~2026-11-25 23:11 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 23:08 [PATCH v3 00/17] " Ilpo Järvinen
2026-11-25 23:09 ` [PATCH v3 01/17] PCI: Add pci_bus_size_and_assign_resources() Ilpo Järvinen
2026-11-25 23:09 ` [PATCH v3 02/17] alpha/PCI: Convert to pci_bus_size_and_assign_resources() Ilpo Järvinen
2026-11-25 23:09 ` [PATCH v3 03/17] PCI: Use guard() for pci_bus_sem in pci_do_resource_release_and_resize() Ilpo Järvinen
2026-11-25 23:09 ` [PATCH v3 04/17] PCI: Add pci_assign_unassigned_slot_resources() Ilpo Järvinen
2026-11-25 23:09 ` [PATCH v3 05/17] PCI: acpiphp: Use pci_assign_unassigned_slot_resources() Ilpo Järvinen
2026-11-25 23:09 ` [PATCH v3 06/17] PCI: Hold pci_bus_sem across sizing and assignment entrypoints Ilpo Järvinen
2026-11-25 23:09 ` [PATCH v3 07/17] PCI: Make pci_find_host_bridge() take a const bus Ilpo Järvinen
2026-11-25 23:09 ` Ilpo Järvinen [this message]
2026-11-25 23:09 ` [PATCH v3 09/17] PCI: Make internal sizing and assignment functions static Ilpo Järvinen
2026-11-25 23:09 ` [PATCH v3 10/17] EDAC/i82875p: Convert to pci_assign_unassigned_bus_resources() Ilpo Järvinen
2026-11-26  0:32   ` Borislav Petkov
2026-11-25 23:09 ` [PATCH v3 11/17] platform/x86: asus-wmi: " Ilpo Järvinen
2026-11-25 23:09 ` [PATCH v3 12/17] platform/x86: eeepc-laptop: " Ilpo Järvinen
2026-11-25 23:09 ` [PATCH v3 13/17] parisc/PCI: " Ilpo Järvinen
2026-11-25 23:09 ` [PATCH v3 14/17] PCI: octep_hp: " Ilpo Järvinen
2026-11-25 23:09 ` [PATCH v3 15/17] PCI: ftpci100: " Ilpo Järvinen
2026-11-25 23:09 ` [PATCH v3 16/17] PCI: hv: " Ilpo Järvinen
2026-11-25 23:09 ` [PATCH v3 17/17] PCI: Remove pci_bus_assign_resources() Ilpo Järvinen

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=20261125230925.2441-9-ilpo.jarvinen@linux.intel.com \
    --to=ilpo.jarvinen@linux.intel.com \
    --cc=alex@shazbot.org \
    --cc=bhelgaas@google.com \
    --cc=christian.koenig@amd.com \
    --cc=dmatlack@google.com \
    --cc=kw@linux.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=lorenzo.pieralisi@arm.com \
    --cc=rafael@kernel.org \
    --cc=robh@kernel.org \
    --cc=stable@vger.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

all inboxes | Powered by JetHome®