Skip to contentSkip to conversation

map GHES memory region according to EFI memory map

PATCH Superseded Jonathan (Zhixiong) Zhang 2 participants 7 messages Jun 13, 2015 – Jun 15, 2015

Subsystems (7)

v4 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v4 cover map GHES memory region according to EFI memory map

From: "Jonathan (Zhixiong) Zhang" <[email protected]>

On a platform with APEI (ACPI Platform Error Interface) enabled, firmware updates a memory region with hardware error record using nocache attribute. When OS reads the region, since it maps the region with cacahed attribute even though EFI memory map defines this region as uncached, OS gets stale data and errorneously reports there is no new HW error.

When ghes driver maps the memory region, it uses the cache attribute according to EFI memory map, if EFI memory map feature is enabled at runtime.

Since both arch/x86 and arch/ia64 implemented architecture agnostic EFI memory map attribue lookup function efi_memattributes(), the code is moved from arch/x86 into EFI subsystem and is declared as __weak; archs other than ia64 should not override the default implementation.

V4:
1. Introduced arch_apei_get_mem_attributes() to allow arch specific implementation of getting pgprot_t appropriate for a physical address.
2. Implemented arch_apei_get_mem_attributes() for x86 and for arm64.

V3:
1. Rebased to v4.1-rc7.
2. Moved efi_mem_attributes() from arch/x86 to drivers/firmware/efi and declared it as __weak.
3. Introduced ARCH_APEI_PAGE_KERNEL_UC to allow arch specific page protection type for UC.
4. Removed efi_ioremap(). It can not be used for GHES memory region mapping purpose since ioremap can not be used in atomic context.

V2:
1. Rebased to v4.1-rc5.
2. Split removal of efi_mem_attributes() and creation of efi_ioremap() into two patches.

Jonathan (Zhixiong) Zhang (4):
efi: x86: rearrange efi_mem_attributes()
x86: acpi: add arch_apei_get_mem_attributes()
arm64: apei: implement arch_apei_get_mem_attributes() acpi, apei: use appropriate pgprot_t to map GHES memory

 arch/arm64/kernel/Makefile  |  1 +
 arch/arm64/kernel/apei.c    | 27 +++++++++++++++++++++++++++
 arch/x86/kernel/acpi/apei.c | 10 ++++++++++
 arch/x86/platform/efi/efi.c | 18 ------------------
 drivers/acpi/apei/ghes.c    |  6 ++++--
 drivers/firmware/efi/efi.c  | 18 ++++++++++++++++++
 include/acpi/apei.h         |  1 +
 7 files changed, 61 insertions(+), 20 deletions(-)
 create mode 100644 arch/arm64/kernel/apei.c
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#1 lore raw

PATCH v4 1/4 efi: x86: rearrange efi_mem_attributes()

From: "Jonathan (Zhixiong) Zhang" <[email protected]>

x86 and ia64 implement efi_mem_attributes() differently. This function needs to be available for other arch (such as arm64) as well, such as for the purpose of ACPI/APEI.

ia64 efi does not setup memmap variable and does not set EFI_MEMMAP flag, so it needs to have its unique implementation of efi_mem_attributes().

Move efi_mem_attributes() implementation from x86 to efi, and declare it with __weak. It is recommended that other archs should not override the default implementation.

---
 arch/x86/platform/efi/efi.c | 18 ------------------
 drivers/firmware/efi/efi.c  | 18 ++++++++++++++++++
 2 files changed, 18 insertions(+), 18 deletions(-)
arch/x86/platform/efi/efi.c +0 -18
index 02744df576d5..10bd5289c593 100644
@@ -926,24 +926,6 @@ u32 efi_mem_type(unsigned long phys_addr)
 	return 0;
 }
 
-u64 efi_mem_attributes(unsigned long phys_addr)
-{
-	efi_memory_desc_t *md;
-	void *p;
-
-	if (!efi_enabled(EFI_MEMMAP))
-		return 0;
-
-	for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
-		md = p;
-		if ((md->phys_addr <= phys_addr) &&
-		    (phys_addr < (md->phys_addr +
-				  (md->num_pages << EFI_PAGE_SHIFT))))
-			return md->attribute;
-	}
-	return 0;
-}
-
 static int __init arch_parse_efi_cmdline(char *str)
 {
 	if (parse_option_str(str, "old_map"))
drivers/firmware/efi/efi.c +18 -0
index 3061bb8629dc..88531b0dee80 100644
@@ -517,3 +517,21 @@ char * __init efi_md_typeattr_format(char *buf, size_t size,
 			 attr & EFI_MEMORY_UC      ? "UC"  : "");
 	return buf;
 }
+
+u64 __weak efi_mem_attributes(unsigned long phys_addr)
+{
+	efi_memory_desc_t *md;
+	void *p;
+
+	if (!efi_enabled(EFI_MEMMAP))
+		return 0;
+
+	for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
+		md = p;
+		if ((md->phys_addr <= phys_addr) &&
+		    (phys_addr < (md->phys_addr +
+		    (md->num_pages << EFI_PAGE_SHIFT))))
+			return md->attribute;
+	}
+	return 0;
+}
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#2 lore raw

Re: [PATCH V4 1/4] efi: x86: rearrange efi_mem_attributes()

Quoted 9 lines · Jonathan (Zhixiong) Zhang

From: "Jonathan (Zhixiong) Zhang" <[email protected]>

x86 and ia64 implement efi_mem_attributes() differently. This function needs to be available for other arch (such as arm64) as well, such as for the purpose of ACPI/APEI.

ia64 efi does not setup memmap variable and does not set EFI_MEMMAP flag, so it needs to have its unique implementation of efi_mem_attributes().

Something like the above paragraph...

drivers/firmware/efi/efi.c char * __init efi_md_typeattr_format(char *buf, size_t size, · line 519

Quoted 4 lines
drivers/firmware/efi/efi.c +0 -0
@@ -517,3 +517,21 @@ char * __init efi_md_typeattr_format(char *buf, size_t size,
 			 attr & EFI_MEMORY_UC      ? "UC"  : "");
 	return buf;
 }

needs to go here, i.e. as documentation for efi_mem_atributes(). You need to explain why this function is marked as __weak at the function definition site.

Quoted 7 lines
drivers/firmware/efi/efi.c +7 -0
+u64 __weak efi_mem_attributes(unsigned long phys_addr)
+{
+	efi_memory_desc_t *md;
+	void *p;
+
+	if (!efi_enabled(EFI_MEMMAP))
+		return 0;

drivers/firmware/efi/efi.c char * __init efi_md_typeattr_format(char *buf, size_t size, · line 537

Quoted 10 lines
drivers/firmware/efi/efi.c +10 -0
+
+	for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
+		md = p;
+		if ((md->phys_addr <= phys_addr) &&
+		    (phys_addr < (md->phys_addr +
+		    (md->num_pages << EFI_PAGE_SHIFT))))
+			return md->attribute;
+	}
+	return 0;
+}

because otherwise people are going to read this in the future and think,

"efi_mem_attribute() doesn't quite work how I want it to for my arch, but it's __weak, so I'll override it"

which is absolutely not the practice we should be promoting.

How about something like this,

/*
* efi_mem_attributes - lookup memmap attributes for physical address
* @phys_addr: the physical address to lookup
*
* Search in the EFI memory map for the region covering
* @phys_addr. Returns the EFI memory attributes if the region
* was found in the memory map, 0 otherwise.
*
* Despite being marked __weak most architectures should *not*
* override this function. It is __weak solely for the benefit
* of ia64 which has a funky EFI memory map that doesn't work
* the same way as other architectures.
*/

Signature · 1 line
Matt Fleming, Intel Open Source Technology Center
#3 lore raw

Re: [PATCH V4 1/4] efi: x86: rearrange efi_mem_attributes()

Quoted 49 lines · Matt Fleming
Quoted 9 lines · Jonathan (Zhixiong) Zhang

From: "Jonathan (Zhixiong) Zhang" <[email protected]>

x86 and ia64 implement efi_mem_attributes() differently. This function needs to be available for other arch (such as arm64) as well, such as for the purpose of ACPI/APEI.

ia64 efi does not setup memmap variable and does not set EFI_MEMMAP flag, so it needs to have its unique implementation of efi_mem_attributes().

Something like the above paragraph...

drivers/firmware/efi/efi.c char * __init efi_md_typeattr_format(char *buf, size_t size, · line 519

Quoted 4 lines
drivers/firmware/efi/efi.c +0 -0
@@ -517,3 +517,21 @@ char * __init efi_md_typeattr_format(char *buf, size_t size,
  			 attr & EFI_MEMORY_UC      ? "UC"  : "");
  	return buf;
  }

needs to go here, i.e. as documentation for efi_mem_atributes(). You need to explain why this function is marked as __weak at the function definition site.

Quoted 7 lines
drivers/firmware/efi/efi.c +7 -0
+u64 __weak efi_mem_attributes(unsigned long phys_addr)
+{
+	efi_memory_desc_t *md;
+	void *p;
+
+	if (!efi_enabled(EFI_MEMMAP))
+		return 0;

drivers/firmware/efi/efi.c char * __init efi_md_typeattr_format(char *buf, size_t size, · line 537

Quoted 10 lines
drivers/firmware/efi/efi.c +10 -0
+
+	for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
+		md = p;
+		if ((md->phys_addr <= phys_addr) &&
+		    (phys_addr < (md->phys_addr +
+		    (md->num_pages << EFI_PAGE_SHIFT))))
+			return md->attribute;
+	}
+	return 0;
+}

because otherwise people are going to read this in the future and think,

"efi_mem_attribute() doesn't quite work how I want it to for my arch, but it's __weak, so I'll override it"

which is absolutely not the practice we should be promoting.

How about something like this,

Quoted 14 lines

/*
* efi_mem_attributes - lookup memmap attributes for physical address
* @phys_addr: the physical address to lookup
*
* Search in the EFI memory map for the region covering
* @phys_addr. Returns the EFI memory attributes if the region
* was found in the memory map, 0 otherwise.
*
* Despite being marked __weak most architectures should *not*
* override this function. It is __weak solely for the benefit
* of ia64 which has a funky EFI memory map that doesn't work
* the same way as other architectures.
*/

Thank you Matt for the suggestion. Will do so accordingly.

Signature · 3 lines
Jonathan (Zhixiong) Zhang
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#4 lore raw

PATCH v4 2/4 x86: acpi: add arch_apei_get_mem_attributes()

In reply to Jonathan (Zhixiong) Zhang

From: "Jonathan (Zhixiong) Zhang" <[email protected]>

... to allow arch specific implementation of getting page protection type associated with a physical address.

If the physical address has memory attributes defined by EFI memmap as EFI_MEMORY_UC, the page protection type is PAGE_KENERL_NOCACHE. Otherwise, the page protection type is PAGE_KERNEL.

---
 arch/x86/kernel/acpi/apei.c | 10 ++++++++++
 include/acpi/apei.h         |  1 +
 2 files changed, 11 insertions(+)
arch/x86/kernel/acpi/apei.c +10 -0
index c280df6b2aa2..cfcde9142b94 100644
@@ -14,6 +14,8 @@
 
 #include <acpi/apei.h>
 
+#include <linux/efi.h>
+
 #include <asm/mce.h>
 #include <asm/tlbflush.h>
 
@@ -60,3 +62,11 @@ void arch_apei_flush_tlb_one(unsigned long addr)
 {
 	__flush_tlb_one(addr);
 }
+
+pgprot_t arch_apei_get_mem_attribute(phys_addr_t addr)
+{
+	if (efi_mem_attributes(addr) & EFI_MEMORY_UC)
+		return PAGE_KERNEL_NOCACHE;
+	else
+		return PAGE_KERNEL;
+}
include/acpi/apei.h +1 -0
index 76284bb560a6..4dd8c490e24f 100644
@@ -45,6 +45,7 @@ int erst_clear(u64 record_id);
 int arch_apei_enable_cmcff(struct acpi_hest_header *hest_hdr, void *data);
 void arch_apei_report_mem_error(int sev, struct cper_sec_mem_err *mem_err);
 void arch_apei_flush_tlb_one(unsigned long addr);
+pgprot_t arch_apei_get_mem_attribute(phys_addr_t addr);
 
 #endif
 #endif
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#5 lore raw

PATCH v4 3/4 arm64: apei: implement arch_apei_get_mem_attributes()

In reply to Jonathan (Zhixiong) Zhang

From: "Jonathan (Zhixiong) Zhang" <[email protected]>

If the physical address has memory attributes defined by EFI memmap as EFI_MEMORY_UC, the page protection type is PROT_DEVICE_nGnRE. Otherwise, the page protection type is PAGE_KERNEL.

---
 arch/arm64/kernel/Makefile |  1 +
 arch/arm64/kernel/apei.c   | 27 +++++++++++++++++++++++++++
 2 files changed, 28 insertions(+)
 create mode 100644 arch/arm64/kernel/apei.c
arch/arm64/kernel/Makefile +1 -0
index 426d0763c81b..c135f1e4e3b8 100644
@@ -36,6 +36,7 @@ arm64-obj-$(CONFIG_EFI)			+= efi.o efi-stub.o efi-entry.o
 arm64-obj-$(CONFIG_PCI)			+= pci.o
 arm64-obj-$(CONFIG_ARMV8_DEPRECATED)	+= armv8_deprecated.o
 arm64-obj-$(CONFIG_ACPI)		+= acpi.o
+arm64-obj-$(CONFIG_ACPI_APEI)		+= apei.o
 
 obj-y					+= $(arm64-obj-y) vdso/
 obj-m					+= $(arm64-obj-m)
arch/arm64/kernel/apei.c new file +27 -0
new file mode 100644
index 000000000000..9c6b62f40ad7
@@ -0,0 +1,27 @@
+/*
+ * Arch-specific APEI-related functions.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <acpi/apei.h>
+
+#include <linux/efi.h>
+
+#include <asm/pgtable.h>
+
+pgprot_t arch_apei_get_mem_attribute(phys_addr_t addr)
+{
+	if (efi_mem_attributes(addr) & EFI_MEMORY_UC)
+		return PROT_DEVICE_nGnRE;
+	else
+		return PAGE_KERNEL;
+}
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#6 lore raw

PATCH v4 4/4 acpi, apei: use appropriate pgprot_t to map GHES memory

In reply to Jonathan (Zhixiong) Zhang

From: "Jonathan (Zhixiong) Zhang" <[email protected]>

With ACPI APEI firmware first handling, generic hardware error record is updated by firmware in GHES memory region. When firmware updated GHES memory region with uncached access attribute, Linux reads stale data from cache.

GHES memory region should be mapped with page protection type according to what is returned from arch_apei_get_mem_attribute(), instead of always with PAGE_KERNEL (eg. cached attribute).

---
 drivers/acpi/apei/ghes.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/acpi/apei/ghes.c +4 -2
index e82d0976a5d0..91b1f6d93079 100644
@@ -164,8 +164,10 @@ static void __iomem *ghes_ioremap_pfn_irq(u64 pfn)
 	unsigned long vaddr;
 
 	vaddr = (unsigned long)GHES_IOREMAP_IRQ_PAGE(ghes_ioremap_area->addr);
-	ioremap_page_range(vaddr, vaddr + PAGE_SIZE,
-			   pfn << PAGE_SHIFT, PAGE_KERNEL);
+	ioremap_page_range(vaddr,
+			   vaddr + PAGE_SIZE,
+			   pfn << PAGE_SHIFT,
+			   arch_apei_get_mem_attribute(pfn << PAGE_SHIFT));
 
 	return (void __iomem *)vaddr;
 }
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project