Skip to contentSkip to conversation

x86/kexec: Add EFI config table identity mapping for kexec kernel

PATCH Superseded Tao Liu 2 participants 4 messages May 25, 2023 – May 26, 2023

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Tao Liu

author
#0 lore raw

PATCH 1/1 x86/kexec: Add EFI config table identity mapping for kexec kernel

A kexec kernel bootup hang is observed on Intel Atom cpu due to unmapped EFI config table.

Currently EFI system table is identity-mapped for the kexec kernel, but EFI config table is not mapped explicitly:

commit 6bbeb276b71f ("x86/kexec: Add the EFI system tables and ACPI
tables to the ident map")

Later in the following 2 commits, EFI config table will be accessed when enabling sev at kernel startup. This may result in a page fault due to EFI config table's unmapped address. Since the page fault occurs at an early stage, it is unrecoverable and kernel hangs.

commit ec1c66af3a30 ("x86/compressed/64: Detect/setup SEV/SME features
earlier during boot")
commit c01fce9cef84 ("x86/compressed: Add SEV-SNP feature
detection/setup")

In addition, the issue doesn't appear on all systems, because the kexec kernel uses Page Size Extension (PSE) for identity mapping. In most cases, EFI config table can end up to be mapped into due to 1 GB page size. However if nogbpages is set, or cpu doesn't support pdpe1gb feature (e.g Intel Atom x6425RE cpu), EFI config table may not be mapped into due to 2 MB page size, thus a page fault hang is more likely to happen.

In this patch, we will make sure the EFI config table is always mapped.

---
 arch/x86/kernel/machine_kexec_64.c | 35 ++++++++++++++++++++++++++----
 1 file changed, 31 insertions(+), 4 deletions(-)
arch/x86/kernel/machine_kexec_64.c +31 -4
index 1a3e2c05a8a5..755aa12f583f 100644
@@ -28,6 +28,7 @@
 #include <asm/setup.h>
 #include <asm/set_memory.h>
 #include <asm/cpu.h>
+#include <asm/efi.h>
 
 #ifdef CONFIG_ACPI
 /*
@@ -86,10 +87,12 @@ const struct kexec_file_ops * const kexec_file_loaders[] = {
 #endif
 
 static int
-map_efi_systab(struct x86_mapping_info *info, pgd_t *level4p)
+map_efi_sys_cfg_tab(struct x86_mapping_info *info, pgd_t *level4p)
 {
 #ifdef CONFIG_EFI
 	unsigned long mstart, mend;
+	void *kaddr;
+	int ret;
 
 	if (!efi_enabled(EFI_BOOT))
 		return 0;
@@ -105,6 +108,30 @@ map_efi_systab(struct x86_mapping_info *info, pgd_t *level4p)
 	if (!mstart)
 		return 0;
 
+	ret = kernel_ident_mapping_init(info, level4p, mstart, mend);
+	if (ret)
+		return ret;
+
+	kaddr = memremap(mstart, mend - mstart, MEMREMAP_WB);
+	if (!kaddr) {
+		pr_err("Could not map UEFI system table\n");
+		return -ENOMEM;
+	}
+
+	mstart = efi_config_table;
+
+	if (efi_enabled(EFI_64BIT)) {
+		efi_system_table_64_t *stbl = (efi_system_table_64_t *)kaddr;
+
+		mend = mstart + sizeof(efi_config_table_64_t) * stbl->nr_tables;
+	} else {
+		efi_system_table_32_t *stbl = (efi_system_table_32_t *)kaddr;
+
+		mend = mstart + sizeof(efi_config_table_32_t) * stbl->nr_tables;
+	}
+
+	memunmap(kaddr);
+
 	return kernel_ident_mapping_init(info, level4p, mstart, mend);
 #endif
 	return 0;
@@ -244,10 +271,10 @@ static int init_pgtable(struct kimage *image, unsigned long start_pgtable)
 	}
 
 	/*
-	 * Prepare EFI systab and ACPI tables for kexec kernel since they are
-	 * not covered by pfn_mapped.
+	 * Prepare EFI systab, config table and ACPI tables for kexec kernel
+	 * since they are not covered by pfn_mapped.
 	 */
-	result = map_efi_systab(&info, level4p);
+	result = map_efi_sys_cfg_tab(&info, level4p);
 	if (result)
 		return result;
 
Signature · 1 line
2.33.1
#1 lore raw

Hi Tao,

Quoted 19 lines · Tao Liu

A kexec kernel bootup hang is observed on Intel Atom cpu due to unmapped EFI config table.

Currently EFI system table is identity-mapped for the kexec kernel, but EFI config table is not mapped explicitly:

commit 6bbeb276b71f ("x86/kexec: Add the EFI system tables and ACPI
tables to the ident map")

Later in the following 2 commits, EFI config table will be accessed when enabling sev at kernel startup. This may result in a page fault due to EFI config table's unmapped address. Since the page fault occurs at an early stage, it is unrecoverable and kernel hangs.

commit ec1c66af3a30 ("x86/compressed/64: Detect/setup SEV/SME features
earlier during boot")
commit c01fce9cef84 ("x86/compressed: Add SEV-SNP feature
detection/setup")

Quoted 8 lines

In addition, the issue doesn't appear on all systems, because the kexec kernel uses Page Size Extension (PSE) for identity mapping. In most cases, EFI config table can end up to be mapped into due to 1 GB page size. However if nogbpages is set, or cpu doesn't support pdpe1gb feature (e.g Intel Atom x6425RE cpu), EFI config table may not be mapped into due to 2 MB page size, thus a page fault hang is more likely to happen.

In this patch, we will make sure the EFI config table is always mapped.

Nice work. While you may need to rephrase above sentence, x86 maintainers don't like log with the 'this patch,' or 'we'. Please refer to 'Changelog' part of Documentation/process/maintainer-tip.rst and improve it.

Quoted 19 lines
---
 arch/x86/kernel/machine_kexec_64.c | 35 ++++++++++++++++++++++++++----
 1 file changed, 31 insertions(+), 4 deletions(-)
arch/x86/kernel/machine_kexec_64.c +1 -0
index 1a3e2c05a8a5..755aa12f583f 100644
@@ -28,6 +28,7 @@
 #include <asm/setup.h>
 #include <asm/set_memory.h>
 #include <asm/cpu.h>
+#include <asm/efi.h>
 
 #ifdef CONFIG_ACPI
 /*

arch/x86/kernel/machine_kexec_64.c const struct kexec_file_ops * const kexec_file_loaders[] = { · line 90

Quoted 5 lines
arch/x86/kernel/machine_kexec_64.c +1 -1
@@ -86,10 +87,12 @@ const struct kexec_file_ops * const kexec_file_loaders[] = {
 #endif
 
 static int
-map_efi_systab(struct x86_mapping_info *info, pgd_t *level4p)
+map_efi_sys_cfg_tab(struct x86_mapping_info *info, pgd_t *level4p)

Can we call the function map_efi_tables() since we will map efi system table, system config table. If you need add another table mapping here, what would you call it, map_efi_sys_cfg_xxx_tab()?

Anyway, not very strong opinion as long as x86 maintainer likes it.

Quoted 56 lines
arch/x86/kernel/machine_kexec_64.c +29 -3
 {
 #ifdef CONFIG_EFI
 	unsigned long mstart, mend;
+	void *kaddr;
+	int ret;
 
 	if (!efi_enabled(EFI_BOOT))
 		return 0;
@@ -105,6 +108,30 @@ map_efi_systab(struct x86_mapping_info *info, pgd_t *level4p)
 	if (!mstart)
 		return 0;
 
+	ret = kernel_ident_mapping_init(info, level4p, mstart, mend);
+	if (ret)
+		return ret;
+
+	kaddr = memremap(mstart, mend - mstart, MEMREMAP_WB);
+	if (!kaddr) {
+		pr_err("Could not map UEFI system table\n");
+		return -ENOMEM;
+	}
+
+	mstart = efi_config_table;
+
+	if (efi_enabled(EFI_64BIT)) {
+		efi_system_table_64_t *stbl = (efi_system_table_64_t *)kaddr;
+
+		mend = mstart + sizeof(efi_config_table_64_t) * stbl->nr_tables;
+	} else {
+		efi_system_table_32_t *stbl = (efi_system_table_32_t *)kaddr;
+
+		mend = mstart + sizeof(efi_config_table_32_t) * stbl->nr_tables;
+	}
+
+	memunmap(kaddr);
+
 	return kernel_ident_mapping_init(info, level4p, mstart, mend);
 #endif
 	return 0;
@@ -244,10 +271,10 @@ static int init_pgtable(struct kimage *image, unsigned long start_pgtable)
 	}
 
 	/*
-	 * Prepare EFI systab and ACPI tables for kexec kernel since they are
-	 * not covered by pfn_mapped.
+	 * Prepare EFI systab, config table and ACPI tables for kexec kernel
+	 * since they are not covered by pfn_mapped.
 	 */
-	result = map_efi_systab(&info, level4p);
+	result = map_efi_sys_cfg_tab(&info, level4p);
 	if (result)
 		return result;
 
Signature · 1 line
2.33.1

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

author
#2 lore raw

Hi Baoquan,

Quoted 30 lines · Baoquan He

Hi Tao,

Quoted 26 lines · Tao Liu

A kexec kernel bootup hang is observed on Intel Atom cpu due to unmapped EFI config table.

Currently EFI system table is identity-mapped for the kexec kernel, but EFI config table is not mapped explicitly:

commit 6bbeb276b71f ("x86/kexec: Add the EFI system tables and ACPI
tables to the ident map")

Later in the following 2 commits, EFI config table will be accessed when enabling sev at kernel startup. This may result in a page fault due to EFI config table's unmapped address. Since the page fault occurs at an early stage, it is unrecoverable and kernel hangs.

commit ec1c66af3a30 ("x86/compressed/64: Detect/setup SEV/SME features
earlier during boot")
commit c01fce9cef84 ("x86/compressed: Add SEV-SNP feature
detection/setup")

In addition, the issue doesn't appear on all systems, because the kexec kernel uses Page Size Extension (PSE) for identity mapping. In most cases, EFI config table can end up to be mapped into due to 1 GB page size. However if nogbpages is set, or cpu doesn't support pdpe1gb feature (e.g Intel Atom x6425RE cpu), EFI config table may not be mapped into due to 2 MB page size, thus a page fault hang is more likely to happen.

Quoted 6 lines
Quoted 1 line

In this patch, we will make sure the EFI config table is always mapped.

Nice work. While you may need to rephrase above sentence, x86 maintainers don't like log with the 'this patch,' or 'we'. Please refer to 'Changelog' part of Documentation/process/maintainer-tip.rst and improve it.

OK, Thanks for the suggestion! I will get the sentence rephrased in v2.

Quoted 22 lines
Quoted 22 lines
---
 arch/x86/kernel/machine_kexec_64.c | 35 ++++++++++++++++++++++++++----
 1 file changed, 31 insertions(+), 4 deletions(-)
arch/x86/kernel/machine_kexec_64.c +1 -0
index 1a3e2c05a8a5..755aa12f583f 100644
@@ -28,6 +28,7 @@
 #include <asm/setup.h>
 #include <asm/set_memory.h>
 #include <asm/cpu.h>
+#include <asm/efi.h>
 
 #ifdef CONFIG_ACPI
 /*
@@ -86,10 +87,12 @@ const struct kexec_file_ops * const kexec_file_loaders[] = {
 #endif
 
Quoted 8 lines

arch/x86/kernel/machine_kexec_64.c const struct kexec_file_ops * const kexec_file_loaders[] = { · line 90

Quoted 3 lines
arch/x86/kernel/machine_kexec_64.c +1 -1
 static int
-map_efi_systab(struct x86_mapping_info *info, pgd_t *level4p)
+map_efi_sys_cfg_tab(struct x86_mapping_info *info, pgd_t *level4p)

Can we call the function map_efi_tables() since we will map efi system table, system config table. If you need add another table mapping here, what would you call it, map_efi_sys_cfg_xxx_tab()?

Yeah, map_efi_sys_cfg_xxx_tab() is surely a bad name. I agree with the map_efi_tables() name.

Thanks,
Tao Liu

Quoted 55 lines

Anyway, not very strong opinion as long as x86 maintainer likes it.

Quoted 53 lines
arch/x86/kernel/machine_kexec_64.c +29 -3
 {
 #ifdef CONFIG_EFI
      unsigned long mstart, mend;
+     void *kaddr;
+     int ret;
 
      if (!efi_enabled(EFI_BOOT))
              return 0;
@@ -105,6 +108,30 @@ map_efi_systab(struct x86_mapping_info *info, pgd_t *level4p)
      if (!mstart)
              return 0;
 
+     ret = kernel_ident_mapping_init(info, level4p, mstart, mend);
+     if (ret)
+             return ret;
+
+     kaddr = memremap(mstart, mend - mstart, MEMREMAP_WB);
+     if (!kaddr) {
+             pr_err("Could not map UEFI system table\n");
+             return -ENOMEM;
+     }
+
+     mstart = efi_config_table;
+
+     if (efi_enabled(EFI_64BIT)) {
+             efi_system_table_64_t *stbl = (efi_system_table_64_t *)kaddr;
+
+             mend = mstart + sizeof(efi_config_table_64_t) * stbl->nr_tables;
+     } else {
+             efi_system_table_32_t *stbl = (efi_system_table_32_t *)kaddr;
+
+             mend = mstart + sizeof(efi_config_table_32_t) * stbl->nr_tables;
+     }
+
+     memunmap(kaddr);
+
      return kernel_ident_mapping_init(info, level4p, mstart, mend);
 #endif
      return 0;
@@ -244,10 +271,10 @@ static int init_pgtable(struct kimage *image, unsigned long start_pgtable)
      }
 
      /*
-      * Prepare EFI systab and ACPI tables for kexec kernel since they are
-      * not covered by pfn_mapped.
+      * Prepare EFI systab, config table and ACPI tables for kexec kernel
+      * since they are not covered by pfn_mapped.
       */
-     result = map_efi_systab(&info, level4p);
+     result = map_efi_sys_cfg_tab(&info, level4p);
      if (result)
              return result;
 
Quoted 4 lines
Quoted 3 lines

--
2.33.1

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#3 lore raw
In reply to Tao Liu

Add Ard to CC.

Quoted 107 lines · Tao Liu

A kexec kernel bootup hang is observed on Intel Atom cpu due to unmapped EFI config table.

Currently EFI system table is identity-mapped for the kexec kernel, but EFI config table is not mapped explicitly:

commit 6bbeb276b71f ("x86/kexec: Add the EFI system tables and ACPI
tables to the ident map")

Later in the following 2 commits, EFI config table will be accessed when enabling sev at kernel startup. This may result in a page fault due to EFI config table's unmapped address. Since the page fault occurs at an early stage, it is unrecoverable and kernel hangs.

commit ec1c66af3a30 ("x86/compressed/64: Detect/setup SEV/SME features
earlier during boot")
commit c01fce9cef84 ("x86/compressed: Add SEV-SNP feature
detection/setup")

In addition, the issue doesn't appear on all systems, because the kexec kernel uses Page Size Extension (PSE) for identity mapping. In most cases, EFI config table can end up to be mapped into due to 1 GB page size. However if nogbpages is set, or cpu doesn't support pdpe1gb feature (e.g Intel Atom x6425RE cpu), EFI config table may not be mapped into due to 2 MB page size, thus a page fault hang is more likely to happen.

In this patch, we will make sure the EFI config table is always mapped.

---
 arch/x86/kernel/machine_kexec_64.c | 35 ++++++++++++++++++++++++++----
 1 file changed, 31 insertions(+), 4 deletions(-)
arch/x86/kernel/machine_kexec_64.c +31 -4
index 1a3e2c05a8a5..755aa12f583f 100644
@@ -28,6 +28,7 @@
 #include <asm/setup.h>
 #include <asm/set_memory.h>
 #include <asm/cpu.h>
+#include <asm/efi.h>
 
 #ifdef CONFIG_ACPI
 /*
@@ -86,10 +87,12 @@ const struct kexec_file_ops * const kexec_file_loaders[] = {
 #endif
 
 static int
-map_efi_systab(struct x86_mapping_info *info, pgd_t *level4p)
+map_efi_sys_cfg_tab(struct x86_mapping_info *info, pgd_t *level4p)
 {
 #ifdef CONFIG_EFI
 	unsigned long mstart, mend;
+	void *kaddr;
+	int ret;
 
 	if (!efi_enabled(EFI_BOOT))
 		return 0;
@@ -105,6 +108,30 @@ map_efi_systab(struct x86_mapping_info *info, pgd_t *level4p)
 	if (!mstart)
 		return 0;
 
+	ret = kernel_ident_mapping_init(info, level4p, mstart, mend);
+	if (ret)
+		return ret;
+
+	kaddr = memremap(mstart, mend - mstart, MEMREMAP_WB);
+	if (!kaddr) {
+		pr_err("Could not map UEFI system table\n");
+		return -ENOMEM;
+	}
+
+	mstart = efi_config_table;
+
+	if (efi_enabled(EFI_64BIT)) {
+		efi_system_table_64_t *stbl = (efi_system_table_64_t *)kaddr;
+
+		mend = mstart + sizeof(efi_config_table_64_t) * stbl->nr_tables;
+	} else {
+		efi_system_table_32_t *stbl = (efi_system_table_32_t *)kaddr;
+
+		mend = mstart + sizeof(efi_config_table_32_t) * stbl->nr_tables;
+	}
+
+	memunmap(kaddr);
+
 	return kernel_ident_mapping_init(info, level4p, mstart, mend);
 #endif
 	return 0;
@@ -244,10 +271,10 @@ static int init_pgtable(struct kimage *image, unsigned long start_pgtable)
 	}
 
 	/*
-	 * Prepare EFI systab and ACPI tables for kexec kernel since they are
-	 * not covered by pfn_mapped.
+	 * Prepare EFI systab, config table and ACPI tables for kexec kernel
+	 * since they are not covered by pfn_mapped.
 	 */
-	result = map_efi_systab(&info, level4p);
+	result = map_efi_sys_cfg_tab(&info, level4p);
 	if (result)
 		return result;
 
Signature · 1 line
2.33.1

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec