Skip to contentSkip to conversation

x86/mcfg: relax checking of MCFG position

PATCH Superseded Roger Pau Monne 2 participants 6 messages Jun 4

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover x86/mcfg: relax checking of MCFG position

Hello,

First patch is a trivial cleanup of header sorting, second patch is the real fix. Possibly something to consider for inclusion in 4.22.

Roger Pau Monne (2):
x86/mcfg: sort header includes
x86/mcfg: relax memory map checks on newer firmware

 xen/arch/x86/x86_64/mmconfig-shared.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/2 x86/mcfg: sort header includes

No functional change intended.

---
 xen/arch/x86/x86_64/mmconfig-shared.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
xen/arch/x86/x86_64/mmconfig-shared.c +5 -3
index ab082b5f5b37..d0cbc151705d 100644
@@ -12,17 +12,19 @@
  * Author: Allen Kay <[email protected]> -  adapted to xen from Linux
  */
 
+#include <xen/acpi.h>
 #include <xen/init.h>
 #include <xen/mm.h>
 #include <xen/param.h>
-#include <xen/acpi.h>
-#include <xen/xmalloc.h>
 #include <xen/pci.h>
-#include <xen/pci_regs.h>
 #include <xen/pci_ids.h>
+#include <xen/pci_regs.h>
+#include <xen/xmalloc.h>
+
 #include <asm/e820.h>
 #include <asm/msr.h>
 #include <asm/msr-index.h>
+
 #include <public/physdev.h>
 
 #include "mmconfig.h"
Signature · 1 line
2.53.0
#3 lore raw

PATCH 2/2 x86/mcfg: relax memory map checks on newer firmware

In reply to Roger Pau Monne

Per PCI Firmware 3.3 specification, section 4.1.2, ECAM space must be reserved by declaring a motherboard resource, but there's no requirement to mention it in E820, so we shouldn't look at E820 to validate the ECAM space described by MCFG. The specification additionally states that: the resources can optionally be returned in Int15 E820h or EFIGetMemoryMap as reserved memory.

The more strict logic was introduced in Linux in 2006 as 946f2ee5c731 ("[PATCH] i386/x86-64: Check that MCFG points to an e820 reserved area"). This was picked up by Xen when MCFG support was added in 3b35911d709e ("Enable pci mmcfg and ATS for x86_64"). Apply the same approach that Linux has done in 199f968f1484 ("x86/pci: Skip early E820 check for ECAM region") and relax the strict reserved region checking so it's only done for firmware manufactured prior to 2016.

When dom0 is booted it can always prevent access to misconfigured MCFG regions by using the PHYSDEVOP_pci_mmcfg_reserved hypercall. This brings Xen's early usage of MCFG (prior to ACPI AML parsing) in line with the implementation in Linux.

This fixes an issue with detection of extended capabilities when running Xen on a Lenovo system that doesn't list the MCFG area as an EfiMemoryMappedIO region in the EFI memory map.

---
I'm not sure what's the best way to attribute the logic here with the Linux commit that introduced this, more than referencing it in the commit message text. The code is too different for me to attempt to label this change as a backport of the original Linux commit.

---
 xen/arch/x86/x86_64/mmconfig-shared.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
xen/arch/x86/x86_64/mmconfig-shared.c +11 -1
index d0cbc151705d..e24a78c8d1d3 100644
@@ -13,6 +13,7 @@
  */
 
 #include <xen/acpi.h>
+#include <xen/dmi.h>
 #include <xen/init.h>
 #include <xen/mm.h>
 #include <xen/param.h>
@@ -369,12 +370,15 @@ static bool __init pci_mmcfg_reject_broken(void)
     typeof(pci_mmcfg_config[0]) *cfg;
     int i;
     bool valid = true;
+    int year;
 
     if ((pci_mmcfg_config_num == 0) ||
         (pci_mmcfg_config == NULL) ||
         (pci_mmcfg_config[0].address == 0))
         return 0;
 
+    dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL);
+
     for (i = 0; i < pci_mmcfg_config_num; i++) {
         u64 addr, size;
 
@@ -390,7 +394,13 @@ static bool __init pci_mmcfg_reject_broken(void)
                (unsigned int)cfg->start_bus_number,
                (unsigned int)cfg->end_bus_number);
 
-        if ( !is_mmconf_reserved(addr, size, i, cfg) ||
+        /*
+         * For firmware from 2016 or later relax the checking and also consider
+         * MCFG regions in holes on the memory map as valid.
+         */
+        if ( ((year < 2016 || !is_memory_hole(maddr_to_mfn(addr),
+                                              maddr_to_mfn(addr + size - 1))) &&
+              !is_mmconf_reserved(addr, size, i, cfg)) ||
              pci_mmcfg_arch_enable(i) < 0 )
         {
             pci_mmcfg_arch_disable(i);
Signature · 1 line
2.53.0
#4 lore raw

Re: [PATCH for-4.22 2/2] x86/mcfg: relax memory map checks on newer firmware

Quoted 6 lines · Roger Pau Monne

Per PCI Firmware 3.3 specification, section 4.1.2, ECAM space must be reserved by declaring a motherboard resource, but there's no requirement to mention it in E820, so we shouldn't look at E820 to validate the ECAM space described by MCFG. The specification additionally states that: the resources can optionally be returned in Int15 E820h or EFIGetMemoryMap as reserved memory.

I'd take out the ", so we shouldn't ..." clause.  I see it came from the Linux commit, but it wasn't great there either.  It's a piece of opinion in the middle of quotes from a spec.

Then, I think you want a new paragraph between these two, saying explicitly that some Lenvovo systems do not mark MMCFG in the memory map.

Quoted 17 lines

The more strict logic was introduced in Linux in 2006 as 946f2ee5c731 ("[PATCH] i386/x86-64: Check that MCFG points to an e820 reserved area"). This was picked up by Xen when MCFG support was added in 3b35911d709e ("Enable pci mmcfg and ATS for x86_64"). Apply the same approach that Linux has done in 199f968f1484 ("x86/pci: Skip early E820 check for ECAM region") and relax the strict reserved region checking so it's only done for firmware manufactured prior to 2016.

When dom0 is booted it can always prevent access to misconfigured MCFG regions by using the PHYSDEVOP_pci_mmcfg_reserved hypercall. This brings Xen's early usage of MCFG (prior to ACPI AML parsing) in line with the implementation in Linux.

This fixes an issue with detection of extended capabilities when running Xen on a Lenovo system that doesn't list the MCFG area as an EfiMemoryMappedIO region in the EFI memory map.

Quoted 6 lines

---
I'm not sure what's the best way to attribute the logic here with the Linux commit that introduced this, more than referencing it in the commit message text. The code is too different for me to attempt to label this change as a backport of the original Linux commit.

I think your commit message is adequate.  It does provide all the reasoning.

Quoted 34 lines
---
 xen/arch/x86/x86_64/mmconfig-shared.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
xen/arch/x86/x86_64/mmconfig-shared.c +4 -0
index d0cbc151705d..e24a78c8d1d3 100644
@@ -13,6 +13,7 @@
  */
 
 #include <xen/acpi.h>
+#include <xen/dmi.h>
 #include <xen/init.h>
 #include <xen/mm.h>
 #include <xen/param.h>
@@ -369,12 +370,15 @@ static bool __init pci_mmcfg_reject_broken(void)
     typeof(pci_mmcfg_config[0]) *cfg;
     int i;
     bool valid = true;
+    int year;
 
     if ((pci_mmcfg_config_num == 0) ||
         (pci_mmcfg_config == NULL) ||
         (pci_mmcfg_config[0].address == 0))
         return 0;
 
+    dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL);
+
     for (i = 0; i < pci_mmcfg_config_num; i++) {
         u64 addr, size;
 
@@ -390,7 +394,13 @@ static bool __init pci_mmcfg_reject_broken(void)
                (unsigned int)cfg->start_bus_number,

xen/arch/x86/x86_64/mmconfig-shared.c static bool __init pci_mmcfg_reject_broken(void) · line 403

Quoted 10 lines
xen/arch/x86/x86_64/mmconfig-shared.c +7 -1
                (unsigned int)cfg->end_bus_number);
 
-        if ( !is_mmconf_reserved(addr, size, i, cfg) ||
+        /*
+         * For firmware from 2016 or later relax the checking and also consider
+         * MCFG regions in holes on the memory map as valid.
+         */
+        if ( ((year < 2016 || !is_memory_hole(maddr_to_mfn(addr),
+                                              maddr_to_mfn(addr + size - 1))) &&
+              !is_mmconf_reserved(addr, size, i, cfg)) ||

This comment is half stale already, as "relax" is really only relevant to the prior behaviour.

"For firmwares prior to 2016, confirm that MMCFG is marked as reserved. For 2016 and later, also allow MMCFG being in a hole."

It's also worth saying that this fix is different to Linux's.  Linux simply ignores the E820 on anything newer than 2016.  Personally I prefer the more cautious approach of saying reserved-or-hole, but this should be called out in the commit message, I think.

~Andrew

#5 lore raw

Re: [PATCH for-4.22 2/2] x86/mcfg: relax memory map checks on newer firmware

Quoted 8 lines · Andrew Cooper
Quoted 6 lines · Roger Pau Monne

Per PCI Firmware 3.3 specification, section 4.1.2, ECAM space must be reserved by declaring a motherboard resource, but there's no requirement to mention it in E820, so we shouldn't look at E820 to validate the ECAM space described by MCFG. The specification additionally states that: the resources can optionally be returned in Int15 E820h or EFIGetMemoryMap as reserved memory.

Quoted 6 lines

I'd take out the ", so we shouldn't ..." clause.  I see it came from the Linux commit, but it wasn't great there either.  It's a piece of opinion in the middle of quotes from a spec.

Then, I think you want a new paragraph between these two, saying explicitly that some Lenvovo systems do not mark MMCFG in the memory map.

OK, I will move the last paragraph here then.

Quoted 74 lines
Quoted 17 lines

The more strict logic was introduced in Linux in 2006 as 946f2ee5c731 ("[PATCH] i386/x86-64: Check that MCFG points to an e820 reserved area"). This was picked up by Xen when MCFG support was added in 3b35911d709e ("Enable pci mmcfg and ATS for x86_64"). Apply the same approach that Linux has done in 199f968f1484 ("x86/pci: Skip early E820 check for ECAM region") and relax the strict reserved region checking so it's only done for firmware manufactured prior to 2016.

When dom0 is booted it can always prevent access to misconfigured MCFG regions by using the PHYSDEVOP_pci_mmcfg_reserved hypercall. This brings Xen's early usage of MCFG (prior to ACPI AML parsing) in line with the implementation in Linux.

This fixes an issue with detection of extended capabilities when running Xen on a Lenovo system that doesn't list the MCFG area as an EfiMemoryMappedIO region in the EFI memory map.

Quoted 6 lines

---
I'm not sure what's the best way to attribute the logic here with the Linux commit that introduced this, more than referencing it in the commit message text. The code is too different for me to attempt to label this change as a backport of the original Linux commit.

I think your commit message is adequate.  It does provide all the reasoning.

Quoted 34 lines
---
 xen/arch/x86/x86_64/mmconfig-shared.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
xen/arch/x86/x86_64/mmconfig-shared.c +4 -0
index d0cbc151705d..e24a78c8d1d3 100644
@@ -13,6 +13,7 @@
  */
 
 #include <xen/acpi.h>
+#include <xen/dmi.h>
 #include <xen/init.h>
 #include <xen/mm.h>
 #include <xen/param.h>
@@ -369,12 +370,15 @@ static bool __init pci_mmcfg_reject_broken(void)
     typeof(pci_mmcfg_config[0]) *cfg;
     int i;
     bool valid = true;
+    int year;
 
     if ((pci_mmcfg_config_num == 0) ||
         (pci_mmcfg_config == NULL) ||
         (pci_mmcfg_config[0].address == 0))
         return 0;
 
+    dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL);
+
     for (i = 0; i < pci_mmcfg_config_num; i++) {
         u64 addr, size;
 
@@ -390,7 +394,13 @@ static bool __init pci_mmcfg_reject_broken(void)
                (unsigned int)cfg->start_bus_number,

xen/arch/x86/x86_64/mmconfig-shared.c static bool __init pci_mmcfg_reject_broken(void) · line 403

Quoted 10 lines
xen/arch/x86/x86_64/mmconfig-shared.c +7 -1
                (unsigned int)cfg->end_bus_number);
 
-        if ( !is_mmconf_reserved(addr, size, i, cfg) ||
+        /*
+         * For firmware from 2016 or later relax the checking and also consider
+         * MCFG regions in holes on the memory map as valid.
+         */
+        if ( ((year < 2016 || !is_memory_hole(maddr_to_mfn(addr),
+                                              maddr_to_mfn(addr + size - 1))) &&
+              !is_mmconf_reserved(addr, size, i, cfg)) ||

This comment is half stale already, as "relax" is really only relevant to the prior behaviour.

Quoted 7 lines

"For firmwares prior to 2016, confirm that MMCFG is marked as reserved. For 2016 and later, also allow MMCFG being in a hole."

It's also worth saying that this fix is different to Linux's.  Linux simply ignores the E820 on anything newer than 2016.  Personally I prefer the more cautious approach of saying reserved-or-hole, but this should be called out in the commit message, I think.

I've adjusted the in-code comment and reworded the commit message to notice this divergence with Linux.

Thanks, Roger.