Linux PCI subsystem development
 help / color / mirror / Atom feed
* [PATCH v2] pci: Prefer MMCONFIG for all config space accesses if available
@ 2026-12-01  5:35 Josh Triplett
  2026-12-01  5:49 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Josh Triplett @ 2026-12-01  5:35 UTC (permalink / raw)
  To: Borislav Petkov, Bjorn Helgaas, Jonathan Corbet, Shuah Khan,
	Thomas Gleixner, Ingo Molnar, Dave Hansen, H. Peter Anvin,
	linux-pci, x86, linux-doc, linux-kernel

Even when MMCONFIG is available, PCIe currently defaults to IO-based
accesses for the first 256 bytes of config space.

IO-based accesses are serialized, and can cause substantial scalability
issues when initializing a large number of devices on a system with many
CPUs. (Those often coincide, because many systems have PCIe devices per
CPU.)

Prefer MMCONFIG even for the first 256 bytes of config space.

The only systems known to have an issue with this are some K8 systems,
where the BIOS advertises MMCONFIG but it fails for northbridge devices;
disable this on such systems, and provide a `pci=no_prefer_mmconf` to in
case any other such systems arise.

Tested on an AWS c8i.48xlarge system, with 192 CPUs, over five boots
with and five boots without.

Without:
PCI init 266ms
Boot to minimal /init 726ms

With:
PCI init 213ms (~20% faster)
Boot to minimal /init 661ms (~9% faster)

Signed-off-by: Josh Triplett <josh@joshtriplett.org>
---
v2: Remove Kconfig option, as suggested by Borislav Petkov. Add quirk
    for K8 systems.

Happy to see either version applied, whichever folks prefer.

 .../admin-guide/kernel-parameters.txt         |  2 +
 arch/x86/include/asm/pci_x86.h                |  6 +++
 arch/x86/pci/common.c                         | 46 +++++++++++++++++--
 arch/x86/pci/init.c                           |  2 +
 4 files changed, 53 insertions(+), 3 deletions(-)

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 0b3c140babd9..871b1f189291 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -5079,6 +5079,8 @@ Kernel parameters
 				disable the use of PCIE advanced error reporting.
 		nodomains	[PCI] Disable support for multiple PCI
 				root domains (aka PCI segments, in ACPI-speak).
+		no_prefer_mmconf	[X86] Don't prefer use of MMCONFIG for
+				base (<256) config space accesses.
 		nommconf	[X86] Disable use of MMCONFIG for PCI
 				Configuration
 		check_enable_amd_mmconf [X86] check for and enable
diff --git a/arch/x86/include/asm/pci_x86.h b/arch/x86/include/asm/pci_x86.h
index 70533fdcbf02..eca53d27e99d 100644
--- a/arch/x86/include/asm/pci_x86.h
+++ b/arch/x86/include/asm/pci_x86.h
@@ -138,6 +138,12 @@ static inline int pci_direct_probe(void) { return -1; }
 static inline  void pci_direct_init(int type) { }
 #endif
 
+#ifdef CONFIG_PCI_MMCONFIG
+extern void pci_quirk_no_prefer_mmconf(void);
+#else
+static inline void pci_quirk_no_prefer_mmconf(void) { }
+#endif
+
 #ifdef CONFIG_PCI_BIOS
 extern void pci_pcbios_init(void);
 #else
diff --git a/arch/x86/pci/common.c b/arch/x86/pci/common.c
index 63105b29fbf5..5ad2f108686e 100644
--- a/arch/x86/pci/common.c
+++ b/arch/x86/pci/common.c
@@ -26,6 +26,9 @@ unsigned int pci_probe = PCI_PROBE_BIOS | PCI_PROBE_CONF1 | PCI_PROBE_CONF2 |
 
 static int pci_bf_sort;
 int pci_routeirq;
+#ifdef CONFIG_PCI_MMCONFIG
+static int pci_no_prefer_mmconf;
+#endif
 int noioapicquirk;
 #ifdef CONFIG_X86_REROUTE_FOR_BROKEN_BOOT_IRQS
 int noioapicreroute = 0;
@@ -40,9 +43,20 @@ const struct pci_raw_ops *__read_mostly raw_pci_ext_ops;
 int raw_pci_read(unsigned int domain, unsigned int bus, unsigned int devfn,
 						int reg, int len, u32 *val)
 {
+	bool tried_mmconf = false;
+
+#ifdef CONFIG_PCI_MMCONFIG
+	if (!pci_no_prefer_mmconf && raw_pci_ext_ops == &pci_mmcfg) {
+		int ret = raw_pci_ext_ops->read(domain, bus, devfn, reg, len, val);
+
+		if (ret != -EINVAL)
+			return ret;
+		tried_mmconf = true;
+	}
+#endif
 	if (domain == 0 && reg < 256 && raw_pci_ops)
 		return raw_pci_ops->read(domain, bus, devfn, reg, len, val);
-	if (raw_pci_ext_ops)
+	if (!tried_mmconf && raw_pci_ext_ops)
 		return raw_pci_ext_ops->read(domain, bus, devfn, reg, len, val);
 	return -EINVAL;
 }
@@ -50,9 +64,20 @@ int raw_pci_read(unsigned int domain, unsigned int bus, unsigned int devfn,
 int raw_pci_write(unsigned int domain, unsigned int bus, unsigned int devfn,
 						int reg, int len, u32 val)
 {
+	bool tried_mmconf = false;
+
+#ifdef CONFIG_PCI_MMCONFIG
+	if (!pci_no_prefer_mmconf && raw_pci_ext_ops == &pci_mmcfg) {
+		int ret = raw_pci_ext_ops->write(domain, bus, devfn, reg, len, val);
+
+		if (ret != -EINVAL)
+			return ret;
+		tried_mmconf = true;
+	}
+#endif
 	if (domain == 0 && reg < 256 && raw_pci_ops)
 		return raw_pci_ops->write(domain, bus, devfn, reg, len, val);
-	if (raw_pci_ext_ops)
+	if (!tried_mmconf && raw_pci_ext_ops)
 		return raw_pci_ext_ops->write(domain, bus, devfn, reg, len, val);
 	return -EINVAL;
 }
@@ -455,6 +480,18 @@ void __init dmi_check_pciprobe(void)
 	dmi_check_system(pciprobe_dmi_table);
 }
 
+#ifdef CONFIG_PCI_MMCONFIG
+void __init pci_quirk_no_prefer_mmconf(void)
+{
+	/*
+	 * Some K8 systems advertise MMCONFIG but it fails for the northbridge.
+	 */
+	if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
+	    boot_cpu_data.x86 == 0x0f)
+		pci_no_prefer_mmconf = 1;
+}
+#endif
+
 void pcibios_scan_root(int busnum)
 {
 	struct pci_bus *bus;
@@ -551,7 +588,10 @@ char *__init pcibios_setup(char *str)
 	}
 #endif
 #ifdef CONFIG_PCI_MMCONFIG
-	else if (!strcmp(str, "nommconf")) {
+	else if (!strcmp(str, "no_prefer_mmconf")) {
+		pci_no_prefer_mmconf = 1;
+		return NULL;
+	} else if (!strcmp(str, "nommconf")) {
 		pci_probe &= ~PCI_PROBE_MMCONF;
 		return NULL;
 	}
diff --git a/arch/x86/pci/init.c b/arch/x86/pci/init.c
index 0bb3b8b44e4e..f452adc25c37 100644
--- a/arch/x86/pci/init.c
+++ b/arch/x86/pci/init.c
@@ -13,6 +13,8 @@ static __init int pci_arch_init(void)
 
 	type = pci_direct_probe();
 
+	pci_quirk_no_prefer_mmconf();
+
 	if (!(pci_probe & PCI_PROBE_NOEARLY))
 		pci_mmcfg_early_init();
 
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] pci: Prefer MMCONFIG for all config space accesses if available
  2026-12-01  5:35 [PATCH v2] pci: Prefer MMCONFIG for all config space accesses if available Josh Triplett
@ 2026-12-01  5:49 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-01  5:49 UTC (permalink / raw)
  To: Josh Triplett, linux-pci

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] x86/pci: 32-bit pci_mmcfg preference adds fixmap TLB flushes
  under lock
--

commit ed763afa39424e0a8e5b03379200b33b56222587
Author: Josh Triplett <josh@joshtriplett.org>

pci: Prefer MMCONFIG for all config space accesses if available

This commit updates raw_pci_read() and raw_pci_write() on x86 to prefer
MMCONFIG (pci_mmcfg) even for the first 256 bytes of PCI config space,
with a quirk disabling this on AMD K8 systems and a pci=no_prefer_mmconf
boot parameter.

> diff --git a/arch/x86/pci/common.c b/arch/x86/pci/common.c
> index 63105b29fbf54..5ad2f108686ec 100644
> --- a/arch/x86/pci/common.c
> +++ b/arch/x86/pci/common.c
[ ... ]
> @@ -40,9 +43,20 @@ const struct pci_raw_ops *__read_mostly raw_pci_ext_ops;
>  int raw_pci_read(unsigned int domain, unsigned int bus, unsigned int devfn,
>  						int reg, int len, u32 *val)
>  {
> +	bool tried_mmconf = false;
> +
> +#ifdef CONFIG_PCI_MMCONFIG
> +	if (!pci_no_prefer_mmconf && raw_pci_ext_ops == &pci_mmcfg) {
> +		int ret = raw_pci_ext_ops->read(domain, bus, devfn, reg, len, val);
> +
> +		if (ret != -EINVAL)
> +			return ret;
> +		tried_mmconf = true;
> +	}
> +#endif
>  	if (domain == 0 && reg < 256 && raw_pci_ops)
>  		return raw_pci_ops->read(domain, bus, devfn, reg, len, val);
> -	if (raw_pci_ext_ops)
> +	if (!tried_mmconf && raw_pci_ext_ops)
>  		return raw_pci_ext_ops->read(domain, bus, devfn, reg, len, val);
>  	return -EINVAL;
>  }
> @@ -50,9 +64,20 @@ int raw_pci_read(unsigned int domain, unsigned int bus, unsigned int devfn,
>  int raw_pci_write(unsigned int domain, unsigned int bus, unsigned int devfn,
>  						int reg, int len, u32 val)
>  {
> +	bool tried_mmconf = false;
> +
> +#ifdef CONFIG_PCI_MMCONFIG
> +	if (!pci_no_prefer_mmconf && raw_pci_ext_ops == &pci_mmcfg) {
> +		int ret = raw_pci_ext_ops->write(domain, bus, devfn, reg, len, val);
> +
> +		if (ret != -EINVAL)
> +			return ret;
> +		tried_mmconf = true;
> +	}
> +#endif
>  	if (domain == 0 && reg < 256 && raw_pci_ops)
>  		return raw_pci_ops->write(domain, bus, devfn, reg, len, val);
> -	if (raw_pci_ext_ops)
> +	if (!tried_mmconf && raw_pci_ext_ops)
>  		return raw_pci_ext_ops->write(domain, bus, devfn, reg, len, val);
>  	return -EINVAL;
>  }

[Severity: Medium]
Should preferring pci_mmcfg for base config space be restricted to 64-bit
x86?

On 32-bit x86, pci_mmcfg is not lockless and does not keep ECAM regions
permanently mapped.  Instead, pci_mmcfg_read() and pci_mmcfg_write() walk
pci_mmcfg_list under RCU, take pci_config_lock, and multiplex a single
FIX_PCIE_MCFG fixmap slot across all devices and CPUs:

arch/x86/pci/mmconfig_32.c:pci_exp_set_dev_base() {
	u32 dev_base = base | PCI_MMCFG_BUS_OFFSET(bus) | (devfn << 12);
	int cpu = smp_processor_id();
	if (dev_base != mmcfg_last_accessed_device ||
	    cpu != mmcfg_last_accessed_cpu) {
		mmcfg_last_accessed_device = dev_base;
		mmcfg_last_accessed_cpu = cpu;
		set_fixmap_nocache(FIX_PCIE_MCFG, dev_base);
	}
}

During PCI enumeration or interleaved config accesses across devices or
CPUs on CONFIG_X86_32, routing reg < 256 through pci_mmcfg still
serializes on pci_config_lock while adding a PTE write and TLB flush via
set_fixmap_nocache() whenever the target device or CPU changes.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/4b37c09a330f78db0ac3265a9b8e278d1d7d6f1c.1796103197.git.josh@joshtriplett.org?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-01  5:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01  5:35 [PATCH v2] pci: Prefer MMCONFIG for all config space accesses if available Josh Triplett
2026-12-01  5:49 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox