Skip to contentSkip to conversation

PCI: Serialize bridge window sizing and resource assignment

PATCH Superseded Ilpo Järvinen 8 participants 43 messages Nov 16 – Nov 20

Subsystems (18)
Stable branch Arm (32-bit) ACPI PCI +14 more subsystems

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Ilpo Järvinen

author maintainer
#0 lore raw

cover PCI: Serialize bridge window sizing and resource assignment

Hi all,

Bridge window sizing and resource assignment can be started from multiple entry points: rescan and hotplug under pci_rescan_remove_lock, and BAR resize from sysfs or driver probe under device_lock() only. As the entry points hold pci_bus_sem at most for read, nothing prevents two sizing and assignment passes from running concurrently on the same bridge windows and BARs.

Multiple issues due to these races could be reproduced some on a real host and more in QEMU with a synthetic PCI host bridge (not part of this series) by looping BAR resize against a rescan through sysfs. The failures include spurious BAR resize failures, a WARN in pci_dev_res_restore(), a NULL pointer dereference in __release_resource(), and a bridge window programmed over RAM. None of them occurred with this series applied.

The series first adds pci_bus_size_and_assign_resources() so that PCI core can hold the locks across both phases and converting 1:1 callers (alpha nautilus retains extra sizing call to avoid complex refactoring). Secondly, convert setup-bus entrypoints to guard() and pci_res_setup_lock to serialize the sizing and assignment passes. Last, convert the remaining pci_bus_assign_resources() users and remove it.

Drivers that call pci_assign_resource() or pci_release_resource() directly are not covered by this series.

I've another series coming up which will add struct pci_setup_data that is to be held across the entire sizing and assignment algorithm, which is another reason why those actions have to be kept together going forward. The goal of that upcoming series is to size also assigned bridge windows properly and we need a place to keep the alignment because STARTALIGN is lost when a resource gets assigned.

Ilpo Järvinen (13):
PCI: Add pci_bus_size_and_assign_resources()
alpha/PCI: Convert to pci_bus_size_and_assign_resources()
PCI: Use guard(&pci_bus_sem) in pci_do_resource_release_and_resize()
PCI: Hold pci_bus_sem across sizing and assignment entrypoints
PCI: Serialize bridge window sizing and resource assignment
i82875p_edac: Convert to pci_assign_unassigned_bus_resources() platform/x86: asus-wmi: Convert to
pci_assign_unassigned_bus_resources()
platform/x86: eeepc-laptop: Convert to
pci_assign_unassigned_bus_resources()
parisc/PCI: Convert to pci_assign_unassigned_bus_resources()
PCI: hotplug: octep: Convert to pci_assign_unassigned_bus_resources()
PCI: ftpci100: Convert to pci_assign_unassigned_bus_resources()
PCI: hv: Convert to pci_assign_unassigned_bus_resources()
PCI: Remove pci_bus_assign_resources()

 arch/alpha/kernel/sys_nautilus.c      |  2 +-
 arch/arm/kernel/bios32.c              |  5 +-
 arch/m68k/coldfire/pci.c              |  3 +-
 arch/mips/pci/pci-legacy.c            |  5 +-
 arch/sh/drivers/pci/pci.c             |  3 +-
 drivers/edac/i82875p_edac.c           |  2 +-
 drivers/parisc/dino.c                 |  2 +-
 drivers/parisc/lba_pci.c              |  7 +--
 drivers/pci/controller/pci-ftpci100.c |  2 +-
 drivers/pci/controller/pci-hyperv.c   |  2 +-
 drivers/pci/hotplug/acpiphp_glue.c    |  4 +-
 drivers/pci/hotplug/octep_hp.c        |  2 +-
 drivers/pci/pci.h                     |  1 +
 drivers/pci/setup-bus.c               | 78 ++++++++++++++++++++++-----
 drivers/pci/setup-cardbus.c           |  3 ++
 drivers/pcmcia/cardbus.c              |  3 +-
 drivers/platform/x86/asus-wmi.c       |  2 +-
 drivers/platform/x86/eeepc-laptop.c   |  2 +-
 include/linux/pci.h                   |  2 +-
 19 files changed, 88 insertions(+), 42 deletions(-)

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4

Signature · 1 line
2.47.3
author maintainer
#1 lore raw

PATCH 1/13 PCI: Add pci_bus_size_and_assign_resources()

Bridge window sizing and resource assignment must not run concurrently with another sizing and assignment pass or with device removal. Preventing that requires holding locks across both phases. Many callers outside of PCI core call pci_bus_size_bridges() immediately followed by pci_bus_assign_resources() and cannot hold the PCI core internal locks across the two calls.

Add pci_bus_size_and_assign_resources() into PCI core which combines these two operations and convert the callers that call them back to back.

---
 arch/arm/kernel/bios32.c           | 5 ++---
 arch/m68k/coldfire/pci.c           | 3 +--
 arch/mips/pci/pci-legacy.c         | 5 ++---
 arch/sh/drivers/pci/pci.c          | 3 +--
 drivers/parisc/lba_pci.c           | 7 ++-----
 drivers/pci/hotplug/acpiphp_glue.c | 3 +--
 drivers/pci/setup-bus.c            | 7 +++++++
 drivers/pcmcia/cardbus.c           | 3 +--
 include/linux/pci.h                | 1 +
 9 files changed, 18 insertions(+), 19 deletions(-)
arch/arm/kernel/bios32.c +2 -3
index ac0e890510da..555bfb091d30 100644
@@ -510,15 +510,14 @@ void pci_common_init_dev(struct device *parent, struct hw_pci *hw)
 		/*
 		 * We insert PCI resources into the iomem_resource and
 		 * ioport_resource trees in either pci_bus_claim_resources()
-		 * or pci_bus_assign_resources().
+		 * or pci_bus_size_and_assign_resources().
 		 */
 		if (pci_has_flag(PCI_PROBE_ONLY)) {
 			pci_bus_claim_resources(bus);
 		} else {
 			struct pci_bus *child;
 
-			pci_bus_size_bridges(bus);
-			pci_bus_assign_resources(bus);
+			pci_bus_size_and_assign_resources(bus);
 
 			list_for_each_entry(child, &bus->children, node)
 				pcie_bus_configure_settings(child);
arch/m68k/coldfire/pci.c +1 -2
index 5afb207ccc51..dd21c77c43a6 100644
@@ -250,8 +250,7 @@ static int __init mcf_pci_init(void)
 	rootbus->resource[0] = &mcf_pci_io;
 	rootbus->resource[1] = &mcf_pci_mem;
 
-	pci_bus_size_bridges(rootbus);
-	pci_bus_assign_resources(rootbus);
+	pci_bus_size_and_assign_resources(rootbus);
 	pci_bus_add_devices(rootbus);
 	return 0;
 }
arch/mips/pci/pci-legacy.c +2 -3
index dae6dafdd6e0..ec0e42d9a895 100644
@@ -129,15 +129,14 @@ static void pcibios_scanbus(struct pci_controller *hose)
 	/*
 	 * We insert PCI resources into the iomem_resource and
 	 * ioport_resource trees in either pci_bus_claim_resources()
-	 * or pci_bus_assign_resources().
+	 * or pci_bus_size_and_assign_resources().
 	 */
 	if (pci_has_flag(PCI_PROBE_ONLY)) {
 		pci_bus_claim_resources(bus);
 	} else {
 		struct pci_bus *child;
 
-		pci_bus_size_bridges(bus);
-		pci_bus_assign_resources(bus);
+		pci_bus_size_and_assign_resources(bus);
 		list_for_each_entry(child, &bus->children, node)
 			pcie_bus_configure_settings(child);
 	}
arch/sh/drivers/pci/pci.c +1 -2
index 878a27a1acfb..c91ca8f4cb1c 100644
@@ -81,8 +81,7 @@ static void pcibios_scanbus(struct pci_channel *hose)
 		need_domain_info = 1;
 	}
 
-	pci_bus_size_bridges(hose->bus);
-	pci_bus_assign_resources(hose->bus);
+	pci_bus_size_and_assign_resources(hose->bus);
 	pci_bus_add_devices(hose->bus);
 }
 
drivers/parisc/lba_pci.c +2 -5
index 5197a3c93c70..39ab93ba458b 100644
@@ -1639,11 +1639,8 @@ lba_driver_probe(struct parisc_device *dev)
 	if (is_pdc_pat()) {
 		/* assign resources to un-initialized devices */
 
-		DBG_PAT("LBA pci_bus_size_bridges()\n");
-		pci_bus_size_bridges(lba_bus);
-
-		DBG_PAT("LBA pci_bus_assign_resources()\n");
-		pci_bus_assign_resources(lba_bus);
+		DBG_PAT("LBA pci_bus_size_and_assign_resources()\n");
+		pci_bus_size_and_assign_resources(lba_bus);
 
 #ifdef DEBUG_LBA_PAT
 		DBG_PAT("\nLBA PIOP resource tree\n");
drivers/pci/hotplug/acpiphp_glue.c +1 -2
index 78e3effbb32d..632344f34cae 100644
@@ -465,8 +465,7 @@ static void acpiphp_native_scan_bridge(struct pci_dev *bridge)
 		max = pci_scan_bridge(bus, dev, max, 1);
 		if (dev->subordinate) {
 			pcibios_resource_survey_bus(dev->subordinate);
-			pci_bus_size_bridges(dev->subordinate);
-			pci_bus_assign_resources(dev->subordinate);
+			pci_bus_size_and_assign_resources(dev->subordinate);
 		}
 	}
 }
drivers/pci/setup-bus.c +7 -0
index ed16ef7c26fa..29ec9ba59d92 100644
@@ -2492,3 +2492,10 @@ void pci_assign_unassigned_bus_resources(struct pci_bus *bus)
 	__pci_bus_assign_resources(bus, &add_list, NULL);
 }
 EXPORT_SYMBOL_GPL(pci_assign_unassigned_bus_resources);
+
+void pci_bus_size_and_assign_resources(struct pci_bus *bus)
+{
+	pci_bus_size_bridges(bus);
+	__pci_bus_assign_resources(bus, NULL, NULL);
+}
+EXPORT_SYMBOL_GPL(pci_bus_size_and_assign_resources);
drivers/pcmcia/cardbus.c +1 -2
index 5e5cf2c3e2c8..19df3f9228a7 100644
@@ -81,8 +81,7 @@ int __ref cb_alloc(struct pcmcia_socket *s)
 	/*
 	 * Size all resources below the CardBus controller.
 	 */
-	pci_bus_size_bridges(bus);
-	pci_bus_assign_resources(bus);
+	pci_bus_size_and_assign_resources(bus);
 	cardbus_config_irq_and_cls(bus, s->pci_irq);
 
 	/* socket specific tune function */
include/linux/pci.h +1 -0
index aa3765a20a96..d89aeb7c9ff7 100644
@@ -1569,6 +1569,7 @@ resource_size_t pcibios_retrieve_fw_addr(struct pci_dev *dev, int idx);
 void pci_bus_assign_resources(const struct pci_bus *bus);
 void pci_bus_claim_resources(struct pci_bus *bus);
 void pci_bus_size_bridges(struct pci_bus *bus);
+void pci_bus_size_and_assign_resources(struct pci_bus *bus);
 int pci_claim_resource(struct pci_dev *, int);
 int pci_claim_bridge_resource(struct pci_dev *bridge, int i);
 void pci_assign_unassigned_resources(void);
Signature · 1 line
2.47.3
#2 lore raw

Re: [PATCH 01/13] PCI: Add pci_bus_size_and_assign_resources()

Sashiko has reviewed this patch and found no issues.
Quoted 7 lines

Bridge window sizing and resource assignment must not run concurrently with another sizing and assignment pass or with device removal. Preventing that requires holding locks across both phases. Many callers outside of PCI core call pci_bus_size_bridges() immediately followed by pci_bus_assign_resources() and cannot hold the PCI core internal locks across the two calls.

Quoted 5 lines

Add pci_bus_size_and_assign_resources() into PCI core which combines these two operations and convert the callers that call them back to back.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Acked-by on v1 1/13
#3 lore raw

Re: [PATCH 01/13] PCI: Add pci_bus_size_and_assign_resources()

In reply to Ilpo Järvinen
Quoted 8 lines · Ilpo Järvinen

Bridge window sizing and resource assignment must not run concurrently with another sizing and assignment pass or with device removal. Preventing that requires holding locks across both phases. Many callers outside of PCI core call pci_bus_size_bridges() immediately followed by pci_bus_assign_resources() and cannot hold the PCI core internal locks across the two calls.

Quoted 5 lines

Add pci_bus_size_and_assign_resources() into PCI core which combines these two operations and convert the callers that call them back to back.

LGTM

Quoted 160 lines
---
 arch/arm/kernel/bios32.c           | 5 ++---
 arch/m68k/coldfire/pci.c           | 3 +--
 arch/mips/pci/pci-legacy.c         | 5 ++---
 arch/sh/drivers/pci/pci.c          | 3 +--
 drivers/parisc/lba_pci.c           | 7 ++-----
 drivers/pci/hotplug/acpiphp_glue.c | 3 +--
 drivers/pci/setup-bus.c            | 7 +++++++
 drivers/pcmcia/cardbus.c           | 3 +--
 include/linux/pci.h                | 1 +
 9 files changed, 18 insertions(+), 19 deletions(-)
arch/arm/kernel/bios32.c +2 -3
index ac0e890510da..555bfb091d30 100644
@@ -510,15 +510,14 @@ void pci_common_init_dev(struct device *parent, struct hw_pci *hw)
                /*
                 * We insert PCI resources into the iomem_resource and
                 * ioport_resource trees in either pci_bus_claim_resources()
-                * or pci_bus_assign_resources().
+                * or pci_bus_size_and_assign_resources().
                 */
                if (pci_has_flag(PCI_PROBE_ONLY)) {
                        pci_bus_claim_resources(bus);
                } else {
                        struct pci_bus *child;
 
-                       pci_bus_size_bridges(bus);
-                       pci_bus_assign_resources(bus);
+                       pci_bus_size_and_assign_resources(bus);
 
                        list_for_each_entry(child, &bus->children, node)
                                pcie_bus_configure_settings(child);
arch/m68k/coldfire/pci.c +1 -2
index 5afb207ccc51..dd21c77c43a6 100644
@@ -250,8 +250,7 @@ static int __init mcf_pci_init(void)
        rootbus->resource[0] = &mcf_pci_io;
        rootbus->resource[1] = &mcf_pci_mem;
 
-       pci_bus_size_bridges(rootbus);
-       pci_bus_assign_resources(rootbus);
+       pci_bus_size_and_assign_resources(rootbus);
        pci_bus_add_devices(rootbus);
        return 0;
 }
arch/mips/pci/pci-legacy.c +2 -3
index dae6dafdd6e0..ec0e42d9a895 100644
@@ -129,15 +129,14 @@ static void pcibios_scanbus(struct pci_controller *hose)
        /*
         * We insert PCI resources into the iomem_resource and
         * ioport_resource trees in either pci_bus_claim_resources()
-        * or pci_bus_assign_resources().
+        * or pci_bus_size_and_assign_resources().
         */
        if (pci_has_flag(PCI_PROBE_ONLY)) {
                pci_bus_claim_resources(bus);
        } else {
                struct pci_bus *child;
 
-               pci_bus_size_bridges(bus);
-               pci_bus_assign_resources(bus);
+               pci_bus_size_and_assign_resources(bus);
                list_for_each_entry(child, &bus->children, node)
                        pcie_bus_configure_settings(child);
        }
arch/sh/drivers/pci/pci.c +1 -2
index 878a27a1acfb..c91ca8f4cb1c 100644
@@ -81,8 +81,7 @@ static void pcibios_scanbus(struct pci_channel *hose)
                need_domain_info = 1;
        }
 
-       pci_bus_size_bridges(hose->bus);
-       pci_bus_assign_resources(hose->bus);
+       pci_bus_size_and_assign_resources(hose->bus);
        pci_bus_add_devices(hose->bus);
 }
 
drivers/parisc/lba_pci.c +2 -5
index 5197a3c93c70..39ab93ba458b 100644
@@ -1639,11 +1639,8 @@ lba_driver_probe(struct parisc_device *dev)
        if (is_pdc_pat()) {
                /* assign resources to un-initialized devices */
 
-               DBG_PAT("LBA pci_bus_size_bridges()\n");
-               pci_bus_size_bridges(lba_bus);
-
-               DBG_PAT("LBA pci_bus_assign_resources()\n");
-               pci_bus_assign_resources(lba_bus);
+               DBG_PAT("LBA pci_bus_size_and_assign_resources()\n");
+               pci_bus_size_and_assign_resources(lba_bus);
 
 #ifdef DEBUG_LBA_PAT
                DBG_PAT("\nLBA PIOP resource tree\n");
drivers/pci/hotplug/acpiphp_glue.c +1 -2
index 78e3effbb32d..632344f34cae 100644
@@ -465,8 +465,7 @@ static void acpiphp_native_scan_bridge(struct pci_dev *bridge)
                max = pci_scan_bridge(bus, dev, max, 1);
                if (dev->subordinate) {
                        pcibios_resource_survey_bus(dev->subordinate);
-                       pci_bus_size_bridges(dev->subordinate);
-                       pci_bus_assign_resources(dev->subordinate);
+                       pci_bus_size_and_assign_resources(dev->subordinate);
                }
        }
 }
drivers/pci/setup-bus.c +7 -0
index ed16ef7c26fa..29ec9ba59d92 100644
@@ -2492,3 +2492,10 @@ void pci_assign_unassigned_bus_resources(struct pci_bus *bus)
        __pci_bus_assign_resources(bus, &add_list, NULL);
 }
 EXPORT_SYMBOL_GPL(pci_assign_unassigned_bus_resources);
+
+void pci_bus_size_and_assign_resources(struct pci_bus *bus)
+{
+       pci_bus_size_bridges(bus);
+       __pci_bus_assign_resources(bus, NULL, NULL);
+}
+EXPORT_SYMBOL_GPL(pci_bus_size_and_assign_resources);
drivers/pcmcia/cardbus.c +1 -2
index 5e5cf2c3e2c8..19df3f9228a7 100644
@@ -81,8 +81,7 @@ int __ref cb_alloc(struct pcmcia_socket *s)
        /*
         * Size all resources below the CardBus controller.
         */
-       pci_bus_size_bridges(bus);
-       pci_bus_assign_resources(bus);
+       pci_bus_size_and_assign_resources(bus);
        cardbus_config_irq_and_cls(bus, s->pci_irq);
 
        /* socket specific tune function */
include/linux/pci.h +1 -1
index aa3765a20a96..d89aeb7c9ff7 100644
@@ -1569,6 +1569,7 @@ resource_size_t pcibios_retrieve_fw_addr(struct pci_dev *dev, int idx);
 void pci_bus_assign_resources(const struct pci_bus *bus);
 void pci_bus_claim_resources(struct pci_bus *bus);
 void pci_bus_size_bridges(struct pci_bus *bus);
+void pci_bus_size_and_assign_resources(struct pci_bus *bus);
 int pci_claim_resource(struct pci_dev *, int);
 int pci_claim_bridge_resource(struct pci_dev *bridge, int i);
 void pci_assign_unassigned_resources(void);
--

2.47.3

author maintainer
#4 lore raw

PATCH 2/13 alpha/PCI: Convert to pci_bus_size_and_assign_resources()

In reply to Ilpo Järvinen

To serialize bridge window sizing and resource assignment, PCI core needs to hold locks across both phases. Alpha nautilus PCI code calls pci_bus_size_bridges() and pci_bus_assign_resources() separately because it needs to do special resource alignment and setup after the window sizes have been calculated. At least the former would seem to belong to some existing __weak pcibios_*() hook but the latter might need yet another pcibios_*() hook just for this single use.

The nautilus code runs during init, so nothing is likely to race with it. Converting it is mainly about consolidating the sizing and assignment entry points of PCI core, which eventually allows removing pci_bus_assign_resources().

Instead of trying to do a complex rework of the code, which is risky without the ability to test on the hardware, simply convert pci_bus_assign_resources() to pci_bus_size_and_assign_resources(). This adds a small bit of extra work by performing the sizing step again, but the calculations should yield the same sizes, so it seems safe.

---
 arch/alpha/kernel/sys_nautilus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/alpha/kernel/sys_nautilus.c +1 -1
index 13b79960b4b9..23a74fd4a83f 100644
@@ -263,7 +263,7 @@ nautilus_init_pci(void)
 	if ((IRONGATE0->dev_vendor >> 16) > 0x7006)	/* Albacore? */
 		IRONGATE0->pci_mem = pci_mem;
 
-	pci_bus_assign_resources(bus);
+	pci_bus_size_and_assign_resources(bus);
 	pci_bus_add_devices(bus);
 }
 
Signature · 1 line
2.47.3
#5 lore raw

Re: [PATCH 02/13] alpha/PCI: Convert to pci_bus_size_and_assign_resources()

Sashiko has reviewed this patch and found no issues.
Quoted 13 lines

To serialize bridge window sizing and resource assignment, PCI core needs to hold locks across both phases. Alpha nautilus PCI code calls pci_bus_size_bridges() and pci_bus_assign_resources() separately because it needs to do special resource alignment and setup after the window sizes have been calculated. At least the former would seem to belong to some existing __weak pcibios_*() hook but the latter might need yet another pcibios_*() hook just for this single use.

The nautilus code runs during init, so nothing is likely to race with it. Converting it is mainly about consolidating the sizing and assignment entry points of PCI core, which eventually allows removing pci_bus_assign_resources().

Quoted 7 lines

Instead of trying to do a complex rework of the code, which is risky without the ability to test on the hardware, simply convert pci_bus_assign_resources() to pci_bus_size_and_assign_resources(). This adds a small bit of extra work by performing the sizing step again, but the calculations should yield the same sizes, so it seems safe.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
maintainer Acked-by on v1 2/13
#6 lore raw

Re: [PATCH 02/13] alpha/PCI: Convert to pci_bus_size_and_assign_resources()

In reply to Ilpo Järvinen

Hi Ilpo,

Quoted 14 lines · Ilpo Järvinen

To serialize bridge window sizing and resource assignment, PCI core needs to hold locks across both phases. Alpha nautilus PCI code calls pci_bus_size_bridges() and pci_bus_assign_resources() separately because it needs to do special resource alignment and setup after the window sizes have been calculated. At least the former would seem to belong to some existing __weak pcibios_*() hook but the latter might need yet another pcibios_*() hook just for this single use.

The nautilus code runs during init, so nothing is likely to race with it. Converting it is mainly about consolidating the sizing and assignment entry points of PCI core, which eventually allows removing pci_bus_assign_resources().

Quoted 6 lines

Instead of trying to do a complex rework of the code, which is risky without the ability to test on the hardware, simply convert pci_bus_assign_resources() to pci_bus_size_and_assign_resources(). This adds a small bit of extra work by performing the sizing step again, but the calculations should yield the same sizes, so it seems safe

Agreed. By the second pass irongate_mem has been request_resource()d, so pbus_size_mem() skips it as assigned, and ioport_resource is never picked as a window (find_bus_resource_of_type()). Only the AGP bridge's unassigned windows are sized again, from the same inputs.

That does rely on the assigned check, though: if your follow-up series starts sizing assigned windows, nautilus' relocated root window (size_windows = 1) must stay untouched by the second pass.

It builds at every patch for the generic and Nautilus configs. I have a UP1100 (Nautilus), but it doesn't start up at the moment; if I get it running again I'll give this a boot. But for the Alpha part of this series:

Quoted 21 lines
---
 arch/alpha/kernel/sys_nautilus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/alpha/kernel/sys_nautilus.c +1 -2
index 13b79960b4b9..23a74fd4a83f 100644
@@ -263,7 +263,7 @@ nautilus_init_pci(void)
        if ((IRONGATE0->dev_vendor >> 16) > 0x7006)     /* Albacore? */
                IRONGATE0->pci_mem = pci_mem;
 
-       pci_bus_assign_resources(bus);
+       pci_bus_size_and_assign_resources(bus);
        pci_bus_add_devices(bus);
 }
 
--

2.47.3

author maintainer
#7 lore raw

Re: [PATCH 02/13] alpha/PCI: Convert to pci_bus_size_and_assign_resources()

Quoted 23 lines · Magnus Lindholm
Quoted 14 lines · Ilpo Järvinen

To serialize bridge window sizing and resource assignment, PCI core needs to hold locks across both phases. Alpha nautilus PCI code calls pci_bus_size_bridges() and pci_bus_assign_resources() separately because it needs to do special resource alignment and setup after the window sizes have been calculated. At least the former would seem to belong to some existing __weak pcibios_*() hook but the latter might need yet another pcibios_*() hook just for this single use.

The nautilus code runs during init, so nothing is likely to race with it. Converting it is mainly about consolidating the sizing and assignment entry points of PCI core, which eventually allows removing pci_bus_assign_resources().

Quoted 6 lines

Instead of trying to do a complex rework of the code, which is risky without the ability to test on the hardware, simply convert pci_bus_assign_resources() to pci_bus_size_and_assign_resources(). This adds a small bit of extra work by performing the sizing step again, but the calculations should yield the same sizes, so it seems safe

Quoted 7 lines

Agreed. By the second pass irongate_mem has been request_resource()d, so pbus_size_mem() skips it as assigned, and ioport_resource is never picked as a window (find_bus_resource_of_type()). Only the AGP bridge's unassigned windows are sized again, from the same inputs.

That does rely on the assigned check, though: if your follow-up series starts sizing assigned windows,

Yes, it will start to do that then.

Quoted 2 lines

nautilus' relocated root window
(size_windows = 1) must stay untouched by the second pass.

If the same size is calculated (during init, I see no reason why the sizing results would change), there should be no change even when it starts to assign windows.

I'm bit worried though when it comes to the extra alignment done by this nautilus code as that can change the size from the original but I don't think there is anything that would allow shrinking the window even with that so it looks okay to me even after the upcoming series.

But it might be easiest to just set IORESOURCE_PCI_FIXED flag after the resource has been added to the tree. That should keep the usual sizing and assignment from touching it at all.

Quoted 3 lines

It builds at every patch for the generic and Nautilus configs. I have a UP1100 (Nautilus), but it doesn't start up at the moment; if I get it running again I'll give this a boot. But for the Alpha part of this series:

Oh, that would potentially make it possible to actually try to refactor this to happen in pcibios_*() code, which I certainly dare not without ability test the change.

Quoted 1 line

Thanks a lot for taking a look and all the insight, it's really appreciated.

Quoted 22 lines
Quoted 21 lines
---
 arch/alpha/kernel/sys_nautilus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/alpha/kernel/sys_nautilus.c +1 -2
index 13b79960b4b9..23a74fd4a83f 100644
@@ -263,7 +263,7 @@ nautilus_init_pci(void)
        if ((IRONGATE0->dev_vendor >> 16) > 0x7006)     /* Albacore? */
                IRONGATE0->pci_mem = pci_mem;
 
-       pci_bus_assign_resources(bus);
+       pci_bus_size_and_assign_resources(bus);
        pci_bus_add_devices(bus);
 }
 
--

2.47.3

Signature · 1 line
 i.
author maintainer
#8 lore raw

Re: [PATCH 02/13] alpha/PCI: Convert to pci_bus_size_and_assign_resources()

Quoted 41 lines · Ilpo Järvinen
Quoted 23 lines · Magnus Lindholm
Quoted 14 lines · Ilpo Järvinen

To serialize bridge window sizing and resource assignment, PCI core needs to hold locks across both phases. Alpha nautilus PCI code calls pci_bus_size_bridges() and pci_bus_assign_resources() separately because it needs to do special resource alignment and setup after the window sizes have been calculated. At least the former would seem to belong to some existing __weak pcibios_*() hook but the latter might need yet another pcibios_*() hook just for this single use.

The nautilus code runs during init, so nothing is likely to race with it. Converting it is mainly about consolidating the sizing and assignment entry points of PCI core, which eventually allows removing pci_bus_assign_resources().

Quoted 6 lines

Instead of trying to do a complex rework of the code, which is risky without the ability to test on the hardware, simply convert pci_bus_assign_resources() to pci_bus_size_and_assign_resources(). This adds a small bit of extra work by performing the sizing step again, but the calculations should yield the same sizes, so it seems safe

Quoted 7 lines

Agreed. By the second pass irongate_mem has been request_resource()d, so pbus_size_mem() skips it as assigned, and ioport_resource is never picked as a window (find_bus_resource_of_type()). Only the AGP bridge's unassigned windows are sized again, from the same inputs.

That does rely on the assigned check, though: if your follow-up series starts sizing assigned windows,

Yes, it will start to do that then.

Quoted 2 lines

nautilus' relocated root window
(size_windows = 1) must stay untouched by the second pass.

If the same size is calculated (during init, I see no reason why the sizing results would change), there should be no change even when it starts to assign windows.

Quoted 8 lines

I'm bit worried though when it comes to the extra alignment done by this nautilus code as that can change the size from the original but I don't think there is anything that would allow shrinking the window even with that so it looks okay to me even after the upcoming series.

But it might be easiest to just set IORESOURCE_PCI_FIXED flag after the resource has been added to the tree. That should keep the usual sizing and assignment from touching it at all.

For the record, I realized this won't be necessary because nothing in the fitting and assignment algorithm can change the resource once it has been assigned.

All direct resource changes are (or should be) blocked for assigned resources to not corrupt the resource tree and root bus resources don't have bus->self so they never go into lists that need bus->self (add_list/realloc_head and the upcoming bridge_win_list).

Quoted 37 lines
Quoted 3 lines

It builds at every patch for the generic and Nautilus configs. I have a UP1100 (Nautilus), but it doesn't start up at the moment; if I get it running again I'll give this a boot. But for the Alpha part of this series:

Oh, that would potentially make it possible to actually try to refactor this to happen in pcibios_*() code, which I certainly dare not without ability test the change.

Quoted 1 line

Thanks a lot for taking a look and all the insight, it's really appreciated.

Quoted 22 lines
Quoted 21 lines
---
 arch/alpha/kernel/sys_nautilus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/alpha/kernel/sys_nautilus.c +1 -2
index 13b79960b4b9..23a74fd4a83f 100644
@@ -263,7 +263,7 @@ nautilus_init_pci(void)
        if ((IRONGATE0->dev_vendor >> 16) > 0x7006)     /* Albacore? */
                IRONGATE0->pci_mem = pci_mem;
 
-       pci_bus_assign_resources(bus);
+       pci_bus_size_and_assign_resources(bus);
        pci_bus_add_devices(bus);
 }
 
--

2.47.3

Signature · 1 line
 i.
author maintainer
#9 lore raw

PATCH 3/13 PCI: Use guard(&pci_bus_sem) in pci_do_resource_release_and_resize()

In reply to Ilpo Järvinen

pci_do_resource_release_and_resize() releases pci_bus_sem on its common exit path. Use guard() instead to make adding more locking simpler.

The saved resource list is now freed while pci_bus_sem is held, which is harmless as the list is local to the function.

---
 drivers/pci/setup-bus.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/pci/setup-bus.c +1 -2
index 29ec9ba59d92..f95f217f94e6 100644
@@ -2398,7 +2398,7 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
 	if (ret)
 		return ret;
 
-	down_read(&pci_bus_sem);
+	guard(rwsem_read)(&pci_bus_sem);
 
 	pci_dev_for_each_resource(pdev, r, i) {
 		if (i >= PCI_BRIDGE_RESOURCES)
@@ -2435,7 +2435,6 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
 	}
 
 out:
-	up_read(&pci_bus_sem);
 	pci_dev_res_free_list(&saved);
 	return ret;
 
Signature · 1 line
2.47.3
#10 lore raw

Re: [PATCH 03/13] PCI: Use guard(&pci_bus_sem) in pci_do_resource_release_and_resize()

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] PCI: guard() mixed with goto cleanup in pci_do_resource_release_and_resize() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] PCI: guard() mixed with goto cleanup in pci_do_resource_release_and_resize()
--

commit 07aa8866ad5508b25df182c6f66b51bc1b05efd3
Author: Ilpo Järvinen <[email protected]>

PCI: Use guard(&pci_bus_sem) in pci_do_resource_release_and_resize()

This commit replaces the manual down_read() and up_read() calls on pci_bus_sem in pci_do_resource_release_and_resize() with guard(rwsem_read).

Quoted 14 lines
drivers/pci/setup-bus.c +1 -1
index 29ec9ba59d928..f95f217f94e69 100644
@@ -2398,7 +2398,7 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
 	if (ret)
 		return ret;
 
-	down_read(&pci_bus_sem);
+	guard(rwsem_read)(&pci_bus_sem);
 
 	pci_dev_for_each_resource(pdev, r, i) {
 		if (i >= PCI_BRIDGE_RESOURCES)

drivers/pci/setup-bus.c int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size · line 2440

Quoted 7 lines
drivers/pci/setup-bus.c +0 -1
@@ -2435,7 +2435,6 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
 	}
 
 out:
-	up_read(&pci_bus_sem);
 	pci_dev_res_free_list(&saved);
 	return ret;
 

[Severity: Low]
This isn't a bug, but using guard(rwsem_read) here mixes scope-based cleanup with the existing goto restore and goto out unwind paths in pci_do_resource_release_and_resize(), contrary to the include/linux/cleanup.h guidelines against mixing guard() and goto error handling in the same function.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author maintainer
#11 lore raw

PATCH 4/13 PCI: Hold pci_bus_sem across sizing and assignment entrypoints

In reply to Ilpo Järvinen

Bridge window sizing and resource assignment algorithm walks bus->devices and insert resources of the found devices into the resource tree in many places. In addition, its internal datastructures hold references to devices and resource it encountered while walking.

Most entrypoints to the algorithm do not hold pci_bus_sem, and pci_assign_unassigned_bus_resources() drops it before the assignment phase.

pci_rescan_remove_lock is not held by all callers and cannot be taken by callers running under device_lock().

Protect the walks by holding pci_bus_sem for read across the entire sizing and assignment operation right at the entrypoints.

---
 drivers/pci/setup-bus.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
drivers/pci/setup-bus.c +19 -7
index f95f217f94e6..5b2d870ca13d 100644
@@ -1457,6 +1457,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(rwsem_read)(&pci_bus_sem);
 	__pci_bus_size_bridges(bus, NULL);
 }
 EXPORT_SYMBOL(pci_bus_size_bridges);
@@ -1549,6 +1550,7 @@ void __pci_bus_assign_resources(const struct pci_bus *bus,
 
 void pci_bus_assign_resources(const struct pci_bus *bus)
 {
+	guard(rwsem_read)(&pci_bus_sem);
 	__pci_bus_assign_resources(bus, NULL, NULL);
 }
 EXPORT_SYMBOL(pci_bus_assign_resources);
@@ -1837,7 +1839,7 @@ static enum enable_type pci_realloc_detect(struct pci_bus *bus,
 	if (host->preserve_config)
 		return auto_disabled;
 
-	pci_walk_bus(bus, iov_resources_unassigned, &unassigned);
+	pci_walk_bus_locked(bus, iov_resources_unassigned, &unassigned);
 	if (unassigned)
 		return auto_enabled;
 
@@ -2190,6 +2192,8 @@ void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus)
 	int pci_try_num = 1;
 	enum enable_type enable_local;
 
+	guard(rwsem_read)(&pci_bus_sem);
+
 	/* Don't realloc if asked to do so */
 	enable_local = pci_realloc_detect(bus, pci_realloc_enable);
 	if (pci_realloc_enabled(enable_local)) {
@@ -2258,14 +2262,15 @@ void pci_assign_unassigned_resources(void)
 	}
 }
 
-void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
+static void pci_bridge_size_and_assign_resources(struct pci_dev *bridge)
 {
 	struct pci_bus *parent = bridge->subordinate;
 	/* List of resources that want additional resources */
 	LIST_HEAD(add_list);
 	int tried_times = 0;
 	LIST_HEAD(fail_head);
-	int ret;
+
+	guard(rwsem_read)(&pci_bus_sem);
 
 	while (1) {
 		__pci_bus_size_bridges(parent, &add_list);
@@ -2292,6 +2297,13 @@ void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
 		pci_prepare_next_assign_round(&fail_head, tried_times,
 					      whole_subtree);
 	}
+}
+
+void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
+{
+	int ret;
+
+	pci_bridge_size_and_assign_resources(bridge);
 
 	ret = pci_reenable_device(bridge);
 	if (ret)
@@ -2424,7 +2436,7 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
 			goto restore;
 	} else {
 		/* No bridge window to adjust; let the core reassign the bus. */
-		pci_bus_assign_resources(bus);
+		__pci_bus_assign_resources(bus, NULL, NULL);
 
 		list_for_each_entry(dev_res, &saved, list) {
 			if (!resource_assigned(dev_res->res)) {
@@ -2483,18 +2495,18 @@ void pci_assign_unassigned_bus_resources(struct pci_bus *bus)
 	/* List of resources that want additional resources */
 	LIST_HEAD(add_list);
 
-	down_read(&pci_bus_sem);
+	guard(rwsem_read)(&pci_bus_sem);
 	for_each_pci_bridge(dev, bus)
 		if (pci_has_subordinate(dev))
 			__pci_bus_size_bridges(dev->subordinate, &add_list);
-	up_read(&pci_bus_sem);
 	__pci_bus_assign_resources(bus, &add_list, NULL);
 }
 EXPORT_SYMBOL_GPL(pci_assign_unassigned_bus_resources);
 
 void pci_bus_size_and_assign_resources(struct pci_bus *bus)
 {
-	pci_bus_size_bridges(bus);
+	guard(rwsem_read)(&pci_bus_sem);
+	__pci_bus_size_bridges(bus, NULL);
 	__pci_bus_assign_resources(bus, NULL, NULL);
 }
 EXPORT_SYMBOL_GPL(pci_bus_size_and_assign_resources);
Signature · 1 line
2.47.3
#12 lore raw

Re: [PATCH 04/13] PCI: Hold pci_bus_sem across sizing and assignment entrypoints

Sashiko has reviewed this patch and found no issues.
Quoted 9 lines

Bridge window sizing and resource assignment algorithm walks bus->devices and insert resources of the found devices into the resource tree in many places. In addition, its internal datastructures hold references to devices and resource it encountered while walking.

Most entrypoints to the algorithm do not hold pci_bus_sem, and pci_assign_unassigned_bus_resources() drops it before the assignment phase.

Quoted 7 lines

pci_rescan_remove_lock is not held by all callers and cannot be taken by callers running under device_lock().

Protect the walks by holding pci_bus_sem for read across the entire sizing and assignment operation right at the entrypoints.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author maintainer
#13 lore raw

PATCH 5/13 PCI: Serialize bridge window sizing and resource assignment

In reply to Ilpo Järvinen

Bridge window sizing and resource assignment are started from multiple entry points: rescan and hotplug under pci_rescan_remove_lock, and BAR resize from sysfs or driver probe under device_lock() only. The entry points 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 pci_res_setup_lock and hold it across sizing and assignment in all entry points, including acpiphp which calls the internal sizing and assignment functions directly. Assert the lock is held in the functions that modify resources deeper in the call chains. 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")

---

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/hotplug/acpiphp_glue.c |  1 +
 drivers/pci/pci.h                  |  1 +
 drivers/pci/setup-bus.c            | 40 ++++++++++++++++++++++++++++++
 drivers/pci/setup-cardbus.c        |  3 +++
 4 files changed, 45 insertions(+)
drivers/pci/hotplug/acpiphp_glue.c +1 -0
index 632344f34cae..d3987be044e3 100644
@@ -502,6 +502,7 @@ static void enable_slot(struct acpiphp_slot *slot, bool bridge)
 
 		acpiphp_rescan_slot(slot);
 		max = acpiphp_max_busnr(bus);
+		guard(mutex)(&pci_res_setup_lock);
 		for (pass = 0; pass < 2; pass++) {
 			for_each_pci_bridge(dev, bus) {
 				if (PCI_SLOT(dev->devfn) != slot->device)
drivers/pci/pci.h +1 -0
index 77efa4c5c960..660a83b0045a 100644
@@ -124,6 +124,7 @@ unsigned char pcie_get_link_speed(unsigned int speed);
 extern bool pci_early_dump;
 
 extern struct mutex pci_rescan_remove_lock;
+extern struct mutex pci_res_setup_lock;
 
 bool pcie_cap_has_lnkctl(const struct pci_dev *dev);
 bool pcie_cap_has_lnkctl2(const struct pci_dev *dev);
drivers/pci/setup-bus.c +40 -0
index 5b2d870ca13d..314c649c648f 100644
@@ -26,6 +26,7 @@
 #include <linux/ioport.h>
 #include <linux/cache.h>
 #include <linux/limits.h>
+#include <linux/lockdep.h>
 #include <linux/sizes.h>
 #include <linux/slab.h>
 #include <linux/acpi.h>
@@ -38,6 +39,17 @@
 unsigned int pci_flags;
 EXPORT_SYMBOL_GPL(pci_flags);
 
+/*
+ * Serializes bridge window sizing, resource fitting, and assignment.
+ * pci_rescan_remove_lock cannot be used for this because BAR resize and
+ * some drivers run these operations under device_lock() during probe.
+ *
+ * Context:
+ *  - Nests inside pci_rescan_remove_lock and device_lock().
+ *  - Nests outside pci_bus_sem.
+ */
+DEFINE_MUTEX(pci_res_setup_lock);
+
 struct pci_dev_resource {
 	struct list_head list;
 	struct resource *res;
@@ -133,6 +145,8 @@ static void pci_dev_res_restore(struct pci_dev_resource *dev_res)
 	int idx = pci_resource_num(dev, res);
 	const char *res_name = pci_resource_name(dev, idx);
 
+	lockdep_assert_held(&pci_res_setup_lock);
+
 	if (WARN_ON_ONCE(resource_assigned(res)))
 		return;
 
@@ -611,6 +625,8 @@ static void __assign_resources_sorted(struct list_head *head,
 	unsigned long fail_type;
 	resource_size_t align;
 
+	lockdep_assert_held(&pci_res_setup_lock);
+
 	if (!realloc_head)
 		realloc_head = &dummy_head;
 
@@ -893,6 +909,8 @@ static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
 {
 	struct pci_dev *bridge = bus->self;
 
+	lockdep_assert_held(&pci_res_setup_lock);
+
 	pci_info(bridge, "PCI bridge to %pR\n", &bus->busn_res);
 
 	if (type & IORESOURCE_IO)
@@ -973,6 +991,8 @@ static void pci_bridge_check_ranges(struct pci_bus *bus)
 	struct pci_dev *bridge = bus->self;
 	struct resource *b_res;
 
+	lockdep_assert_held(&pci_res_setup_lock);
+
 	b_res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW];
 	b_res->flags |= IORESOURCE_MEM;
 
@@ -1077,6 +1097,8 @@ static void pbus_size_io(struct pci_bus *bus, resource_size_t add_size,
 	resource_size_t children_add_size = 0;
 	resource_size_t min_align, align;
 
+	lockdep_assert_held(&pci_res_setup_lock);
+
 	if (!b_res)
 		return;
 
@@ -1286,6 +1308,8 @@ static void pbus_size_mem(struct pci_bus *bus, struct resource *b_res,
 	resource_size_t children_add_size = 0;
 	resource_size_t add_align = 0;
 
+	lockdep_assert_held(&pci_res_setup_lock);
+
 	if (!b_res)
 		return;
 
@@ -1457,6 +1481,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);
 	guard(rwsem_read)(&pci_bus_sem);
 	__pci_bus_size_bridges(bus, NULL);
 }
@@ -1486,6 +1511,8 @@ static void pdev_assign_fixed_resources(struct pci_dev *dev)
 {
 	struct resource *r;
 
+	lockdep_assert_held(&pci_res_setup_lock);
+
 	pci_dev_for_each_resource(dev, r) {
 		struct pci_bus *b;
 
@@ -1550,6 +1577,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);
 	guard(rwsem_read)(&pci_bus_sem);
 	__pci_bus_assign_resources(bus, NULL, NULL);
 }
@@ -1663,6 +1691,8 @@ static void pci_bridge_release_resources(struct pci_bus *bus,
 	struct pci_dev *dev = bus->self;
 	int idx, ret;
 
+	lockdep_assert_held(&pci_res_setup_lock);
+
 	if (!resource_assigned(b_win))
 		return;
 
@@ -1860,6 +1890,8 @@ static void adjust_bridge_window(struct pci_dev *bridge, struct resource *res,
 	resource_size_t add_size, size = resource_size(res);
 	struct pci_dev_resource *dev_res;
 
+	lockdep_assert_held(&pci_res_setup_lock);
+
 	if (resource_assigned(res))
 		return;
 
@@ -2192,6 +2224,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);
 	guard(rwsem_read)(&pci_bus_sem);
 
 	/* Don't realloc if asked to do so */
@@ -2270,6 +2303,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);
 	guard(rwsem_read)(&pci_bus_sem);
 
 	while (1) {
@@ -2328,6 +2362,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);
+
 	while (!pci_is_root_bus(bus)) {
 		bridge = bus->self;
 		res = pbus_select_window(bus, res);
@@ -2398,6 +2434,8 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
 	unsigned int i;
 	int old, ret;
 
+	guard(mutex)(&pci_res_setup_lock);
+
 	b_win = pbus_select_window(bus, res);
 	if (!b_win)
 		return -EINVAL;
@@ -2495,6 +2533,7 @@ void pci_assign_unassigned_bus_resources(struct pci_bus *bus)
 	/* List of resources that want additional resources */
 	LIST_HEAD(add_list);
 
+	guard(mutex)(&pci_res_setup_lock);
 	guard(rwsem_read)(&pci_bus_sem);
 	for_each_pci_bridge(dev, bus)
 		if (pci_has_subordinate(dev))
@@ -2505,6 +2544,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);
 	guard(rwsem_read)(&pci_bus_sem);
 	__pci_bus_size_bridges(bus, NULL);
 	__pci_bus_assign_resources(bus, NULL, NULL);
drivers/pci/setup-cardbus.c +3 -0
index dcb8e2b3f73b..6a473a1694b9 100644
@@ -6,6 +6,7 @@
 #include <linux/bitfield.h>
 #include <linux/errno.h>
 #include <linux/ioport.h>
+#include <linux/lockdep.h>
 #include <linux/pci.h>
 #include <linux/sizes.h>
 #include <linux/sprintf.h>
@@ -39,6 +40,8 @@ int pci_bus_size_cardbus_bridge(struct pci_bus *bus,
 	resource_size_t b_res_3_size = pci_cardbus_mem_size * 2;
 	u16 ctrl;
 
+	lockdep_assert_held(&pci_res_setup_lock);
+
 	b_res = &bridge->resource[PCI_CB_BRIDGE_IO_0_WINDOW];
 	if (resource_assigned(b_res))
 		goto handle_b_res_1;
Signature · 1 line
2.47.3
#14 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] PCI: acpiphp: enable_slot() holds pci_res_setup_lock across pci_scan_bridge() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] PCI: acpiphp: enable_slot() holds pci_res_setup_lock across pci_scan_bridge()
--

commit 25d4f4c5afed80a01b7847608ddc4982cc9fd58d
Author: Ilpo Järvinen <[email protected]>

PCI: Serialize bridge window sizing and resource assignment

This commit introduces pci_res_setup_lock to serialize bridge window sizing and resource assignment across rescan, hotplug, and BAR resize paths.

drivers/pci/hotplug/acpiphp_glue.c static void enable_slot(struct acpiphp_slot *slot, bool bridge) · line 508

Quoted 12 lines
drivers/pci/hotplug/acpiphp_glue.c +1 -0
index 632344f34caed..d3987be044e33 100644
@@ -502,6 +502,7 @@ static void enable_slot(struct acpiphp_slot *slot, bool bridge)
 
 		acpiphp_rescan_slot(slot);
 		max = acpiphp_max_busnr(bus);
+		guard(mutex)(&pci_res_setup_lock);
 		for (pass = 0; pass < 2; pass++) {
 			for_each_pci_bridge(dev, bus) {
 				if (PCI_SLOT(dev->devfn) != slot->device)

[Severity: Medium]
Does taking pci_res_setup_lock before the pci_scan_bridge() loop invert the lock hierarchy with device_lock()?

pci_res_setup_lock is documented as nesting inside device_lock() because __resource_resize_store() and driver probe hold device_lock(dev) when calling pci_do_resource_release_and_resize(). However, pci_scan_bridge() discovers child devices and calls pci_device_add() -> device_add(), which exposes the resourceN_resize sysfs attributes in bus_add_device() before acquiring device_lock(dev) for dev_set_ready_to_probe() and bus_probe_device():

CPU0 CPU1
---- ----
enable_slot()
lock(&pci_res_setup_lock)
pci_scan_bridge()

    ...
      device_add()
        bus_add_device()
                                        __resource_resize_store()
                                          device_lock(dev)
                                          ...
                                            lock(&pci_res_setup_lock)
        device_lock(dev)

*** DEADLOCK ***

Could pci_res_setup_lock be taken only around the __pci_bus_size_bridges() and __pci_bus_assign_resources() calls after pci_scan_bridge() has completed?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
maintainer Acked-by on v1 5/13
#15 lore raw
In reply to Ilpo Järvinen
Quoted 127 lines · Ilpo Järvinen

Bridge window sizing and resource assignment are started from multiple entry points: rescan and hotplug under pci_rescan_remove_lock, and BAR resize from sysfs or driver probe under device_lock() only. The entry points 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 pci_res_setup_lock and hold it across sizing and assignment in all entry points, including acpiphp which calls the internal sizing and assignment functions directly. Assert the lock is held in the functions that modify resources deeper in the call chains. 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
Quoted 4 lines
Fixes 8bb705e3e79d ("PCI: Add pci_resize_resource() for resizing BARs")
Fixes 91fa127794ac ("PCI: Expose PCIe Resizable BAR support via sysfs")

LGTM

Quoted 243 lines

---

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/hotplug/acpiphp_glue.c |  1 +
 drivers/pci/pci.h                  |  1 +
 drivers/pci/setup-bus.c            | 40 ++++++++++++++++++++++++++++++
 drivers/pci/setup-cardbus.c        |  3 +++
 4 files changed, 45 insertions(+)
drivers/pci/hotplug/acpiphp_glue.c +1 -0
index 632344f34cae..d3987be044e3 100644
@@ -502,6 +502,7 @@ static void enable_slot(struct acpiphp_slot *slot, bool bridge)
 
                acpiphp_rescan_slot(slot);
                max = acpiphp_max_busnr(bus);
+               guard(mutex)(&pci_res_setup_lock);
                for (pass = 0; pass < 2; pass++) {
                        for_each_pci_bridge(dev, bus) {
                                if (PCI_SLOT(dev->devfn) != slot->device)
drivers/pci/pci.h +1 -0
index 77efa4c5c960..660a83b0045a 100644
@@ -124,6 +124,7 @@ unsigned char pcie_get_link_speed(unsigned int speed);
 extern bool pci_early_dump;
 
 extern struct mutex pci_rescan_remove_lock;
+extern struct mutex pci_res_setup_lock;
 
 bool pcie_cap_has_lnkctl(const struct pci_dev *dev);
 bool pcie_cap_has_lnkctl2(const struct pci_dev *dev);
drivers/pci/setup-bus.c +40 -0
index 5b2d870ca13d..314c649c648f 100644
@@ -26,6 +26,7 @@
 #include <linux/ioport.h>
 #include <linux/cache.h>
 #include <linux/limits.h>
+#include <linux/lockdep.h>
 #include <linux/sizes.h>
 #include <linux/slab.h>
 #include <linux/acpi.h>
@@ -38,6 +39,17 @@
 unsigned int pci_flags;
 EXPORT_SYMBOL_GPL(pci_flags);
 
+/*
+ * Serializes bridge window sizing, resource fitting, and assignment.
+ * pci_rescan_remove_lock cannot be used for this because BAR resize and
+ * some drivers run these operations under device_lock() during probe.
+ *
+ * Context:
+ *  - Nests inside pci_rescan_remove_lock and device_lock().
+ *  - Nests outside pci_bus_sem.
+ */
+DEFINE_MUTEX(pci_res_setup_lock);
+
 struct pci_dev_resource {
        struct list_head list;
        struct resource *res;
@@ -133,6 +145,8 @@ static void pci_dev_res_restore(struct pci_dev_resource *dev_res)
        int idx = pci_resource_num(dev, res);
        const char *res_name = pci_resource_name(dev, idx);
 
+       lockdep_assert_held(&pci_res_setup_lock);
+
        if (WARN_ON_ONCE(resource_assigned(res)))
                return;
 
@@ -611,6 +625,8 @@ static void __assign_resources_sorted(struct list_head *head,
        unsigned long fail_type;
        resource_size_t align;
 
+       lockdep_assert_held(&pci_res_setup_lock);
+
        if (!realloc_head)
                realloc_head = &dummy_head;
 
@@ -893,6 +909,8 @@ static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
 {
        struct pci_dev *bridge = bus->self;
 
+       lockdep_assert_held(&pci_res_setup_lock);
+
        pci_info(bridge, "PCI bridge to %pR\n", &bus->busn_res);
 
        if (type & IORESOURCE_IO)
@@ -973,6 +991,8 @@ static void pci_bridge_check_ranges(struct pci_bus *bus)
        struct pci_dev *bridge = bus->self;
        struct resource *b_res;
 
+       lockdep_assert_held(&pci_res_setup_lock);
+
        b_res = &bridge->resource[PCI_BRIDGE_MEM_WINDOW];
        b_res->flags |= IORESOURCE_MEM;
 
@@ -1077,6 +1097,8 @@ static void pbus_size_io(struct pci_bus *bus, resource_size_t add_size,
        resource_size_t children_add_size = 0;
        resource_size_t min_align, align;
 
+       lockdep_assert_held(&pci_res_setup_lock);
+
        if (!b_res)
                return;
 
@@ -1286,6 +1308,8 @@ static void pbus_size_mem(struct pci_bus *bus, struct resource *b_res,
        resource_size_t children_add_size = 0;
        resource_size_t add_align = 0;
 
+       lockdep_assert_held(&pci_res_setup_lock);
+
        if (!b_res)
                return;
 
@@ -1457,6 +1481,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);
        guard(rwsem_read)(&pci_bus_sem);
        __pci_bus_size_bridges(bus, NULL);
 }
@@ -1486,6 +1511,8 @@ static void pdev_assign_fixed_resources(struct pci_dev *dev)
 {
        struct resource *r;
 
+       lockdep_assert_held(&pci_res_setup_lock);
+
        pci_dev_for_each_resource(dev, r) {
                struct pci_bus *b;
 
@@ -1550,6 +1577,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);
        guard(rwsem_read)(&pci_bus_sem);
        __pci_bus_assign_resources(bus, NULL, NULL);
 }
@@ -1663,6 +1691,8 @@ static void pci_bridge_release_resources(struct pci_bus *bus,
        struct pci_dev *dev = bus->self;
        int idx, ret;
 
+       lockdep_assert_held(&pci_res_setup_lock);
+
        if (!resource_assigned(b_win))
                return;
 
@@ -1860,6 +1890,8 @@ static void adjust_bridge_window(struct pci_dev *bridge, struct resource *res,
        resource_size_t add_size, size = resource_size(res);
        struct pci_dev_resource *dev_res;
 
+       lockdep_assert_held(&pci_res_setup_lock);
+
        if (resource_assigned(res))
                return;
 
@@ -2192,6 +2224,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);
        guard(rwsem_read)(&pci_bus_sem);
 
        /* Don't realloc if asked to do so */
@@ -2270,6 +2303,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);
        guard(rwsem_read)(&pci_bus_sem);
 
        while (1) {
@@ -2328,6 +2362,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);
+
        while (!pci_is_root_bus(bus)) {
                bridge = bus->self;
                res = pbus_select_window(bus, res);
@@ -2398,6 +2434,8 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
        unsigned int i;
        int old, ret;
 
+       guard(mutex)(&pci_res_setup_lock);
+
        b_win = pbus_select_window(bus, res);
        if (!b_win)
                return -EINVAL;
@@ -2495,6 +2533,7 @@ void pci_assign_unassigned_bus_resources(struct pci_bus *bus)
        /* List of resources that want additional resources */
        LIST_HEAD(add_list);
 
+       guard(mutex)(&pci_res_setup_lock);
        guard(rwsem_read)(&pci_bus_sem);
        for_each_pci_bridge(dev, bus)
                if (pci_has_subordinate(dev))
@@ -2505,6 +2544,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);
        guard(rwsem_read)(&pci_bus_sem);
        __pci_bus_size_bridges(bus, NULL);
        __pci_bus_assign_resources(bus, NULL, NULL);
drivers/pci/setup-cardbus.c +3 -1
index dcb8e2b3f73b..6a473a1694b9 100644
@@ -6,6 +6,7 @@
 #include <linux/bitfield.h>
 #include <linux/errno.h>
 #include <linux/ioport.h>
+#include <linux/lockdep.h>
 #include <linux/pci.h>
 #include <linux/sizes.h>
 #include <linux/sprintf.h>
@@ -39,6 +40,8 @@ int pci_bus_size_cardbus_bridge(struct pci_bus *bus,
        resource_size_t b_res_3_size = pci_cardbus_mem_size * 2;
        u16 ctrl;
 
+       lockdep_assert_held(&pci_res_setup_lock);
+
        b_res = &bridge->resource[PCI_CB_BRIDGE_IO_0_WINDOW];
        if (resource_assigned(b_res))
                goto handle_b_res_1;
--

2.47.3

author maintainer
#16 lore raw

PATCH 6/13 i82875p_edac: Convert to pci_assign_unassigned_bus_resources()

In reply to Ilpo Järvinen

The i82875p_edac driver scans single device and then attempts to assign its resource using pci_bus_assign_resources(). That function makes bridge window sizing disjoint from the assignment phase and is going to be removed.

Convert pci_bus_assign_resources() to pci_assign_unassigned_bus_resources() which is likely no-op in this case as it looks like bridges are not involved here.

---
 drivers/edac/i82875p_edac.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/edac/i82875p_edac.c +1 -1
index 869de8e372b3..59a92a209dea 100644
@@ -290,7 +290,7 @@ static int i82875p_setup_overfl_dev(struct pci_dev *pdev,
 		if (dev == NULL)
 			return 1;
 
-		pci_bus_assign_resources(dev->bus);
+		pci_assign_unassigned_bus_resources(dev->bus);
 		pci_bus_add_device(dev);
 	}
 
Signature · 1 line
2.47.3
#17 lore raw

Re: [PATCH 06/13] i82875p_edac: Convert to pci_assign_unassigned_bus_resources()

Sashiko has reviewed this patch and found no issues.
Quoted 10 lines

The i82875p_edac driver scans single device and then attempts to assign its resource using pci_bus_assign_resources(). That function makes bridge window sizing disjoint from the assignment phase and is going to be removed.

Convert pci_bus_assign_resources() to pci_assign_unassigned_bus_resources() which is likely no-op in this case as it looks like bridges are not involved here.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
maintainer
#18 lore raw

Re: [PATCH 06/13] i82875p_edac: Convert to pci_assign_unassigned_bus_resources()

In reply to Ilpo Järvinen

In addition to this, Tony, should we orphan this one too?

It is a P4-something-chipset...

Quoted 30 lines · Ilpo Järvinen

The i82875p_edac driver scans single device and then attempts to assign its resource using pci_bus_assign_resources(). That function makes bridge window sizing disjoint from the assignment phase and is going to be removed.

Convert pci_bus_assign_resources() to pci_assign_unassigned_bus_resources() which is likely no-op in this case as it looks like bridges are not involved here.

---
 drivers/edac/i82875p_edac.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/edac/i82875p_edac.c +1 -1
index 869de8e372b3..59a92a209dea 100644
@@ -290,7 +290,7 @@ static int i82875p_setup_overfl_dev(struct pci_dev *pdev,
 		if (dev == NULL)
 			return 1;
 
-		pci_bus_assign_resources(dev->bus);
+		pci_assign_unassigned_bus_resources(dev->bus);
 		pci_bus_add_device(dev);
 	}
 
Signature · 1 line
2.47.3
Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
maintainer
#19 lore raw

RE: [PATCH 06/13] i82875p_edac: Convert to pci_assign_unassigned_bus_resources()

Quoted 3 lines

In addition to this, Tony, should we orphan this one too?

It is a P4-something-chipset...

Looks like it was release in 2003 or so. Only supports 4GB of memory. Even if someone still has one powered up, unlikely that they would run an upstream kernel on it.

I'm OK with marking it as orphaned.

-Tony

maintainer
#20 lore raw

Re: [PATCH 06/13] i82875p_edac: Convert to pci_assign_unassigned_bus_resources()

Quoted 9 lines · Tony
Quoted 3 lines

In addition to this, Tony, should we orphan this one too?

It is a P4-something-chipset...

Looks like it was release in 2003 or so. Only supports 4GB of memory. Even if someone still has one powered up, unlikely that they would run an upstream kernel on it.

I'm OK with marking it as orphaned.

Ok, here's what I have in mind:

From: "Borislav Petkov (AMD)" <[email protected]>
Date: Tue, 17 Nov 2026 11:49:09 -0800
Subject: [PATCH] MAINTAINERS: Add a common entry for orphaned EDAC drivers

This removes a bunch of lines in the MAINTAINERS file while having the same result: unify orphaned EDAC drivers which are not going to receive any more fixes due to lack of hardware, age of the hardware supported and general lack of use.

This is also adding drivers/edac/i82875p_edac.c to the lineup which is a 32-bit only chipset for Pentium 4 from 2003 or so.

The code is not going away - if there are users who are willing to test, we can easily resurrect them.

---
 MAINTAINERS | 29 +++++++++--------------------
 1 file changed, 9 insertions(+), 20 deletions(-)
MAINTAINERS +9 -20
index 31c06c57de93..f1a83cea9798 100644
@@ -9386,11 +9386,6 @@ L:	[email protected]
 S:	Maintained
 F:	drivers/edac/octeon_edac*
 
-EDAC-CAVIUM THUNDERX
-L:	[email protected]
-S:	Orphan
-F:	drivers/edac/thunderx_edac*
-
 EDAC-CORE
 M:	Borislav Petkov <[email protected]>
 M:	Tony Luck <[email protected]>
@@ -9414,11 +9409,6 @@ L:	[email protected]
 S:	Supported
 F:	drivers/edac/dmc520_edac.c
 
-EDAC-E752X
-L:	[email protected]
-S:	Orphan
-F:	drivers/edac/e752x_edac.c
-
 EDAC-E7XXX
 L:	[email protected]
 S:	Maintained
@@ -9443,11 +9433,6 @@ L:	[email protected]
 S:	Maintained
 F:	drivers/edac/i10nm_base.c
 
-EDAC-I3000
-L:	[email protected]
-S:	Orphan
-F:	drivers/edac/i3000_edac.c
-
 EDAC-I5000
 R:	Andrew Luebke <[email protected]>
 L:	[email protected]
@@ -9491,11 +9476,6 @@ L:	[email protected]
 S:	Maintained
 F:	drivers/edac/igen6_edac.c
 
-EDAC-MPC85XX
-L:	[email protected]
-S:	Orphan
-F:	drivers/edac/mpc85xx_edac.[ch]
-
 EDAC-NPCM
 M:	Marvin Lin <[email protected]>
 M:	Stanley Chu <[email protected]>
@@ -9542,6 +9522,15 @@ L:	[email protected]
 S:	Odd Fixes
 F:	drivers/edac/ti_edac.c
 
+EDAC ORPHANED DRIVERS
+L:	[email protected]
+S:	Orphan
+F:	drivers/edac/e752x_edac.c
+F:	drivers/edac/i3000_edac.c
+F:	drivers/edac/i82875p_edac.c
+F:	drivers/edac/mpc85xx_edac.[ch]
+F:	drivers/edac/thunderx_edac*
+
 EDIROL UA-101/UA-1000 DRIVER
 M:	Clemens Ladisch <[email protected]>
 L:	[email protected]
Signature · 7 lines
2.53.0

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
maintainer Acked-by
#21 lore raw

RE: [PATCH 06/13] i82875p_edac: Convert to pci_assign_unassigned_bus_resources()

Quoted 10 lines

This removes a bunch of lines in the MAINTAINERS file while having the same result: unify orphaned EDAC drivers which are not going to receive any more fixes due to lack of hardware, age of the hardware supported and general lack of use.

This is also adding drivers/edac/i82875p_edac.c to the lineup which is a 32-bit only chipset for Pentium 4 from 2003 or so.

The code is not going away - if there are users who are willing to test, we can easily resurrect them.

I like the bundling of all the orphans together.

-Tony

maintainer
#22 lore raw

Re: [PATCH 06/13] i82875p_edac: Convert to pci_assign_unassigned_bus_resources()

Quoted 8 lines · Tony
Quoted 8 lines

This removes a bunch of lines in the MAINTAINERS file while having the same result: unify orphaned EDAC drivers which are not going to receive any more fixes due to lack of hardware, age of the hardware supported and general lack of use.

This is also adding drivers/edac/i82875p_edac.c to the lineup which is a 32-bit only chipset for Pentium 4 from 2003 or so.

Quoted 6 lines
Quoted 2 lines

The code is not going away - if there are users who are willing to test, we can easily resurrect them.

I like the bundling of all the orphans together.

Thanks, lemme push it out.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
maintainer Acked-by Tony Luck
#23 lore raw

Re: [PATCH 06/13] i82875p_edac: Convert to pci_assign_unassigned_bus_resources()

Quoted 1 line · Borislav Petkov

Thanks, lemme push it out.

Completely fat-fingered it on the wrong tree and missed a couple of more orphaned ones. Below is the updated version. I've kept your Acked-by because the patch idea is basically unchanged.

Thx.

From: "Borislav Petkov (AMD)" <[email protected]>
Date: Tue, 17 Nov 2026 11:49:09 -0800
Subject: [PATCH] MAINTAINERS: Add a common entry for orphaned EDAC drivers

This removes a bunch of lines in the MAINTAINERS file while having the same result: unify orphaned EDAC drivers which are not going to receive any more fixes due to lack of hardware, age of the hardware supported and general lack of use.

This is also adding drivers/edac/i82875p_edac.c to the lineup which is a 32-bit only chipset for Pentium 4 from 2003 or so.

The code is not going away - if there are users who are willing to test, we can easily resurrect them.

---
 MAINTAINERS | 46 ++++++++++------------------------------------
 1 file changed, 10 insertions(+), 36 deletions(-)
MAINTAINERS +10 -36
index 12406a2f4d57..4b930d531214 100644
@@ -2152,11 +2152,6 @@ M:	Khuong Dinh <[email protected]>
 S:	Supported
 F:	arch/arm64/boot/dts/apm/
 
-APPLIED MICRO (APM) X-GENE SOC EDAC
-S:	Orphan
-F:	Documentation/devicetree/bindings/edac/apm,xgene-edac.yaml
-F:	drivers/edac/xgene_edac.c
-
 APPLIED MICRO (APM) X-GENE SOC ETHERNET (V2) DRIVER
 M:	Iyappan Subramanian <[email protected]>
 M:	Keyur Chudgar <[email protected]>
@@ -9631,11 +9626,6 @@ L:	[email protected]
 S:	Maintained
 F:	drivers/edac/octeon_edac*
 
-EDAC-CAVIUM THUNDERX
-L:	[email protected]
-S:	Orphan
-F:	drivers/edac/thunderx_edac*
-
 EDAC-CORE
 M:	Borislav Petkov <[email protected]>
 M:	Tony Luck <[email protected]>
@@ -9659,11 +9649,6 @@ L:	[email protected]
 S:	Supported
 F:	drivers/edac/dmc520_edac.c
 
-EDAC-E752X
-L:	[email protected]
-S:	Orphan
-F:	drivers/edac/e752x_edac.c
-
 EDAC-E7XXX
 L:	[email protected]
 S:	Maintained
@@ -9688,16 +9673,6 @@ L:	[email protected]
 S:	Maintained
 F:	drivers/edac/i10nm_base.c
 
-EDAC-I3000
-L:	[email protected]
-S:	Orphan
-F:	drivers/edac/i3000_edac.c
-
-EDAC-I3200
-L:	[email protected]
-S:	Orphan
-F:	drivers/edac/i3200_edac.c
-
 EDAC-I5000
 R:	Andrew Luebke <[email protected]>
 L:	[email protected]
@@ -9722,11 +9697,6 @@ L:	[email protected]
 S:	Maintained
 F:	drivers/edac/i7core_edac.c
 
-EDAC-I82975X
-L:	[email protected]
-S:	Orphan
-F:	drivers/edac/i82975x_edac.c
-
 EDAC-IE31200
 M:	Jason Baron <[email protected]>
 L:	[email protected]
@@ -9746,11 +9716,6 @@ L:	[email protected]
 S:	Maintained
 F:	drivers/edac/intel-bff.c
 
-EDAC-MPC85XX
-L:	[email protected]
-S:	Orphan
-F:	drivers/edac/mpc85xx_edac.[ch]
-
 EDAC-NPCM
 M:	Marvin Lin <[email protected]>
 M:	Stanley Chu <[email protected]>
@@ -9797,10 +9762,19 @@ L:	[email protected]
 S:	Odd Fixes
 F:	drivers/edac/ti_edac.c
 
-EDAC-X38
+EDAC ORPHANED DRIVERS
 L:	[email protected]
 S:	Orphan
+F:	Documentation/devicetree/bindings/edac/apm,xgene-edac.yaml
+F:	drivers/edac/e752x_edac.c
+F:	drivers/edac/i3000_edac.c
+F:	drivers/edac/i3200_edac.c
+F:	drivers/edac/i82875p_edac.c
+F:	drivers/edac/i82975x_edac.c
+F:	drivers/edac/mpc85xx_edac.[ch]
+F:	drivers/edac/thunderx_edac*
 F:	drivers/edac/x38_edac.c
+F:	drivers/edac/xgene_edac.c
 
 EDIROL UA-101/UA-1000 DRIVER
 M:	Clemens Ladisch <[email protected]>
Signature · 1 line
2.53.0
Quoted 6 lines
Signature · 5 lines
-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
author maintainer
#24 lore raw

PATCH 7/13 platform/x86: asus-wmi: Convert to pci_assign_unassigned_bus_resources()

In reply to Ilpo Järvinen

The asus-wmi driver scans single device and then attempts to assign its resource using pci_bus_assign_resources(). That function makes bridge window sizing disjoint from the assignment phase and is going to be removed.

Convert pci_bus_assign_resources() to pci_assign_unassigned_bus_resources() which is likely no-op in this case as it looks like bridges are not involved here.

---
 drivers/platform/x86/asus-wmi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/platform/x86/asus-wmi.c +1 -1
index b33ebdb21aa2..9eb783eb53c6 100644
@@ -2325,7 +2325,7 @@ static void asus_rfkill_hotplug(struct asus_wmi *asus)
 			}
 			dev = pci_scan_single_device(bus, 0);
 			if (dev) {
-				pci_bus_assign_resources(bus);
+				pci_assign_unassigned_bus_resources(bus);
 				pci_bus_add_device(dev);
 			}
 		} else {
Signature · 1 line
2.47.3
#25 lore raw

Re: [PATCH 07/13] platform/x86: asus-wmi: Convert to pci_assign_unassigned_bus_resources()

Sashiko has reviewed this patch and found no issues.
Quoted 10 lines

The asus-wmi driver scans single device and then attempts to assign its resource using pci_bus_assign_resources(). That function makes bridge window sizing disjoint from the assignment phase and is going to be removed.

Convert pci_bus_assign_resources() to pci_assign_unassigned_bus_resources() which is likely no-op in this case as it looks like bridges are not involved here.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
maintainer Reviewed-by on v1 7/13
#26 lore raw

Re: [PATCH 07/13] platform/x86: asus-wmi: Convert to pci_assign_unassigned_bus_resources()

In reply to Ilpo Järvinen
Quoted 8 lines · Ilpo Järvinen

The asus-wmi driver scans single device and then attempts to assign its resource using pci_bus_assign_resources(). That function makes bridge window sizing disjoint from the assignment phase and is going to be removed.

Convert pci_bus_assign_resources() to pci_assign_unassigned_bus_resources() which is likely no-op in this case as it looks like bridges are not involved here.

Quoted 18 lines
---
 drivers/platform/x86/asus-wmi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/platform/x86/asus-wmi.c +1 -1
index b33ebdb21aa2..9eb783eb53c6 100644
@@ -2325,7 +2325,7 @@ static void asus_rfkill_hotplug(struct asus_wmi *asus)
 			}
 			dev = pci_scan_single_device(bus, 0);
 			if (dev) {
-				pci_bus_assign_resources(bus);
+				pci_assign_unassigned_bus_resources(bus);
 				pci_bus_add_device(dev);
 			}
 		} else {
author maintainer
#27 lore raw

PATCH 8/13 platform/x86: eeepc-laptop: Convert to pci_assign_unassigned_bus_resources()

In reply to Ilpo Järvinen

The eeepc-laptop driver scans single device and then attempts to assign its resource using pci_bus_assign_resources(). That function makes bridge window sizing disjoint from the assignment phase and is going to be removed.

Convert pci_bus_assign_resources() to pci_assign_unassigned_bus_resources() which is likely no-op in this case as it looks like bridges are not involved here.

---
 drivers/platform/x86/eeepc-laptop.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/platform/x86/eeepc-laptop.c +1 -1
index 8a640c25830a..7394221cbad1 100644
@@ -609,7 +609,7 @@ static void eeepc_rfkill_hotplug(struct eeepc_laptop *eeepc, acpi_handle handle)
 		}
 		dev = pci_scan_single_device(bus, 0);
 		if (dev) {
-			pci_bus_assign_resources(bus);
+			pci_assign_unassigned_bus_resources(bus);
 			pci_bus_add_device(dev);
 		}
 	} else {
Signature · 1 line
2.47.3
#28 lore raw

Re: [PATCH 08/13] platform/x86: eeepc-laptop: Convert to pci_assign_unassigned_bus_resources()

Sashiko has reviewed this patch and found no issues.
Quoted 10 lines

The eeepc-laptop driver scans single device and then attempts to assign its resource using pci_bus_assign_resources(). That function makes bridge window sizing disjoint from the assignment phase and is going to be removed.

Convert pci_bus_assign_resources() to pci_assign_unassigned_bus_resources() which is likely no-op in this case as it looks like bridges are not involved here.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
maintainer Reviewed-by on v1 8/13
#29 lore raw

Re: [PATCH 08/13] platform/x86: eeepc-laptop: Convert to pci_assign_unassigned_bus_resources()

In reply to Ilpo Järvinen
Quoted 8 lines · Ilpo Järvinen

The eeepc-laptop driver scans single device and then attempts to assign its resource using pci_bus_assign_resources(). That function makes bridge window sizing disjoint from the assignment phase and is going to be removed.

Convert pci_bus_assign_resources() to pci_assign_unassigned_bus_resources() which is likely no-op in this case as it looks like bridges are not involved here.

Hi Ilpo,

I don't know exactly why, but this smells like what I had to do in
asus-armoury: scan the pci bus for new devices.

It has been on my TODO list for a while (and I think it will need to stay there some more) to try to understand why these are even needed.

Dissecting the ROG ally xg mobile ACPI I found the signal that is supposed to tell the kernel about the hotplug, but it's either not reaching the correct part of the system or the triggered rescan is not on the whole pci root port and the linux kernel is searching for changes where those changes aren't possible.

I have some more notes:

In https://uefi.org/specs/ACPI/6.4_A/05_ACPI_Software_Programming_Model.html#device-object-notifications
I quote: "If the device is a bridge, OSPM may re-enumerate the bridge and the child bus"... The kernel has handling for this already with ACPI_NOTIFY_DEVICE_CHECK but it looks like what needs to happen isn't happening (probably is re-enumerating only the child).

I will need to debug this eventually, unless someone more skilled then me in ACPI and pci takes a look at that and solve the issue properly without triggering a rescan, since the ACPI notification is surely there (at least for ally), not this exact case.

Best regards,
Denis

Quoted 18 lines
---
 drivers/platform/x86/eeepc-laptop.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/platform/x86/eeepc-laptop.c +1 -1
index 8a640c25830a..7394221cbad1 100644
@@ -609,7 +609,7 @@ static void eeepc_rfkill_hotplug(struct eeepc_laptop *eeepc, acpi_handle handle)
 		}
 		dev = pci_scan_single_device(bus, 0);
 		if (dev) {
-			pci_bus_assign_resources(bus);
+			pci_assign_unassigned_bus_resources(bus);
 			pci_bus_add_device(dev);
 		}
 	} else {
author maintainer
#30 lore raw

PATCH 9/13 parisc/PCI: Convert to pci_assign_unassigned_bus_resources()

In reply to Ilpo Järvinen

The Dino PCI bus driver performs assignment of resource using pci_bus_assign_resources(). That function makes bridge window sizing disjoint from the assignment phase and is going to be removed.

Convert pci_bus_assign_resources() to pci_assign_unassigned_bus_resources(). If no other bridges are involved, the extra sizing should be no-op.

---
 drivers/parisc/dino.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/parisc/dino.c +1 -1
index 286f4f75dba7..da75ba15faf4 100644
@@ -1050,7 +1050,7 @@ static int __init dino_probe(struct parisc_device *dev)
 	 * if it isn't, this global bus number count will fail
 	 */
 	dino_current_bus = max + 1;
-	pci_bus_assign_resources(bus);
+	pci_assign_unassigned_bus_resources(bus);
 	pci_bus_add_devices(bus);
 	return 0;
 }
Signature · 1 line
2.47.3
#31 lore raw

Re: [PATCH 09/13] parisc/PCI: Convert to pci_assign_unassigned_bus_resources()

Sashiko has reviewed this patch and found no issues.
Quoted 9 lines

The Dino PCI bus driver performs assignment of resource using pci_bus_assign_resources(). That function makes bridge window sizing disjoint from the assignment phase and is going to be removed.

Convert pci_bus_assign_resources() to pci_assign_unassigned_bus_resources(). If no other bridges are involved, the extra sizing should be no-op.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
author maintainer
#32 lore raw

PATCH 10/13 PCI: hotplug: octep: Convert to pci_assign_unassigned_bus_resources()

In reply to Ilpo Järvinen

The Octeon hotplug driver scans single device and then attempts to assign its resource using pci_bus_assign_resources(). That function makes bridge window sizing disjoint from the assignment phase and is going to be removed.

Convert pci_bus_assign_resources() to pci_assign_unassigned_bus_resources(). If no other bridges are involved, the extra sizing should be no-op.

---
 drivers/pci/hotplug/octep_hp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/hotplug/octep_hp.c +1 -1
index a0a7f9ccb8fa..e793fc3012fd 100644
@@ -79,7 +79,7 @@ static void octep_hp_enable_pdev(struct octep_hp_controller *hp_ctrl,
 	/* Scan the device and add it to the bus */
 	hp_slot->hp_pdev = pci_scan_single_device(hp_ctrl->pdev->bus,
 						  hp_slot->hp_devfn);
-	pci_bus_assign_resources(hp_ctrl->pdev->bus);
+	pci_assign_unassigned_bus_resources(hp_ctrl->pdev->bus);
 	pci_bus_add_device(hp_slot->hp_pdev);
 
 	dev_dbg(&hp_slot->hp_pdev->dev, "Enabled slot %s\n",
Signature · 1 line
2.47.3
#33 lore raw

Re: [PATCH 10/13] PCI: hotplug: octep: Convert to pci_assign_unassigned_bus_resources()

Sashiko has reviewed this patch and found no issues.
Quoted 10 lines

The Octeon hotplug driver scans single device and then attempts to assign its resource using pci_bus_assign_resources(). That function makes bridge window sizing disjoint from the assignment phase and is going to be removed.

Convert pci_bus_assign_resources() to pci_assign_unassigned_bus_resources(). If no other bridges are involved, the extra sizing should be no-op.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
author maintainer
#34 lore raw

PATCH 11/13 PCI: ftpci100: Convert to pci_assign_unassigned_bus_resources()

In reply to Ilpo Järvinen

The ftpci100 driver attempts to assign bus resource using pci_bus_assign_resources(). That function makes bridge window sizing disjoint from the assignment phase and is going to be removed.

Convert pci_bus_assign_resources() to pci_assign_unassigned_bus_resources(). If no other bridges are involved, the extra sizing should be no-op.

---
 drivers/pci/controller/pci-ftpci100.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/controller/pci-ftpci100.c +1 -1
index 28e43831c0f1..96b3deb70f57 100644
@@ -509,7 +509,7 @@ static int faraday_pci_probe(struct platform_device *pdev)
 	p->bus->max_bus_speed = max_bus_speed;
 	p->bus->cur_bus_speed = cur_bus_speed;
 
-	pci_bus_assign_resources(p->bus);
+	pci_assign_unassigned_bus_resources(p->bus);
 	pci_bus_add_devices(p->bus);
 
 	return 0;
Signature · 1 line
2.47.3
#35 lore raw

Re: [PATCH 11/13] PCI: ftpci100: Convert to pci_assign_unassigned_bus_resources()

Sashiko has reviewed this patch and found no issues.
Quoted 9 lines

The ftpci100 driver attempts to assign bus resource using pci_bus_assign_resources(). That function makes bridge window sizing disjoint from the assignment phase and is going to be removed.

Convert pci_bus_assign_resources() to pci_assign_unassigned_bus_resources(). If no other bridges are involved, the extra sizing should be no-op.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
author maintainer
#36 lore raw

PATCH 12/13 PCI: hv: Convert to pci_assign_unassigned_bus_resources()

In reply to Ilpo Järvinen

The pci-hyperv driver scans the bus and assigns resources using pci_bus_assign_resources(). That function makes bridge window sizing disjoint from the assignment phase and is going to be removed.

Convert pci_bus_assign_resources() to pci_assign_unassigned_bus_resources(). If no other bridges are involved, the extra sizing should be no-op.

---
 drivers/pci/controller/pci-hyperv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/controller/pci-hyperv.c +1 -1
index 824cf27e85e6..8eff3e9b325f 100644
@@ -2549,7 +2549,7 @@ static int create_root_hv_pci_bus(struct hv_pcibus_device *hbus)
 
 	pci_lock_rescan_remove();
 	hv_pci_assign_numa_node(hbus);
-	pci_bus_assign_resources(bridge->bus);
+	pci_assign_unassigned_bus_resources(bridge->bus);
 	hv_pci_assign_slots(hbus);
 	pci_bus_add_devices(bridge->bus);
 	pci_unlock_rescan_remove();
Signature · 1 line
2.47.3
#37 lore raw

Re: [PATCH 12/13] PCI: hv: Convert to pci_assign_unassigned_bus_resources()

Sashiko has reviewed this patch and found no issues.
Quoted 9 lines

The pci-hyperv driver scans the bus and assigns resources using pci_bus_assign_resources(). That function makes bridge window sizing disjoint from the assignment phase and is going to be removed.

Convert pci_bus_assign_resources() to pci_assign_unassigned_bus_resources(). If no other bridges are involved, the extra sizing should be no-op.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
Tested-by Reviewed-by on v1 12/13
#38 lore raw

RE: [PATCH 12/13] PCI: hv: Convert to pci_assign_unassigned_bus_resources()

In reply to Ilpo Järvinen

From: Ilpo Järvinen <[email protected]> Sent: Monday, November 16, 2026 7:24 AM

Quoted 8 lines

The pci-hyperv driver scans the bus and assigns resources using pci_bus_assign_resources(). That function makes bridge window sizing disjoint from the assignment phase and is going to be removed.

Convert pci_bus_assign_resources() to pci_assign_unassigned_bus_resources(). If no other bridges are involved, the extra sizing should be no-op.

I tested this series on an x86 VM in the Azure public cloud that has 5 NVMe devices and a Mellanox VF NIC. All 6 devices are PCI pass-thru devices in the VM that are initially set up using the pci-hyperv.c driver, so each device has its own root bus and bridge. No issues were seen in a basic smoke test.

I added some debug code to pci_assign_unassigned_bus_resources(), and indeed the "for_each_pci_bridge()" loop found nothing as each device was setup. So __pci_bus_assign_resources() was called with an empty add_list, and as expected, the new execution path is equivalent to the old pci_bus_assign_resources() call.

Question: Your cover letter says drivers that call pci_assign_resources() directly are not covered by this series. pci-hyperv.c *does* call pci_assign_resources() directly in hv_pci_allocate_bridge_windows(). Is that a problem? I don't understand all the details of the PCI resource assignment well enough to judge for myself. :-(

In any case,

And at least to some degree,

Quoted 22 lines
---
 drivers/pci/controller/pci-hyperv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/controller/pci-hyperv.c +1 -2
index 824cf27e85e6..8eff3e9b325f 100644
@@ -2549,7 +2549,7 @@ static int create_root_hv_pci_bus(struct hv_pcibus_device *hbus)
 
 	pci_lock_rescan_remove();
 	hv_pci_assign_numa_node(hbus);
-	pci_bus_assign_resources(bridge->bus);
+	pci_assign_unassigned_bus_resources(bridge->bus);
 	hv_pci_assign_slots(hbus);
 	pci_bus_add_devices(bridge->bus);
 	pci_unlock_rescan_remove();
--

2.47.3

author maintainer
#39 lore raw

RE: [PATCH 12/13] PCI: hv: Convert to pci_assign_unassigned_bus_resources()

Quoted 22 lines · Michael Kelley

From: Ilpo Järvinen <[email protected]> Sent: Monday, November 16, 2026 7:24 AM

Quoted 8 lines

The pci-hyperv driver scans the bus and assigns resources using pci_bus_assign_resources(). That function makes bridge window sizing disjoint from the assignment phase and is going to be removed.

Convert pci_bus_assign_resources() to pci_assign_unassigned_bus_resources(). If no other bridges are involved, the extra sizing should be no-op.

I tested this series on an x86 VM in the Azure public cloud that has 5 NVMe devices and a Mellanox VF NIC. All 6 devices are PCI pass-thru devices in the VM that are initially set up using the pci-hyperv.c driver, so each device has its own root bus and bridge. No issues were seen in a basic smoke test.

I added some debug code to pci_assign_unassigned_bus_resources(), and indeed the "for_each_pci_bridge()" loop found nothing as each device was setup. So __pci_bus_assign_resources() was called with an empty add_list, and as expected, the new execution path is equivalent to the old pci_bus_assign_resources() call.

Quoted 5 lines

Question: Your cover letter says drivers that call pci_assign_resources() directly are not covered by this series. pci-hyperv.c *does* call pci_assign_resources() directly in hv_pci_allocate_bridge_windows(). Is that a problem? I don't understand all the details of the PCI resource assignment well enough to judge for myself. :-(

There's a small name mixup here.

pci_assign_resources() is to graft a device's resource (or a window) into resource tree, whereas that calls pci_add_resource() adding windows to the root bus and that seems to occur before there is anything underneath. At this point, there's nothing to do for the resource fitting and assignment algorithm (it iterates the devices underneath).

...So I don't see a race here related to pci_assign_resources().

Most entrypoints of the fitting and assignment algorith input bus, and I don't think anything in kernel would as to assign resources underneath that root bus. Only pci_assign_unassigned_resources() looks for all root buses, and that is only called from arch init code so I don't think vfio setting up it host bridges matters with that call.

There seems to be, in general (not just hyperv), some races that are unrelated to resource setup in host bridge setup and scanning. I'll see what I can do but it'll be in a separate series and those seems to fall under can-shoot-oneself's-own-foot category.

Thanks for taking a look.

Signature · 1 line
 i.
Quoted 32 lines

In any case,

And at least to some degree,

Quoted 22 lines
---
 drivers/pci/controller/pci-hyperv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/controller/pci-hyperv.c +1 -2
index 824cf27e85e6..8eff3e9b325f 100644
@@ -2549,7 +2549,7 @@ static int create_root_hv_pci_bus(struct hv_pcibus_device *hbus)
 
 	pci_lock_rescan_remove();
 	hv_pci_assign_numa_node(hbus);
-	pci_bus_assign_resources(bridge->bus);
+	pci_assign_unassigned_bus_resources(bridge->bus);
 	hv_pci_assign_slots(hbus);
 	pci_bus_add_devices(bridge->bus);
 	pci_unlock_rescan_remove();
--

2.47.3

Signature · 1 line
 i.
#40 lore raw

RE: [PATCH 12/13] PCI: hv: Convert to pci_assign_unassigned_bus_resources()

From: Ilpo Järvinen <[email protected]> Sent: Friday, November 20, 2026 4:55 AM

Quoted 25 lines · Michael Kelley
Quoted 23 lines

From: Ilpo Järvinen <[email protected]> Sent: Monday, November 16, 2026 7:24 AM

Quoted 8 lines

The pci-hyperv driver scans the bus and assigns resources using pci_bus_assign_resources(). That function makes bridge window sizing disjoint from the assignment phase and is going to be removed.

Convert pci_bus_assign_resources() to pci_assign_unassigned_bus_resources(). If no other bridges are involved, the extra sizing should be no-op.

I tested this series on an x86 VM in the Azure public cloud that has 5 NVMe devices and a Mellanox VF NIC. All 6 devices are PCI pass-thru devices in the VM that are initially set up using the pci-hyperv.c driver, so each device has its own root bus and bridge. No issues were seen in a basic smoke test.

I added some debug code to pci_assign_unassigned_bus_resources(), and indeed the "for_each_pci_bridge()" loop found nothing as each device was setup. So __pci_bus_assign_resources() was called with an empty add_list, and as expected, the new execution path is equivalent to the old pci_bus_assign_resources() call.

Quoted 7 lines
Quoted 5 lines

Question: Your cover letter says drivers that call pci_assign_resources() directly are not covered by this series. pci-hyperv.c *does* call pci_assign_resources() directly in hv_pci_allocate_bridge_windows(). Is that a problem? I don't understand all the details of the PCI resource assignment well enough to judge for myself. :-(

There's a small name mixup here.

Indeed! My bad ....

Quoted 15 lines

pci_assign_resources() is to graft a device's resource (or a window) into resource tree, whereas that calls pci_add_resource() adding windows to the root bus and that seems to occur before there is anything underneath. At this point, there's nothing to do for the resource fitting and assignment algorithm (it iterates the devices underneath).

...So I don't see a race here related to pci_assign_resources().

Most entrypoints of the fitting and assignment algorith input bus, and I don't think anything in kernel would as to assign resources underneath that root bus. Only pci_assign_unassigned_resources() looks for all root buses, and that is only called from arch init code so I don't think vfio setting up it host bridges matters with that call.

Quoted 6 lines

There seems to be, in general (not just hyperv), some races that are unrelated to resource setup in host bridge setup and scanning. I'll see what I can do but it'll be in a separate series and those seems to fall under can-shoot-oneself's-own-foot category.

Thanks for taking a look.

Thank you!

Michael

author maintainer
#41 lore raw

PATCH 13/13 PCI: Remove pci_bus_assign_resources()

In reply to Ilpo Järvinen

The pci_bus_assign_resources() interface is problematic because it makes bridge window sizing disjoint from assignment phase.

pci_bus_assign_resources() is now unused so remove it.

---
 drivers/pci/setup-bus.c | 8 --------
 include/linux/pci.h     | 1 -
 2 files changed, 9 deletions(-)
drivers/pci/setup-bus.c +0 -8
index 314c649c648f..547d0f920360 100644
@@ -1575,14 +1575,6 @@ void __pci_bus_assign_resources(const struct pci_bus *bus,
 		pci_dev_res_free_list(add_list);
 }
 
-void pci_bus_assign_resources(const struct pci_bus *bus)
-{
-	guard(mutex)(&pci_res_setup_lock);
-	guard(rwsem_read)(&pci_bus_sem);
-	__pci_bus_assign_resources(bus, NULL, NULL);
-}
-EXPORT_SYMBOL(pci_bus_assign_resources);
-
 static void pci_claim_device_resources(struct pci_dev *dev)
 {
 	int i;
include/linux/pci.h +0 -1
index d89aeb7c9ff7..7d71ad6d89af 100644
@@ -1566,7 +1566,6 @@ ssize_t pci_write_vpd_any(struct pci_dev *dev, loff_t pos, size_t count, const v
 
 /* Helper functions for low-level code (drivers/pci/setup-[bus,res].c) */
 resource_size_t pcibios_retrieve_fw_addr(struct pci_dev *dev, int idx);
-void pci_bus_assign_resources(const struct pci_bus *bus);
 void pci_bus_claim_resources(struct pci_bus *bus);
 void pci_bus_size_bridges(struct pci_bus *bus);
 void pci_bus_size_and_assign_resources(struct pci_bus *bus);
Signature · 1 line
2.47.3
#42 lore raw

Re: [PATCH 13/13] PCI: Remove pci_bus_assign_resources()

Sashiko has reviewed this patch and found no issues.
Quoted 6 lines

The pci_bus_assign_resources() interface is problematic because it makes bridge window sizing disjoint from assignment phase.

pci_bus_assign_resources() is now unused so remove it.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13