Skip to contentSkip to conversation

kexec kernel efi runtime support

PATCH Superseded Dave Young 5 participants 73 messages Nov 26, 2013 – Dec 9, 2013

Subsystems (6)
Documentation x86 x86 MM EFI +2 more subsystems

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Dave Young

author
#0 lore raw

v4 cover kexec kernel efi runtime support

Hi,

Here is the V4 resend for supporting kexec kernel efi runtime. Per pervious discussion I pass the 1st kernel efi runtime mapping via setup_data to 2nd kernel. Besides of the runtime mapping info I also pass the fw_vendor, runtime, config table, smbios physical address in setup_data. EFI spec mentioned fw_vendor, runtime, config table addresses will be converted to virt address after entering virtual mode, but we will use it as physical address in efi_init. For smbios EFI spec did not mention about the address updating, but during my test on a HP workstation, the bios will convert it to Virt addr, thus pass it in setup_data as well.

For fw_vendor, runtime, config table, I export them in /sys/firmware/ efi/, smbios is already in /sys/firmware/efi/systab.

For efi runtime mapping I add a new directory /sys/firmware/efi/ runtime-map/ like below
[dave@darkstar ~]$ tree /sys/firmware/efi/runtime-map/ /sys/firmware/efi/runtime-map/

├── 0
│   ├── attribute
│   ├── num_pages
│   ├── phys_addr
│   ├── type
│   └── virt_addr
├── 1
│   ├── attribute
│   ├── num_pages
│   ├── phys_addr
│   ├── type
│   └── virt_addr

[snip]

kexec-tools will assemble them as setup_data and pass to 2nd kernel. I will send userspace patches as well.

Limitation is I only write support for x86_64, test on below machines:
Lenovo thinkpad t420
Dell inspiron 14 - 3421
HP Z420 workstation
Qemu + OVMF

Please help to review the patches.

The patches are based on matt's efi master tree

Changes from v1:
add one flag in xloadflags, so kexec-tools can safely load old kernel without efi support.
coding style fixes
function name for map phys_addr to fixed virt_addr
Add ABI documentation for sysfs files

Changes from v2:
01/09: a new patch to remove unused variables in __map_region function
catched by Toshi Kani
09/09: a new patch to export x86 boot_params to sysfs instead of use
debugfs files
Matt: reuse __map_region instead do same thing in another function.
add a wrapper function efi_map_region_fixed [02/09] check return value of krealloc
sysfs dir name s/efi-runtime-map/runtime-map [06/09] use desc_size in efi_runtime_map
for the xloadflags defination: +&& defined(CONFIG_KEXEC)
Greg: sysfs : one file one value for fw_vendor, runtime, tables. [05/09]
Document them in ABI testing
HPA: Document the new xloadflag
Also there's other function cleanup and improvement for error handling.

Changes from v3:
Greg: sysfs code move to use __ATTR_RO and attr_group
Boris: comments and code alignment

Added 3 new patches below
10-print-efi-runtime-memmap.patch
- 10/12: print only runtime ranges in case EFI_DEBUG printing
11-reserve-setup-data-late.patch
- fix a bug of kdump kernel, move function for reserving setup data ranges late after parsing memmap= cmdline params because kdump kernel will pass exact memmap late.
12-x86-kdebugfs-use-ioremap.patch
- fix a bug of x86/kernel/kdebugfs.c, use ioremap instead of __va for low mem because __va does not work for exact memmap=

Dave Young (12):
efi: remove unused variables in __map_region
efi: add a wrapper function efi_map_region_fixed
efi: reserve boot service fix
efi: cleanup efi_enter_virtual_mode function
efi: export more efi table variable to sysfs
efi: export efi runtime memory mapping to sysfs
efi: passing kexec necessary efi data via setup_data
efi: only print saved efi runtime maps instead of all memmap ranges
for kexec
x86: add xloadflags bit for efi runtime support on kexec
x86: export x86 boot_params to sysfs
x86: reserve setup_data ranges late after parsing memmap cmdline
x86: kdebugfs do not use __va for getting setup_data virt addr

 Documentation/ABI/testing/sysfs-firmware-efi       |  26 ++
 .../ABI/testing/sysfs-firmware-efi-runtime-map     |  45 +++
 Documentation/ABI/testing/sysfs-kernel-boot_params |  40 +++
 Documentation/x86/boot.txt                         |   3 +
 arch/x86/boot/header.S                             |   9 +-
 arch/x86/include/asm/efi.h                         |  13 +
 arch/x86/include/uapi/asm/bootparam.h              |   2 +
 arch/x86/kernel/Makefile                           |   2 +-
 arch/x86/kernel/kdebugfs.c                         |  35 +--
 arch/x86/kernel/ksysfs.c                           | 339 +++++++++++++++++++++
 arch/x86/kernel/setup.c                            |   7 +-
 arch/x86/platform/efi/efi.c                        | 313 +++++++++++++++----
 arch/x86/platform/efi/efi_32.c                     |   2 +
 arch/x86/platform/efi/efi_64.c                     |  16 +-
 drivers/firmware/efi/Kconfig                       |  10 +
 drivers/firmware/efi/Makefile                      |   1 +
 drivers/firmware/efi/efi.c                         |  47 ++-
 drivers/firmware/efi/runtime-map.c                 | 199 ++++++++++++
 include/linux/efi.h                                |   9 +
 19 files changed, 1028 insertions(+), 90 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-firmware-efi
 create mode 100644 Documentation/ABI/testing/sysfs-firmware-efi-runtime-map
 create mode 100644 Documentation/ABI/testing/sysfs-kernel-boot_params
 create mode 100644 arch/x86/kernel/ksysfs.c
 create mode 100644 drivers/firmware/efi/runtime-map.c
Signature · 7 lines
1.8.3.1


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
author
#1 lore raw

PATCH v4 1/12 efi: remove unused variables in __map_region

variables size and end is useless in this function, thus remove them.

---
 arch/x86/platform/efi/efi_64.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
arch/x86/platform/efi/efi_64.c +1 -5
index bf286c3..c5a6491 100644
@@ -148,15 +148,11 @@ void efi_setup_page_tables(void)
 static void __init __map_region(efi_memory_desc_t *md, u64 va)
 {
 	pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd);
-	unsigned long pf = 0, size;
-	u64 end;
+	unsigned long pf = 0;
 
 	if (!(md->attribute & EFI_MEMORY_WB))
 		pf |= _PAGE_PCD;
 
-	size = md->num_pages << PAGE_SHIFT;
-	end  = va + size;
-
 	if (kernel_map_pages_in_pgd(pgd, md->phys_addr, va, md->num_pages, pf))
 		pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
 			   md->phys_addr, va);
Signature · 1 line
1.8.3.1
author
#2 lore raw

PATCH v4 2/12 efi: add a wrapper function efi_map_region_fixed

In reply to Dave Young

Kexec kernel will use saved runtime virtual mapping, so add a new function efi_map_region_fixed for directly mapping a md to md->virt.

The md is passed in from 1st kernel, the virtual addr is saved in md->virt_addr.

Matt: coding style
reuse __map_region
Boris: Strenthen comment lines to 80 cols.

---
 arch/x86/include/asm/efi.h     |  1 +
 arch/x86/platform/efi/efi_32.c |  2 ++
 arch/x86/platform/efi/efi_64.c | 10 ++++++++++
 3 files changed, 13 insertions(+)
arch/x86/include/asm/efi.h +1 -0
index 89a05b0..9fbaeb2 100644
@@ -128,6 +128,7 @@ extern void efi_call_phys_epilog(void);
 extern void efi_unmap_memmap(void);
 extern void efi_memory_uc(u64 addr, unsigned long size);
 extern void __init efi_map_region(efi_memory_desc_t *md);
+extern void __init efi_map_region_fixed(efi_memory_desc_t *md);
 extern void efi_sync_low_kernel_mappings(void);
 extern void efi_setup_page_tables(void);
 extern void __init old_map_region(efi_memory_desc_t *md);
arch/x86/platform/efi/efi_32.c +2 -0
index e94557c..7b3ec6e 100644
@@ -47,6 +47,8 @@ void __init efi_map_region(efi_memory_desc_t *md)
 	old_map_region(md);
 }
 
+void __init efi_map_region_fixed(efi_memory_desc_t *md) {}
+
 void efi_call_phys_prelog(void)
 {
 	struct desc_ptr gdt_descr;
arch/x86/platform/efi/efi_64.c +10 -0
index c5a6491..ff08cb1 100644
@@ -199,6 +199,16 @@ void __init efi_map_region(efi_memory_desc_t *md)
 	md->virt_addr = efi_va;
 }
 
+/*
+ * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
+ * md->virt_addr is the original virtual address which had been mapped in kexec
+ * 1st kernel.
+ */
+void __init efi_map_region_fixed(efi_memory_desc_t *md)
+{
+	__map_region(md, md->virt_addr);
+}
+
 void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
 				 u32 type, u64 attribute)
 {
Signature · 1 line
1.8.3.1
author
#3 lore raw

PATCH v4 3/12 efi: reserve boot service fix

In reply to Dave Young

Current code check boot service region with kernel text region by:
start+size >= __pa_symbol(_text)
The end of the above region should be start + size - 1 instead.

I see this problem in ovmf + Fedora 19 grub boot:
text start: 1000000 md start: 800000 md size: 800000

---
 arch/x86/platform/efi/efi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/platform/efi/efi.c +1 -1
index f8ec4da..15e3b5e 100644
@@ -438,7 +438,7 @@ void __init efi_reserve_boot_services(void)
 		 * - Not within any part of the kernel
 		 * - Not the bios reserved area
 		*/
-		if ((start+size >= __pa_symbol(_text)
+		if ((start + size > __pa_symbol(_text)
 				&& start <= __pa_symbol(_end)) ||
 			!e820_all_mapped(start, start+size, E820_RAM) ||
 			memblock_is_region_reserved(start, size)) {
Signature · 1 line
1.8.3.1
author
#4 lore raw

PATCH v4 4/12 efi: cleanup efi_enter_virtual_mode function

In reply to Dave Young

Add two small functions:
efi_merge_regions and efi_map_regions, efi_enter_virtual_mode calls them instead of embedding two long for loop.

v1->v2:
refresh; coding style fixes.

v2->v3:
Toshi Kani:
remove unused variable
Matt: check return value of krealloc.
v3->v4:
Boris: Stretch comment to 80 cols

---
 arch/x86/platform/efi/efi.c | 109 +++++++++++++++++++++++++++-----------------
 1 file changed, 66 insertions(+), 43 deletions(-)
arch/x86/platform/efi/efi.c +66 -43
index 15e3b5e..480529a 100644
@@ -773,44 +773,12 @@ void __init old_map_region(efi_memory_desc_t *md)
 		       (unsigned long long)md->phys_addr);
 }
 
-/*
- * This function will switch the EFI runtime services to virtual mode.
- * Essentially, we look through the EFI memmap and map every region that
- * has the runtime attribute bit set in its memory descriptor into the
- * ->trampoline_pgd page table using a top-down VA allocation scheme.
- *
- * The old method which used to update that memory descriptor with the
- * virtual address obtained from ioremap() is still supported when the
- * kernel is booted with efi=old_map on its command line. Same old
- * method enabled the runtime services to be called without having to
- * thunk back into physical mode for every invocation.
- *
- * The new method does a pagetable switch in a preemption-safe manner
- * so that we're in a different address space when calling a runtime
- * function. For function arguments passing we do copy the PGDs of the
- * kernel page table into ->trampoline_pgd prior to each call.
- */
-void __init efi_enter_virtual_mode(void)
+/* Merge contiguous regions of the same type and attribute */
+static void __init efi_merge_regions(void)
 {
+	void *p;
 	efi_memory_desc_t *md, *prev_md = NULL;
-	void *p, *new_memmap = NULL;
-	unsigned long size;
-	efi_status_t status;
-	u64 end, systab;
-	int count = 0;
 
-	efi.systab = NULL;
-
-	/*
-	 * We don't do virtual mode, since we don't do runtime services, on
-	 * non-native EFI
-	 */
-	if (!efi_is_native()) {
-		efi_unmap_memmap();
-		return;
-	}
-
-	/* Merge contiguous regions of the same type and attribute */
 	for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
 		u64 prev_size;
 		md = p;
@@ -837,6 +805,18 @@ void __init efi_enter_virtual_mode(void)
 		prev_md = md;
 
 	}
+}
+
+/*
+ * Map efi memory ranges for runtime serivce and update new_memmap with virtual
+ * addresses.
+ */
+static void * __init efi_map_regions(int *count)
+{
+	efi_memory_desc_t *md;
+	void *p, *new_memmap = NULL;
+	unsigned long size;
+	u64 end, systab;
 
 	for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
 		md = p;
@@ -861,14 +841,60 @@ void __init efi_enter_virtual_mode(void)
 		}
 
 		new_memmap = krealloc(new_memmap,
-				      (count + 1) * memmap.desc_size,
-				      GFP_KERNEL);
+					(*count + 1) * memmap.desc_size,
+					GFP_KERNEL);
 		if (!new_memmap)
-			goto err_out;
+			goto ret;
 
-		memcpy(new_memmap + (count * memmap.desc_size), md,
+		memcpy(new_memmap + (*count * memmap.desc_size), md,
 		       memmap.desc_size);
-		count++;
+		(*count)++;
+	}
+
+ret:
+	return new_memmap;
+}
+
+/*
+ * This function will switch the EFI runtime services to virtual mode.
+ * Essentially, we look through the EFI memmap and map every region that
+ * has the runtime attribute bit set in its memory descriptor into the
+ * ->trampoline_pgd page table using a top-down VA allocation scheme.
+ *
+ * The old method which used to update that memory descriptor with the
+ * virtual address obtained from ioremap() is still supported when the
+ * kernel is booted with efi=old_map on its command line. Same old
+ * method enabled the runtime services to be called without having to
+ * thunk back into physical mode for every invocation.
+ *
+ * The new method does a pagetable switch in a preemption-safe manner
+ * so that we're in a different address space when calling a runtime
+ * function. For function arguments passing we do copy the PGDs of the
+ * kernel page table into ->trampoline_pgd prior to each call.
+ */
+void __init efi_enter_virtual_mode(void)
+{
+	efi_status_t status;
+	void *new_memmap = NULL;
+	int count = 0;
+
+	efi.systab = NULL;
+
+	/*
+	 * We don't do virtual mode, since we don't do runtime services, on
+	 * non-native EFI
+	 */
+	if (!efi_is_native()) {
+		efi_unmap_memmap();
+		return;
+	}
+
+	efi_merge_regions();
+
+	new_memmap = efi_map_regions(&count);
+	if (!new_memmap) {
+		pr_err("Error reallocating memory, EFI runtime non-functional!\n");
+		return;
 	}
 
 	BUG_ON(!efi.systab);
@@ -922,9 +948,6 @@ void __init efi_enter_virtual_mode(void)
 			 0, NULL);
 
 	return;
-
- err_out:
-	pr_err("Error reallocating memory, EFI runtime non-functional!\n");
 }
 
 /*
Signature · 1 line
1.8.3.1
#5 lore raw

Re: [PATCH v4 04/12] efi: cleanup efi_enter_virtual_mode function

Quoted 19 lines · Dave Young

Add two small functions:
efi_merge_regions and efi_map_regions, efi_enter_virtual_mode calls them instead of embedding two long for loop.

v1->v2:
refresh; coding style fixes.

v2->v3:
Toshi Kani:
remove unused variable
Matt: check return value of krealloc.
v3->v4:
Boris: Stretch comment to 80 cols

---
 arch/x86/platform/efi/efi.c | 109 +++++++++++++++++++++++++++-----------------
 1 file changed, 66 insertions(+), 43 deletions(-)

[...]

arch/x86/platform/efi/efi.c void __init efi_enter_virtual_mode(void) · line 953

Quoted 10 lines
arch/x86/platform/efi/efi.c +0 -3
@@ -922,9 +948,6 @@ void __init efi_enter_virtual_mode(void)
 			 0, NULL);
 
 	return;
-
- err_out:
-	pr_err("Error reallocating memory, EFI runtime non-functional!\n");
 }
 
 /*

Teeny, tiny comment: You could now delete the above return statement.

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

PATCH v4 5/12 efi: export more efi table variable to sysfs

In reply to Dave Young

Export fw_vendor, runtime and config tables physical addresses to /sys/firmware/efi/systab becaue kexec kernel will need them.

Quoted 1 line

From EFI spec these 3 variables will be updated to

virtual address after entering virtual mode. But kernel startup code will need the physical address.

changelog:
Greg: add standalone sysfs files instead of add lines to systab Document them as testing ABI
Greg: use group attrs and is_visible
Boris: align comments lines

---
 Documentation/ABI/testing/sysfs-firmware-efi | 26 ++++++++++++++++
 arch/x86/platform/efi/efi.c                  |  4 +++
 drivers/firmware/efi/efi.c                   | 44 ++++++++++++++++++++++++++++
 include/linux/efi.h                          |  3 ++
 4 files changed, 77 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-firmware-efi
Documentation/ABI/testing/sysfs-firmware-efi new file +26 -0
new file mode 100644
index 0000000..9252a24
@@ -0,0 +1,26 @@
+What:		/sys/firmware/efi/fw_vendor
+Date:		Nov 2013
+Contact:	Dave Young <[email protected]>
+Description:
+		Shows the physical address of firmware vendor in the EFI
+		system table.
+Users:
+		Kexec Mailing List <[email protected]>
+
+What:		/sys/firmware/efi/runtime
+Date:		Nov 2013
+Contact:	Dave Young <[email protected]>
+Description:
+		Shows the physical address of runtime service table in the
+		EFI system table.
+Users:
+		Kexec Mailing List <[email protected]>
+
+What:		/sys/firmware/efi/config_table
+Date:		Nov 2013
+Contact:	Dave Young <[email protected]>
+Description:
+		Shows the physical address of config table in the EFI
+		system table.
+Users:
+		Kexec Mailing List <[email protected]>
arch/x86/platform/efi/efi.c +4 -0
index 480529a..2a292aa 100644
@@ -653,6 +653,10 @@ void __init efi_init(void)
 
 	set_bit(EFI_SYSTEM_TABLES, &x86_efi_facility);
 
+	efi.config_table = (unsigned long)efi.systab->tables;
+	efi.fw_vendor	 = (unsigned long)efi.systab->fw_vendor;
+	efi.runtime	 = (unsigned long)efi.systab->runtime;
+
 	/*
 	 * Show what we know for posterity
 	 */
drivers/firmware/efi/efi.c +44 -0

63 lines not on this page · raw message

include/linux/efi.h +3 -0
index 6c0ca52..fb60b10 100644
@@ -556,6 +556,9 @@ extern struct efi {
 	unsigned long hcdp;		/* HCDP table */
 	unsigned long uga;		/* UGA table */
 	unsigned long uv_systab;	/* UV system table */
+	unsigned long fw_vendor;	/* fw_vendor */
+	unsigned long runtime;		/* runtime table */
+	unsigned long config_table;	/* config tables */
 	efi_get_time_t *get_time;
 	efi_set_time_t *set_time;
 	efi_get_wakeup_time_t *get_wakeup_time;
Signature · 1 line
1.8.3.1
maintainer
#7 lore raw

Re: [PATCH v4 05/12] efi: export more efi table variable to sysfs

drivers/firmware/efi/efi.c struct efi __read_mostly efi = { · line 37

Quoted 11 lines · Dave Young
drivers/firmware/efi/efi.c +3 -0
index 2e2fbde..5d85956 100644
@@ -32,6 +32,9 @@ struct efi __read_mostly efi = {
 	.hcdp       = EFI_INVALID_TABLE_ADDR,
 	.uga        = EFI_INVALID_TABLE_ADDR,
 	.uv_systab  = EFI_INVALID_TABLE_ADDR,
+	.fw_vendor  = EFI_INVALID_TABLE_ADDR,
+	.runtime    = EFI_INVALID_TABLE_ADDR,
+	.config_table  = EFI_INVALID_TABLE_ADDR,

See what I mean with alignment?

Now this config_table thing sticks out and completely destroys the balance in the universe.

:-)

Quoted 14 lines
drivers/firmware/efi/efi.c +7 -0
 };
 EXPORT_SYMBOL(efi);
 
@@ -71,13 +74,54 @@ static ssize_t systab_show(struct kobject *kobj,
 static struct kobj_attribute efi_attr_systab =
 			__ATTR(systab, 0400, systab_show, NULL);
 
+static ssize_t fw_vendor_show(struct kobject *kobj,
+				struct kobj_attribute *attr, char *buf)
+{
+	return sprintf(buf, "0x%lx\n", efi.fw_vendor);
+}
+
+static ssize_t runtime_show(struct kobject *kobj,

drivers/firmware/efi/efi.c static ssize_t systab_show(struct kobject *kobj, · line 93

Quoted 10 lines
drivers/firmware/efi/efi.c +10 -0
+				struct kobj_attribute *attr, char *buf)
+{
+	return sprintf(buf, "0x%lx\n", efi.runtime);
+}
+
+static ssize_t config_table_show(struct kobject *kobj,
+				struct kobj_attribute *attr, char *buf)
+{
+	return sprintf(buf, "0x%lx\n", efi.config_table);
+}

Those are sooo similar, it's like the macro SHOW writes itself.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
author
#8 lore raw

Re: [PATCH v4 05/12] efi: export more efi table variable to sysfs

Quoted 14 lines · Borislav Petkov

drivers/firmware/efi/efi.c struct efi __read_mostly efi = { · line 37

Quoted 11 lines · Dave Young
drivers/firmware/efi/efi.c +3 -0
index 2e2fbde..5d85956 100644
@@ -32,6 +32,9 @@ struct efi __read_mostly efi = {
 	.hcdp       = EFI_INVALID_TABLE_ADDR,
 	.uga        = EFI_INVALID_TABLE_ADDR,
 	.uv_systab  = EFI_INVALID_TABLE_ADDR,
+	.fw_vendor  = EFI_INVALID_TABLE_ADDR,
+	.runtime    = EFI_INVALID_TABLE_ADDR,
+	.config_table  = EFI_INVALID_TABLE_ADDR,

See what I mean with alignment?

Yes, I see. As for this segment I would not like change the previous code just because of this reason

Quoted 24 lines

Now this config_table thing sticks out and completely destroys the balance in the universe.

:-)

Quoted 18 lines
drivers/firmware/efi/efi.c +11 -0
 };
 EXPORT_SYMBOL(efi);
 
@@ -71,13 +74,54 @@ static ssize_t systab_show(struct kobject *kobj,
 static struct kobj_attribute efi_attr_systab =
 			__ATTR(systab, 0400, systab_show, NULL);
 
+static ssize_t fw_vendor_show(struct kobject *kobj,
+				struct kobj_attribute *attr, char *buf)
+{
+	return sprintf(buf, "0x%lx\n", efi.fw_vendor);
+}
+
+static ssize_t runtime_show(struct kobject *kobj,
+				struct kobj_attribute *attr, char *buf)
+{
+	return sprintf(buf, "0x%lx\n", efi.runtime);
+}
Quoted 8 lines

drivers/firmware/efi/efi.c static ssize_t systab_show(struct kobject *kobj, · line 93

Quoted 6 lines
drivers/firmware/efi/efi.c +6 -0
+
+static ssize_t config_table_show(struct kobject *kobj,
+				struct kobj_attribute *attr, char *buf)
+{
+	return sprintf(buf, "0x%lx\n", efi.config_table);
+}

Those are sooo similar, it's like the macro SHOW writes itself.

Ok, will add a macro for the above functions..

Thanks for review
Dave

#9 lore raw

Re: [PATCH v4 05/12] efi: export more efi table variable to sysfs

In reply to Dave Young
Quoted 3 lines · Dave Young

Export fw_vendor, runtime and config tables physical addresses to /sys/firmware/efi/systab becaue kexec kernel will need them.

This commit log needs updating.

Quoted 25 lines

From EFI spec these 3 variables will be updated to virtual address after entering virtual mode. But kernel startup code will need the physical address.

changelog:
Greg: add standalone sysfs files instead of add lines to systab Document them as testing ABI
Greg: use group attrs and is_visible
Boris: align comments lines

---
 Documentation/ABI/testing/sysfs-firmware-efi | 26 ++++++++++++++++
 arch/x86/platform/efi/efi.c                  |  4 +++
 drivers/firmware/efi/efi.c                   | 44 ++++++++++++++++++++++++++++
 include/linux/efi.h                          |  3 ++
 4 files changed, 77 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-firmware-efi
Documentation/ABI/testing/sysfs-firmware-efi new file +0 -0
new file mode 100644
index 0000000..9252a24

Documentation/ABI/testing/sysfs-firmware-efi line 3

Quoted 3 lines
Documentation/ABI/testing/sysfs-firmware-efi new file +3 -0
@@ -0,0 +1,26 @@
+What:		/sys/firmware/efi/fw_vendor
+Date:		Nov 2013
+Contact:	Dave Young <[email protected]>

redhat.org eh? Typo or legit?

Documentation/ABI/testing/sysfs-firmware-efi line 6

Quoted 3 lines
Documentation/ABI/testing/sysfs-firmware-efi new file +3 -0
+Description:
+		Shows the physical address of firmware vendor in the EFI
+		system table.

It shows the physical address of the firmware vendor "entry" or "field" in the EFI system table.

Documentation/ABI/testing/sysfs-firmware-efi line 8

Quoted 2 lines
Documentation/ABI/testing/sysfs-firmware-efi new file +2 -0
+Users:
+		Kexec Mailing List <[email protected]>

The "Kexec mailing list" isn't a user, kexec is.

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

Re: [PATCH v4 05/12] efi: export more efi table variable to sysfs

Quoted 6 lines · Matt Fleming
Quoted 3 lines · Dave Young

Export fw_vendor, runtime and config tables physical addresses to /sys/firmware/efi/systab becaue kexec kernel will need them.

This commit log needs updating.

Will update

Quoted 20 lines
Quoted 20 lines

From EFI spec these 3 variables will be updated to virtual address after entering virtual mode. But kernel startup code will need the physical address.

changelog:
Greg: add standalone sysfs files instead of add lines to systab Document them as testing ABI
Greg: use group attrs and is_visible
Boris: align comments lines

---
 Documentation/ABI/testing/sysfs-firmware-efi | 26 ++++++++++++++++
 arch/x86/platform/efi/efi.c                  |  4 +++
 drivers/firmware/efi/efi.c                   | 44 ++++++++++++++++++++++++++++
 include/linux/efi.h                          |  3 ++
 4 files changed, 77 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-firmware-efi
Quoted 11 lines

Documentation/ABI/testing/sysfs-firmware-efi line 3

Quoted 9 lines
Documentation/ABI/testing/sysfs-firmware-efi new file +3 -0
new file mode 100644
index 0000000..9252a24
@@ -0,0 +1,26 @@
+What:		/sys/firmware/efi/fw_vendor
+Date:		Nov 2013
+Contact:	Dave Young <[email protected]>

redhat.org eh? Typo or legit?

It's typo, will fix

Quoted 7 lines

Documentation/ABI/testing/sysfs-firmware-efi line 6

Quoted 4 lines
Documentation/ABI/testing/sysfs-firmware-efi new file +3 -0
+Description:
+		Shows the physical address of firmware vendor in the EFI
+		system table.

It shows the physical address of the firmware vendor "entry" or "field" in the EFI system table.

Will update

Quoted 5 lines

Documentation/ABI/testing/sysfs-firmware-efi line 8

Quoted 3 lines
Documentation/ABI/testing/sysfs-firmware-efi new file +2 -0
+Users:
+		Kexec Mailing List <[email protected]>

The "Kexec mailing list" isn't a user, kexec is.

Ok, will change to "Kexec"

Thanks for review
Dave

author
#11 lore raw

PATCH v4 6/12 efi: export efi runtime memory mapping to sysfs

In reply to Dave Young

kexec kernel will need exactly same mapping for
efi runtime memory ranges. Thus here export the
runtime ranges mapping to sysfs, kexec-tools
will assemble them and pass to 2nd kernel via
setup_data.

Introducing a new directly /sys/firmware/efi/runtime-map Just like /sys/firmware/memmap. Containing below attribute in each file of that directory:
attribute num_pages phys_addr type virt_addr

It will not work for efi 32bit. Only x86_64 currently.

Matt: s/efi-runtime-map.c/runtime-map.c
change dir name to runtime-map
update to use desc_size in efi_runtime_map cleaup the code, add function efi_save_runtime_map improve err handling

---
 .../ABI/testing/sysfs-firmware-efi-runtime-map     |  45 +++++
 arch/x86/platform/efi/efi.c                        |  26 +++
 drivers/firmware/efi/Kconfig                       |  10 ++
 drivers/firmware/efi/Makefile                      |   1 +
 drivers/firmware/efi/efi.c                         |   3 +-
 drivers/firmware/efi/runtime-map.c                 | 199 +++++++++++++++++++++
 include/linux/efi.h                                |   6 +
 7 files changed, 289 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/ABI/testing/sysfs-firmware-efi-runtime-map
 create mode 100644 drivers/firmware/efi/runtime-map.c
Documentation/ABI/testing/sysfs-firmware-efi-runtime-map new file +45 -0
new file mode 100644
index 0000000..dab8d41
@@ -0,0 +1,45 @@
+What:		/sys/firmware/efi/runtime-map/
+Date:		Oct 2013
+Contact:	Dave Young <[email protected]>
+Description:
+		Switching efi runtime services to virtual mode requires
+		that all efi memory ranges which has the runtime attribute
+		bit set to be mapped to virtual addresses.
+
+		In kexec kernel kernel can not entering virtual mode again
+		because there's a limitation that SetVirtualAddressMap can
+		only be called once for entering virtual mode. But kexec
+		kernel still need maintain same physical address to virtual
+		address mapping as the 1st kernel. The mappings are exported
+		to sysfs so userspace tools can reassemble them and pass them
+		into kexec kernel.
+
+		/sys/firmware/efi/runtim-map/ is what kernel export for
+		this purpose. The structure is as follows:
+
+		subdirectories are named with the number of the memory range:
+
+			/sys/firmware/efi/runtime-map/0
+			/sys/firmware/efi/runtime-map/1
+			/sys/firmware/efi/runtime-map/2
+			/sys/firmware/efi/runtime-map/3
+			...
+
+		Each subdirectory contains five files:
+
+		attribute : The attribute of the memory range.
+		num_pages : The size of the memory range in page number.
+		phys_addr : The start physical address of the memory range.
+		type      : The type of the memory range.
+		virt_addr : The start virtual address of the memory range.
+
+		Above values are all hexadecimal number with the '0x' prefix.
+
+		So, for example:
+
+			/sys/firmware/efi/runtime-map/0/attribute
+			/sys/firmware/efi/runtime-map/0/num_pages
+			/sys/firmware/efi/runtime-map/0/phys_addr
+			/sys/firmware/efi/runtime-map/0/type
+			/sys/firmware/efi/runtime-map/0/virt_addr
+                        ...
arch/x86/platform/efi/efi.c +26 -0
index 2a292aa..c3a2aaa 100644
@@ -76,6 +76,9 @@ static __initdata efi_config_table_type_t arch_tables[] = {
 	{NULL_GUID, NULL, NULL},
 };
 
+void *efi_runtime_map;
+int nr_efi_runtime_map;
+
 /*
  * Returns 1 if 'facility' is enabled, 0 otherwise.
  */
@@ -811,6 +814,21 @@ static void __init efi_merge_regions(void)
 	}
 }
 
+static int __init efi_save_runtime_map(efi_memory_desc_t *md)
+{
+	efi_runtime_map = krealloc(efi_runtime_map,
+			(nr_efi_runtime_map + 1) *
+			memmap.desc_size, GFP_KERNEL);
+	if (!efi_runtime_map)
+		return -ENOMEM;
+
+	memcpy(efi_runtime_map + nr_efi_runtime_map * memmap.desc_size,
+		md, memmap.desc_size);
+	nr_efi_runtime_map++;
+
+	return 0;
+}
+
 /*
  * Map efi memory ranges for runtime serivce and update new_memmap with virtual
  * addresses.
@@ -821,6 +839,7 @@ static void * __init efi_map_regions(int *count)
 	void *p, *new_memmap = NULL;
 	unsigned long size;
 	u64 end, systab;
+	int error = 0;
 
 	for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
 		md = p;
@@ -852,9 +871,16 @@ static void * __init efi_map_regions(int *count)
 
 		memcpy(new_memmap + (*count * memmap.desc_size), md,
 		       memmap.desc_size);
+		if (!error && md->type != EFI_BOOT_SERVICES_CODE &&
+				md->type != EFI_BOOT_SERVICES_DATA)
+			error = efi_save_runtime_map(md);
 		(*count)++;
 	}
 
+	if (error) {
+		nr_efi_runtime_map = 0;
+		kfree(efi_runtime_map);
+	}
 ret:
 	return new_memmap;
 }
drivers/firmware/efi/Kconfig +10 -0
index b0fc7c7..b4d01ad 100644
@@ -36,4 +36,14 @@ config EFI_VARS_PSTORE_DEFAULT_DISABLE
 	  backend for pstore by default. This setting can be overridden
 	  using the efivars module's pstore_disable parameter.
 
+config EFI_RUNTIME_MAP
+	bool "Export efi runtime maps to sysfs" if EXPERT
+	default X86 && EFI
+	help
+	  Export efi runtime memory maps to /sys/firmware/efi/runtime-map.
+	  That memory map is used for example by kexec to set up efi virtual
+	  mapping the 2nd kernel, but can also be used for debugging purposes.
+
+	  See also Documentation/ABI/testing/sysfs-firmware-efi-runtime-map.
+
 endmenu
drivers/firmware/efi/Makefile +1 -0
index 99245ab..ecadcc1 100644
@@ -4,3 +4,4 @@
 obj-y					+= efi.o vars.o
 obj-$(CONFIG_EFI_VARS)			+= efivars.o
 obj-$(CONFIG_EFI_VARS_PSTORE)		+= efi-pstore.o
+obj-$(CONFIG_EFI_RUNTIME_MAP)		+= runtime-map.o
drivers/firmware/efi/efi.c +2 -1
index 5d85956..a480de8 100644
@@ -38,7 +38,8 @@ struct efi __read_mostly efi = {
 };
 EXPORT_SYMBOL(efi);
 
-static struct kobject *efi_kobj;
+struct kobject *efi_kobj;
+EXPORT_SYMBOL_GPL(efi_kobj);
 static struct kobject *efivars_kobj;
 
 /*
drivers/firmware/efi/runtime-map.c new file +199 -0

199 lines not on this page · raw message

include/linux/efi.h +6 -0
index fb60b10..4f1651d 100644
@@ -872,4 +872,10 @@ int efivars_sysfs_init(void);
 
 #endif /* CONFIG_EFI_VARS */
 
+#ifdef CONFIG_EFI_RUNTIME_MAP
+extern void *efi_runtime_map;
+extern int nr_efi_runtime_map;
+extern struct kobject *efi_kobj;
+#endif
+
 #endif /* _LINUX_EFI_H */
Signature · 1 line
1.8.3.1
#12 lore raw

Re: [PATCH v4 06/12] efi: export efi runtime memory mapping to sysfs

Quoted 7 lines · Dave Young

kexec kernel will need exactly same mapping for
efi runtime memory ranges. Thus here export the
runtime ranges mapping to sysfs, kexec-tools
will assemble them and pass to 2nd kernel via
setup_data.

Introducing a new directly /sys/firmware/efi/runtime-map

I'm not sure why the word "directly" is used here?

Quoted 5 lines

Just like /sys/firmware/memmap. Containing below attribute in each file of that directory:
attribute num_pages phys_addr type virt_addr

It will not work for efi 32bit. Only x86_64 currently.

Actually, exporting the tables does work for 32-bit, right? It's just that kexec doesn't work for 32-bit EFI?

Quoted 30 lines

Matt: s/efi-runtime-map.c/runtime-map.c
change dir name to runtime-map
update to use desc_size in efi_runtime_map cleaup the code, add function efi_save_runtime_map improve err handling

---
 .../ABI/testing/sysfs-firmware-efi-runtime-map     |  45 +++++
 arch/x86/platform/efi/efi.c                        |  26 +++
 drivers/firmware/efi/Kconfig                       |  10 ++
 drivers/firmware/efi/Makefile                      |   1 +
 drivers/firmware/efi/efi.c                         |   3 +-
 drivers/firmware/efi/runtime-map.c                 | 199 +++++++++++++++++++++
 include/linux/efi.h                                |   6 +
 7 files changed, 289 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/ABI/testing/sysfs-firmware-efi-runtime-map
 create mode 100644 drivers/firmware/efi/runtime-map.c
Documentation/ABI/testing/sysfs-firmware-efi-runtime-map new file +5 -0
new file mode 100644
index 0000000..dab8d41
@@ -0,0 +1,45 @@
+What:		/sys/firmware/efi/runtime-map/
+Date:		Oct 2013
+Contact:	Dave Young <[email protected]>
+Description:
+		Switching efi runtime services to virtual mode requires

Documentation/ABI/testing/sysfs-firmware-efi-runtime-map line 15

Quoted 10 lines
Documentation/ABI/testing/sysfs-firmware-efi-runtime-map new file +10 -0
+		that all efi memory ranges which has the runtime attribute
+		bit set to be mapped to virtual addresses.
+
+		In kexec kernel kernel can not entering virtual mode again
+		because there's a limitation that SetVirtualAddressMap can
+		only be called once for entering virtual mode. But kexec
+		kernel still need maintain same physical address to virtual
+		address mapping as the 1st kernel. The mappings are exported
+		to sysfs so userspace tools can reassemble them and pass them
+		into kexec kernel.

How about,

"The efi runtime services can only be switched to virtual
mode once without rebooting. The kexec kernel must maintain the same physical to virtual address mappings as the first kernel. The mappings are exported to sysfs so userspace tools can reassemble them and pass them into the kexec kernel."

?

Documentation/ABI/testing/sysfs-firmware-efi-runtime-map line 17

Quoted 1 line
Documentation/ABI/testing/sysfs-firmware-efi-runtime-map new file +1 -0
+		/sys/firmware/efi/runtim-map/ is what kernel export for
				^^ runtime-map/
Signature · 1 line
Matt Fleming, Intel Open Source Technology Center
author
#13 lore raw

Re: [PATCH v4 06/12] efi: export efi runtime memory mapping to sysfs

Quoted 10 lines · Matt Fleming
Quoted 7 lines · Dave Young

kexec kernel will need exactly same mapping for
efi runtime memory ranges. Thus here export the
runtime ranges mapping to sysfs, kexec-tools
will assemble them and pass to 2nd kernel via
setup_data.

Introducing a new directly /sys/firmware/efi/runtime-map

I'm not sure why the word "directly" is used here?

should be directory, will fix.

Quoted 9 lines
Quoted 6 lines

Just like /sys/firmware/memmap. Containing below attribute in each file of that directory:
attribute num_pages phys_addr type virt_addr

It will not work for efi 32bit. Only x86_64 currently.

Actually, exporting the tables does work for 32-bit, right? It's just that kexec doesn't work for 32-bit EFI?

It does not make sense to export them for 32-bit because we do not support kexec efi on 32-bit. Will change the description to below:

"kexec efi boot on 32-bit is not supported so I only export the tables for 64-bit efi system."

Quoted 44 lines
Quoted 31 lines

Matt: s/efi-runtime-map.c/runtime-map.c
change dir name to runtime-map
update to use desc_size in efi_runtime_map cleaup the code, add function efi_save_runtime_map improve err handling

---
 .../ABI/testing/sysfs-firmware-efi-runtime-map     |  45 +++++
 arch/x86/platform/efi/efi.c                        |  26 +++
 drivers/firmware/efi/Kconfig                       |  10 ++
 drivers/firmware/efi/Makefile                      |   1 +
 drivers/firmware/efi/efi.c                         |   3 +-
 drivers/firmware/efi/runtime-map.c                 | 199 +++++++++++++++++++++
 include/linux/efi.h                                |   6 +
 7 files changed, 289 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/ABI/testing/sysfs-firmware-efi-runtime-map
 create mode 100644 drivers/firmware/efi/runtime-map.c
Documentation/ABI/testing/sysfs-firmware-efi-runtime-map new file +5 -0
new file mode 100644
index 0000000..dab8d41
@@ -0,0 +1,45 @@
+What:		/sys/firmware/efi/runtime-map/
+Date:		Oct 2013
+Contact:	Dave Young <[email protected]>
+Description:
+		Switching efi runtime services to virtual mode requires

Documentation/ABI/testing/sysfs-firmware-efi-runtime-map line 15

Quoted 10 lines
Documentation/ABI/testing/sysfs-firmware-efi-runtime-map new file +10 -0
+		that all efi memory ranges which has the runtime attribute
+		bit set to be mapped to virtual addresses.
+
+		In kexec kernel kernel can not entering virtual mode again
+		because there's a limitation that SetVirtualAddressMap can
+		only be called once for entering virtual mode. But kexec
+		kernel still need maintain same physical address to virtual
+		address mapping as the 1st kernel. The mappings are exported
+		to sysfs so userspace tools can reassemble them and pass them
+		into kexec kernel.

How about,

Quoted 7 lines

"The efi runtime services can only be switched to virtual
mode once without rebooting. The kexec kernel must maintain the same physical to virtual address mappings as the first kernel. The mappings are exported to sysfs so userspace tools can reassemble them and pass them into the kexec kernel."

?

Excellent, will above.

Quoted 4 lines

Documentation/ABI/testing/sysfs-firmware-efi-runtime-map line 17

Quoted 2 lines
Documentation/ABI/testing/sysfs-firmware-efi-runtime-map new file +1 -0
+		/sys/firmware/efi/runtim-map/ is what kernel export for
				^^ runtime-map/

Will fix the typo.

Thanks for review
Dave

author
#14 lore raw

Re: [PATCH v4 06/12] efi: export efi runtime memory mapping to sysfs

Quoted 10 lines · Dave Young
Quoted 4 lines
Quoted 1 line

It will not work for efi 32bit. Only x86_64 currently.

Actually, exporting the tables does work for 32-bit, right? It's just that kexec doesn't work for 32-bit EFI?

It does not make sense to export them for 32-bit because we do not support kexec efi on 32-bit. Will change the description to below:

"kexec efi boot on 32-bit is not supported so I only export the tables for 64-bit efi system."

I thought about only enabling it for x86_64 and efi is in .config. Rethinking about this, since exporting code will also work for 32-bit so I will remove the lines you mentioned.

Thanks
Dave

maintainer
#15 lore raw

Re: [PATCH v4 06/12] efi: export efi runtime memory mapping to sysfs

In reply to Dave Young
Quoted 38 lines · Dave Young

kexec kernel will need exactly same mapping for
efi runtime memory ranges. Thus here export the
runtime ranges mapping to sysfs, kexec-tools
will assemble them and pass to 2nd kernel via
setup_data.

Introducing a new directly /sys/firmware/efi/runtime-map Just like /sys/firmware/memmap. Containing below attribute in each file of that directory:
attribute num_pages phys_addr type virt_addr

It will not work for efi 32bit. Only x86_64 currently.

Matt: s/efi-runtime-map.c/runtime-map.c
change dir name to runtime-map
update to use desc_size in efi_runtime_map cleaup the code, add function efi_save_runtime_map improve err handling

---
 .../ABI/testing/sysfs-firmware-efi-runtime-map     |  45 +++++
 arch/x86/platform/efi/efi.c                        |  26 +++
 drivers/firmware/efi/Kconfig                       |  10 ++
 drivers/firmware/efi/Makefile                      |   1 +
 drivers/firmware/efi/efi.c                         |   3 +-
 drivers/firmware/efi/runtime-map.c                 | 199 +++++++++++++++++++++
 include/linux/efi.h                                |   6 +
 7 files changed, 289 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/ABI/testing/sysfs-firmware-efi-runtime-map
 create mode 100644 drivers/firmware/efi/runtime-map.c
Documentation/ABI/testing/sysfs-firmware-efi-runtime-map new file +0 -0
new file mode 100644
index 0000000..dab8d41

Documentation/ABI/testing/sysfs-firmware-efi-runtime-map line 6

Quoted 6 lines
Documentation/ABI/testing/sysfs-firmware-efi-runtime-map new file +6 -0
@@ -0,0 +1,45 @@
+What:		/sys/firmware/efi/runtime-map/
+Date:		Oct 2013
+Contact:	Dave Young <[email protected]>
+Description:
+		Switching efi runtime services to virtual mode requires
+		that all efi memory ranges which has the runtime attribute

s/has/have/

Documentation/ABI/testing/sysfs-firmware-efi-runtime-map line 13

Quoted 7 lines
Documentation/ABI/testing/sysfs-firmware-efi-runtime-map new file +7 -0
+		bit set to be mapped to virtual addresses.
+
+		In kexec kernel kernel can not entering virtual mode again
+		because there's a limitation that SetVirtualAddressMap can
+		only be called once for entering virtual mode. But kexec
+		kernel still need maintain same physical address to virtual
+		address mapping as the 1st kernel.

Simply say

"SetVirtualAddressMap has the limitation that it can be called only once per system lifetime so a second kernel, like kexec, needs to reuse the exact same virtual addresses like the first kernel."

Documentation/ABI/testing/sysfs-firmware-efi-runtime-map

Quoted 6 lines

The mappings are exported

Documentation/ABI/testing/sysfs-firmware-efi-runtime-map new file +5 -0
+		to sysfs so userspace tools can reassemble them and pass them
+		into kexec kernel.
+
+		/sys/firmware/efi/runtim-map/ is what kernel export for
+		this purpose. The structure is as follows:

"/sys/firmware/efi/runtime-map/ is the directory the kernel exports that information in."

Documentation/ABI/testing/sysfs-firmware-efi-runtime-map

Quoted 12 lines
Documentation/ABI/testing/sysfs-firmware-efi-runtime-map new file +12 -0
+
+		subdirectories are named with the number of the memory range:
+
+			/sys/firmware/efi/runtime-map/0
+			/sys/firmware/efi/runtime-map/1
+			/sys/firmware/efi/runtime-map/2
+			/sys/firmware/efi/runtime-map/3
+			...
+
+		Each subdirectory contains five files:
+
+		attribute : The attribute of the memory range.

attributes

Documentation/ABI/testing/sysfs-firmware-efi-runtime-map

Quoted 1 line
Documentation/ABI/testing/sysfs-firmware-efi-runtime-map new file +1 -0
+		num_pages : The size of the memory range in page number.

in 4K pages.

Documentation/ABI/testing/sysfs-firmware-efi-runtime-map

Quoted 1 line
Documentation/ABI/testing/sysfs-firmware-efi-runtime-map new file +1 -0
+		phys_addr : The start physical address of the memory range.

The physical address of the memory range

Documentation/ABI/testing/sysfs-firmware-efi-runtime-map

Quoted 2 lines
Documentation/ABI/testing/sysfs-firmware-efi-runtime-map new file +2 -0
+		type      : The type of the memory range.
+		virt_addr : The start virtual address of the memory range.

The virtual address...

Documentation/ABI/testing/sysfs-firmware-efi-runtime-map

Quoted 2 lines
Documentation/ABI/testing/sysfs-firmware-efi-runtime-map new file +2 -0
+
+		Above values are all hexadecimal number with the '0x' prefix.

numbers

Documentation/ABI/testing/sysfs-firmware-efi-runtime-map

Quoted 9 lines
Documentation/ABI/testing/sysfs-firmware-efi-runtime-map new file +9 -0
+
+		So, for example:
+
+			/sys/firmware/efi/runtime-map/0/attribute
+			/sys/firmware/efi/runtime-map/0/num_pages
+			/sys/firmware/efi/runtime-map/0/phys_addr
+			/sys/firmware/efi/runtime-map/0/type
+			/sys/firmware/efi/runtime-map/0/virt_addr
+                        ...

What is this example supposed to show? The above is already detailed enough.

Quoted 15 lines
arch/x86/platform/efi/efi.c +3 -0
index 2a292aa..c3a2aaa 100644
@@ -76,6 +76,9 @@ static __initdata efi_config_table_type_t arch_tables[] = {
 	{NULL_GUID, NULL, NULL},
 };
 
+void *efi_runtime_map;
+int nr_efi_runtime_map;
+
 /*
  * Returns 1 if 'facility' is enabled, 0 otherwise.
  */

arch/x86/platform/efi/efi.c static void __init efi_merge_regions(void) · line 817

Quoted 4 lines
arch/x86/platform/efi/efi.c +1 -0
@@ -811,6 +814,21 @@ static void __init efi_merge_regions(void)
 	}
 }
 
+static int __init efi_save_runtime_map(efi_memory_desc_t *md)

It is a static function so no need for the "efi_" prefix.

Also, it should be called save_runtime_mem_desc or so because this is what it does.

arch/x86/platform/efi/efi.c static void __init efi_merge_regions(void) · line 821

Quoted 4 lines
arch/x86/platform/efi/efi.c +4 -0
+{
+	efi_runtime_map = krealloc(efi_runtime_map,
+			(nr_efi_runtime_map + 1) *
+			memmap.desc_size, GFP_KERNEL);

WARNING: Reusing the krealloc arg is almost always a bug #21: FILE: arch/x86/platform/efi/efi.c:819:
+ efi_runtime_map = krealloc(efi_runtime_map,

Please run your diffs through checkpatch.pl --strict before submitting.

Btw, we use krealloc below again which should be changed too.

Quoted 22 lines
arch/x86/platform/efi/efi.c +11 -0
+	if (!efi_runtime_map)
+		return -ENOMEM;
+
+	memcpy(efi_runtime_map + nr_efi_runtime_map * memmap.desc_size,
+		md, memmap.desc_size);
+	nr_efi_runtime_map++;
+
+	return 0;
+}
+
 /*
  * Map efi memory ranges for runtime serivce and update new_memmap with virtual
  * addresses.
@@ -821,6 +839,7 @@ static void * __init efi_map_regions(int *count)
 	void *p, *new_memmap = NULL;
 	unsigned long size;
 	u64 end, systab;
+	int error = 0;
 
 	for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
 		md = p;

arch/x86/platform/efi/efi.c static void * __init efi_map_regions(int *count) · line 876

Quoted 6 lines
arch/x86/platform/efi/efi.c +3 -0
@@ -852,9 +871,16 @@ static void * __init efi_map_regions(int *count)
 
 		memcpy(new_memmap + (*count * memmap.desc_size), md,
 		       memmap.desc_size);
+		if (!error && md->type != EFI_BOOT_SERVICES_CODE &&
+				md->type != EFI_BOOT_SERVICES_DATA)
+			error = efi_save_runtime_map(md);

I'm not sure I like this entangled stuff with the error code - I can barely understand what's going on there - not necessarily because of your changes but because this code is not that trivial, especially after time has passed.

And I don't see you kfreeing efi_runtime_map in the case where the first krealloc() fails at some iteration.

What would be much cleaner IMO would be if you let efi_map_regions() finish, *and*, *only* if it was successful iterate again over the regions and do efi_save_runtime_map() on the runtime ones. This will make the code much more readable too.

arch/x86/platform/efi/efi.c static void * __init efi_map_regions(int *count) · line 883

Quoted 7 lines
arch/x86/platform/efi/efi.c +4 -0
 		(*count)++;
 	}
 
+	if (error) {
+		nr_efi_runtime_map = 0;
+		kfree(efi_runtime_map);
+	}

This error gets overwritten each time the loop runs and you possibly lose !0 values. What I'm suggesting above would take care of that case too.

Quoted 8 lines
arch/x86/platform/efi/efi.c +0 -0
 ret:
 	return new_memmap;
 }
drivers/firmware/efi/Kconfig +0 -0
index b0fc7c7..b4d01ad 100644

drivers/firmware/efi/Kconfig config EFI_VARS_PSTORE_DEFAULT_DISABLE · line 41

Quoted 6 lines
drivers/firmware/efi/Kconfig +3 -0
@@ -36,4 +36,14 @@ config EFI_VARS_PSTORE_DEFAULT_DISABLE
 	  backend for pstore by default. This setting can be overridden
 	  using the efivars module's pstore_disable parameter.
 
+config EFI_RUNTIME_MAP
+	bool "Export efi runtime maps to sysfs" if EXPERT
+	default X86 && EFI

This should depend on KEXEC.

Quoted 13 lines
drivers/firmware/efi/Kconfig +7 -0
+	help
+	  Export efi runtime memory maps to /sys/firmware/efi/runtime-map.
+	  That memory map is used for example by kexec to set up efi virtual
+	  mapping the 2nd kernel, but can also be used for debugging purposes.
+
+	  See also Documentation/ABI/testing/sysfs-firmware-efi-runtime-map.
+
 endmenu
drivers/firmware/efi/Makefile +0 -0
index 99245ab..ecadcc1 100644

drivers/firmware/efi/Makefile line 7

Quoted 4 lines
drivers/firmware/efi/Makefile +1 -0
@@ -4,3 +4,4 @@
 obj-y					+= efi.o vars.o
 obj-$(CONFIG_EFI_VARS)			+= efivars.o
 obj-$(CONFIG_EFI_VARS_PSTORE)		+= efi-pstore.o
+obj-$(CONFIG_EFI_RUNTIME_MAP)		+= runtime-map.o

This will conflict with upstream:

checking file drivers/firmware/efi/Makefile
Hunk #1 FAILED at 4.
1 out of 1 hunk FAILED

It should be:

drivers/firmware/efi/Makefile +1 -0
index 9ba156d3c775..7f05c368b868 100644
@@ -5,3 +5,4 @@ obj-y                                   += efi.o vars.o
 obj-$(CONFIG_EFI_VARS)                 += efivars.o
 obj-$(CONFIG_EFI_VARS_PSTORE)          += efi-pstore.o
 obj-$(CONFIG_UEFI_CPER)                        += cper.o
+obj-$(CONFIG_EFI_RUNTIME_MAP)          += runtime-map.o

drivers/firmware/efi/efi.c struct efi __read_mostly efi = { · line 42

Quoted 11 lines
drivers/firmware/efi/efi.c +2 -1
index 5d85956..a480de8 100644
@@ -38,7 +38,8 @@ struct efi __read_mostly efi = {
 };
 EXPORT_SYMBOL(efi);
 
-static struct kobject *efi_kobj;
+struct kobject *efi_kobj;
+EXPORT_SYMBOL_GPL(efi_kobj);

Can we move this private to linux/drivers/efi/runtime-map.c? If yes, then there's no need to export it.

Quoted 11 lines
drivers/firmware/efi/efi.c +0 -0
 static struct kobject *efivars_kobj;
 
 /*
drivers/firmware/efi/runtime-map.c new file +2 -0
new file mode 100644
index 0000000..036d099
@@ -0,0 +1,199 @@
+/*
+ * linux/drivers/efi/runtime-map.c

drivers/firmware/efi/runtime-map.c line 12

Quoted 10 lines
drivers/firmware/efi/runtime-map.c new file +10 -0
+ * Copyright (C) 2013 Red Hat, Inc., Dave Young <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License v2.0 as published by
+ * the Free Software Foundation
+ *
+ * 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.

Please drop this boilerplate text and do a single sentence saying that it is licensed under GPL v2 and refer to COPYING in the kernel repo.

Quoted 87 lines
drivers/firmware/efi/runtime-map.c new file +87 -0
+ *
+ */
+
+#include <linux/string.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/types.h>
+#include <linux/efi.h>
+#include <linux/slab.h>
+
+#include <asm/setup.h>
+
+struct efi_runtime_map_entry {
+	efi_memory_desc_t md;
+	struct kobject kobj;   /* kobject for each entry */
+};
+
+static struct efi_runtime_map_entry **map_entries;
+
+static ssize_t map_attr_show(struct kobject *kobj,
+				struct attribute *attr, char *buf);
+static ssize_t type_show(struct efi_runtime_map_entry *entry, char *buf);
+static ssize_t phys_addr_show(struct efi_runtime_map_entry *entry, char *buf);
+static ssize_t virt_addr_show(struct efi_runtime_map_entry *entry, char *buf);
+static ssize_t num_pages_show(struct efi_runtime_map_entry *entry, char *buf);
+static ssize_t attribute_show(struct efi_runtime_map_entry *entry, char *buf);
+
+struct map_attribute {
+	struct attribute attr;
+	ssize_t (*show)(struct efi_runtime_map_entry *entry, char *buf);
+};
+
+static struct map_attribute map_type_attr = __ATTR_RO(type);
+static struct map_attribute map_phys_addr_attr   = __ATTR_RO(phys_addr);
+static struct map_attribute map_virt_addr_attr  = __ATTR_RO(virt_addr);
+static struct map_attribute map_num_pages_attr  = __ATTR_RO(num_pages);
+static struct map_attribute map_attribute_attr  = __ATTR_RO(attribute);
+
+/*
+ * These are default attributes that are added for every memmap entry.
+ */
+static struct attribute *def_attrs[] = {
+	&map_type_attr.attr,
+	&map_phys_addr_attr.attr,
+	&map_virt_addr_attr.attr,
+	&map_num_pages_attr.attr,
+	&map_attribute_attr.attr,
+	NULL
+};
+
+static const struct sysfs_ops map_attr_ops = {
+	.show = map_attr_show,
+};
+
+static inline struct efi_runtime_map_entry *to_map_entry(struct kobject *kobj)
+{
+	return container_of(kobj, struct efi_runtime_map_entry, kobj);
+}
+
+static void map_release(struct kobject *kobj)
+{
+	struct efi_runtime_map_entry *entry;
+
+	entry = to_map_entry(kobj);
+	kfree(entry);
+}
+
+static struct kobj_type __refdata map_ktype = {
+	.sysfs_ops	= &map_attr_ops,
+	.default_attrs	= def_attrs,
+	.release	= map_release,
+};
+
+static ssize_t type_show(struct efi_runtime_map_entry *entry, char *buf)
+{
+	return snprintf(buf, PAGE_SIZE, "0x%x\n", entry->md.type);
+}
+
+static ssize_t phys_addr_show(struct efi_runtime_map_entry *entry, char *buf)
+{
+	return snprintf(buf, PAGE_SIZE, "0x%llx\n", entry->md.phys_addr);
+}
+
+static ssize_t virt_addr_show(struct efi_runtime_map_entry *entry, char *buf)
+{
+	return snprintf(buf, PAGE_SIZE, "0x%llx\n", entry->md.virt_addr);
+}

drivers/firmware/efi/runtime-map.c line 109

Quoted 10 lines
drivers/firmware/efi/runtime-map.c new file +10 -0
+
+static ssize_t num_pages_show(struct efi_runtime_map_entry *entry, char *buf)
+{
+	return snprintf(buf, PAGE_SIZE, "0x%llx\n", entry->md.num_pages);
+}
+
+static ssize_t attribute_show(struct efi_runtime_map_entry *entry, char *buf)
+{
+	return snprintf(buf, PAGE_SIZE, "0x%llx\n", entry->md.attribute);
+}

I can see the macro here writing itself too :)

Quoted 10 lines
drivers/firmware/efi/runtime-map.c new file +10 -0
+
+static inline struct map_attribute *to_map_attr(struct attribute *attr)
+{
+	return container_of(attr, struct map_attribute, attr);
+}
+
+static ssize_t map_attr_show(struct kobject *kobj,
+				struct attribute *attr, char *buf)
+{
+	struct efi_runtime_map_entry *entry = to_map_entry(kobj);

drivers/firmware/efi/runtime-map.c line 129

Quoted 10 lines
drivers/firmware/efi/runtime-map.c new file +10 -0
+	struct map_attribute *map_attr = to_map_attr(attr);
+
+	return map_attr->show(entry, buf);
+}
+
+static struct kset *map_kset;
+
+/*
+ * Add map entry on sysfs
+ */

The function name should be enough - no need for the comment.

Quoted 13 lines
drivers/firmware/efi/runtime-map.c new file +13 -0
+static struct efi_runtime_map_entry *add_sysfs_runtime_map_entry(int nr)
+{
+	int ret;
+	unsigned long desc_size;
+	struct efi_runtime_map_entry *entry;
+	struct efi_info *e = &boot_params.efi_info;
+
+	if (!map_kset) {
+		map_kset = kset_create_and_add("runtime-map", NULL,
+				efi_kobj);
+		if (!map_kset)
+			return ERR_PTR(-ENOMEM);
+	}

drivers/firmware/efi/runtime-map.c line 152

Quoted 10 lines
drivers/firmware/efi/runtime-map.c new file +10 -0
+
+	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
+	if (!entry) {
+		kset_unregister(map_kset);
+		return entry;
+	}
+
+	desc_size = e->efi_memdesc_size;
+	memcpy(&entry->md, efi_runtime_map + nr * desc_size,
+		sizeof(efi_memory_desc_t));

If you're going to break this line anyway, you don't really need the local desc_size variable:

	memcpy(&entry->md,
	       efi_runtime_map + nr * e->efi_memdesc_size,
	       sizeof(efi_memory_desc_t));
Quoted 15 lines
drivers/firmware/efi/runtime-map.c new file +15 -0
+
+	kobject_init(&entry->kobj, &map_ktype);
+	entry->kobj.kset = map_kset;
+	ret = kobject_add(&entry->kobj, NULL, "%d", nr);
+	if (ret) {
+		kobject_put(&entry->kobj);
+		kset_unregister(map_kset);
+		return ERR_PTR(ret);
+	}
+
+	return entry;
+}
+
+static int __init efi_runtime_map_init(void)
+{

drivers/firmware/efi/runtime-map.c line 177

Quoted 10 lines
drivers/firmware/efi/runtime-map.c new file +10 -0
+	int i, j, ret = 0;
+	struct efi_runtime_map_entry *entry;
+
+	if (!efi_runtime_map) {
+		pr_warn("no efi_runtime_map found\n");
+		return -EINVAL;
+	}
+
+	map_entries = kzalloc(nr_efi_runtime_map *
+		sizeof(struct efi_runtime_map_entry *), GFP_KERNEL);

Shorten:

	map_entries = kzalloc(nr_efi_runtime_map * sizeof(entry), GFP_KERNEL);
Quoted 5 lines
drivers/firmware/efi/runtime-map.c new file +5 -0
+	if (!map_entries)
+		return -ENOMEM;
+
+	for (i = 0; i < nr_efi_runtime_map; i++) {
+		entry = add_sysfs_runtime_map_entry(i);

drivers/firmware/efi/runtime-map.c line 192

Quoted 10 lines
drivers/firmware/efi/runtime-map.c new file +10 -0
+		if (IS_ERR(entry)) {
+			for (j = i - 1; j > 0; j--) {
+				entry = *(map_entries + j);
+				kobject_put(&entry->kobj);
+			}
+			if (map_kset)
+				kset_unregister(map_kset);
+			ret = PTR_ERR(entry);
+			goto out;
+		}

You can move this error unwinding path away from the main loop:

	if (IS_ERR(entry))
		goto out;

and make it more readable.

drivers/firmware/efi/runtime-map.c line 199

Quoted 7 lines
drivers/firmware/efi/runtime-map.c new file +7 -0
+		*(map_entries + i) = entry;
+	}
+
+out:
+	return ret;
+}
+late_initcall(efi_runtime_map_init);

Why an initcall? Can't we run this from efisubsys_init()?

Thanks.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
author
#16 lore raw

Re: [PATCH v4 06/12] efi: export efi runtime memory mapping to sysfs

Quoted 33 lines · Borislav Petkov
Quoted 32 lines · Dave Young

kexec kernel will need exactly same mapping for
efi runtime memory ranges. Thus here export the
runtime ranges mapping to sysfs, kexec-tools
will assemble them and pass to 2nd kernel via
setup_data.

Introducing a new directly /sys/firmware/efi/runtime-map Just like /sys/firmware/memmap. Containing below attribute in each file of that directory:
attribute num_pages phys_addr type virt_addr

It will not work for efi 32bit. Only x86_64 currently.

Matt: s/efi-runtime-map.c/runtime-map.c
change dir name to runtime-map
update to use desc_size in efi_runtime_map cleaup the code, add function efi_save_runtime_map improve err handling

---
 .../ABI/testing/sysfs-firmware-efi-runtime-map     |  45 +++++
 arch/x86/platform/efi/efi.c                        |  26 +++
 drivers/firmware/efi/Kconfig                       |  10 ++
 drivers/firmware/efi/Makefile                      |   1 +
 drivers/firmware/efi/efi.c                         |   3 +-
 drivers/firmware/efi/runtime-map.c                 | 199 +++++++++++++++++++++
 include/linux/efi.h                                |   6 +
 7 files changed, 289 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/ABI/testing/sysfs-firmware-efi-runtime-map
 create mode 100644 drivers/firmware/efi/runtime-map.c
Quoted 14 lines

Documentation/ABI/testing/sysfs-firmware-efi-runtime-map line 6

Quoted 12 lines
Documentation/ABI/testing/sysfs-firmware-efi-runtime-map new file +6 -0
new file mode 100644
index 0000000..dab8d41
@@ -0,0 +1,45 @@
+What:		/sys/firmware/efi/runtime-map/
+Date:		Oct 2013
+Contact:	Dave Young <[email protected]>
+Description:
+		Switching efi runtime services to virtual mode requires
+		that all efi memory ranges which has the runtime attribute

s/has/have/

Will fix

Quoted 11 lines

Documentation/ABI/testing/sysfs-firmware-efi-runtime-map line 13

Quoted 8 lines
Documentation/ABI/testing/sysfs-firmware-efi-runtime-map new file +7 -0
+		bit set to be mapped to virtual addresses.
+
+		In kexec kernel kernel can not entering virtual mode again
+		because there's a limitation that SetVirtualAddressMap can
+		only be called once for entering virtual mode. But kexec
+		kernel still need maintain same physical address to virtual
+		address mapping as the 1st kernel.

Simply say

Quoted 4 lines

"SetVirtualAddressMap has the limitation that it can be called only once per system lifetime so a second kernel, like kexec, needs to reuse the exact same virtual addresses like the first kernel."

Matt are suggesting to below one, is it ok to you? I'm fine with both.

The efi runtime services can only be switched to virtual mode once without rebooting. The kexec kernel must maintain the same physical to virtual address mappings as the first kernel. The mappings are exported to sysfs so userspace tools can reassemble them and pass them into the kexec kernel.

Quoted 10 lines

Documentation/ABI/testing/sysfs-firmware-efi-runtime-map

Quoted 7 lines

The mappings are exported

Documentation/ABI/testing/sysfs-firmware-efi-runtime-map new file +5 -0
+		to sysfs so userspace tools can reassemble them and pass them
+		into kexec kernel.
+
+		/sys/firmware/efi/runtim-map/ is what kernel export for
+		this purpose. The structure is as follows:

"/sys/firmware/efi/runtime-map/ is the directory the kernel exports that information in."

Ok.

Quoted 15 lines

Documentation/ABI/testing/sysfs-firmware-efi-runtime-map

Quoted 13 lines
Documentation/ABI/testing/sysfs-firmware-efi-runtime-map new file +12 -0
+
+		subdirectories are named with the number of the memory range:
+
+			/sys/firmware/efi/runtime-map/0
+			/sys/firmware/efi/runtime-map/1
+			/sys/firmware/efi/runtime-map/2
+			/sys/firmware/efi/runtime-map/3
+			...
+
+		Each subdirectory contains five files:
+
+		attribute : The attribute of the memory range.

attributes

Ok.

Quoted 4 lines

Documentation/ABI/testing/sysfs-firmware-efi-runtime-map

Quoted 2 lines
Documentation/ABI/testing/sysfs-firmware-efi-runtime-map new file +1 -0
+		num_pages : The size of the memory range in page number.

in 4K pages.

Is it always 4K, how about "in pages"?

Quoted 4 lines

Documentation/ABI/testing/sysfs-firmware-efi-runtime-map

Quoted 2 lines
Documentation/ABI/testing/sysfs-firmware-efi-runtime-map new file +1 -0
+		phys_addr : The start physical address of the memory range.

The physical address of the memory range

Ok.

Quoted 5 lines

Documentation/ABI/testing/sysfs-firmware-efi-runtime-map

Quoted 3 lines
Documentation/ABI/testing/sysfs-firmware-efi-runtime-map new file +2 -0
+		type      : The type of the memory range.
+		virt_addr : The start virtual address of the memory range.

The virtual address...

Ok.

Quoted 5 lines

Documentation/ABI/testing/sysfs-firmware-efi-runtime-map

Quoted 3 lines
Documentation/ABI/testing/sysfs-firmware-efi-runtime-map new file +2 -0
+
+		Above values are all hexadecimal number with the '0x' prefix.

numbers

Ok.

Quoted 13 lines

Documentation/ABI/testing/sysfs-firmware-efi-runtime-map

Quoted 10 lines
Documentation/ABI/testing/sysfs-firmware-efi-runtime-map new file +9 -0
+
+		So, for example:
+
+			/sys/firmware/efi/runtime-map/0/attribute
+			/sys/firmware/efi/runtime-map/0/num_pages
+			/sys/firmware/efi/runtime-map/0/phys_addr
+			/sys/firmware/efi/runtime-map/0/type
+			/sys/firmware/efi/runtime-map/0/virt_addr
+                        ...

What is this example supposed to show? The above is already detailed enough.

Ok, I can remove them.

Quoted 19 lines
Quoted 19 lines
arch/x86/platform/efi/efi.c +3 -0
index 2a292aa..c3a2aaa 100644
@@ -76,6 +76,9 @@ static __initdata efi_config_table_type_t arch_tables[] = {
 	{NULL_GUID, NULL, NULL},
 };
 
+void *efi_runtime_map;
+int nr_efi_runtime_map;
+
 /*
  * Returns 1 if 'facility' is enabled, 0 otherwise.
  */
@@ -811,6 +814,21 @@ static void __init efi_merge_regions(void)
 	}
 }
 
Quoted 6 lines

arch/x86/platform/efi/efi.c static void __init efi_merge_regions(void) · line 817

Quoted 1 line
arch/x86/platform/efi/efi.c +1 -0
+static int __init efi_save_runtime_map(efi_memory_desc_t *md)

It is a static function so no need for the "efi_" prefix.

Also, it should be called save_runtime_mem_desc or so because this is what it does.

Ok.

Quoted 10 lines

arch/x86/platform/efi/efi.c static void __init efi_merge_regions(void) · line 821

Quoted 5 lines
arch/x86/platform/efi/efi.c +4 -0
+{
+	efi_runtime_map = krealloc(efi_runtime_map,
+			(nr_efi_runtime_map + 1) *
+			memmap.desc_size, GFP_KERNEL);

WARNING: Reusing the krealloc arg is almost always a bug #21: FILE: arch/x86/platform/efi/efi.c:819:
+ efi_runtime_map = krealloc(efi_runtime_map,

Quoted 3 lines

Please run your diffs through checkpatch.pl --strict before submitting.

Btw, we use krealloc below again which should be changed too.

Since we do not know the exact number of ranges, introducing a list or count the totoal range numbers before will add more code. Do you really think it's necessary?

I tend to not fix every warnings from checkpatch if it's not a must-fix one.

Quoted 24 lines
Quoted 24 lines
arch/x86/platform/efi/efi.c +11 -0
+	if (!efi_runtime_map)
+		return -ENOMEM;
+
+	memcpy(efi_runtime_map + nr_efi_runtime_map * memmap.desc_size,
+		md, memmap.desc_size);
+	nr_efi_runtime_map++;
+
+	return 0;
+}
+
 /*
  * Map efi memory ranges for runtime serivce and update new_memmap with virtual
  * addresses.
@@ -821,6 +839,7 @@ static void * __init efi_map_regions(int *count)
 	void *p, *new_memmap = NULL;
 	unsigned long size;
 	u64 end, systab;
+	int error = 0;
 
 	for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
 		md = p;
@@ -852,9 +871,16 @@ static void * __init efi_map_regions(int *count)
 
Quoted 10 lines

arch/x86/platform/efi/efi.c static void * __init efi_map_regions(int *count) · line 876

Quoted 5 lines
arch/x86/platform/efi/efi.c +3 -0
 		memcpy(new_memmap + (*count * memmap.desc_size), md,
 		       memmap.desc_size);
+		if (!error && md->type != EFI_BOOT_SERVICES_CODE &&
+				md->type != EFI_BOOT_SERVICES_DATA)
+			error = efi_save_runtime_map(md);

I'm not sure I like this entangled stuff with the error code - I can barely understand what's going on there - not necessarily because of your changes but because this code is not that trivial, especially after time has passed.

suppose kexec runtime map preparing fails we still can get 1st kernel boot ok.

Quoted 3 lines

And I don't see you kfreeing efi_runtime_map in the case where the first krealloc() fails at some iteration.

Good catch, the original krealloc error handling also need improve. Ok, so I will improve the code by adding a link list for new_memmap and removing the krealloc.

Quoted 5 lines

What would be much cleaner IMO would be if you let efi_map_regions() finish, *and*, *only* if it was successful iterate again over the regions and do efi_save_runtime_map() on the runtime ones. This will make the code much more readable too.

For simplifying the code, will do.

Quoted 24 lines

arch/x86/platform/efi/efi.c static void * __init efi_map_regions(int *count) · line 883

Quoted 8 lines
arch/x86/platform/efi/efi.c +4 -0
 		(*count)++;
 	}
 
+	if (error) {
+		nr_efi_runtime_map = 0;
+		kfree(efi_runtime_map);
+	}

This error gets overwritten each time the loop runs and you possibly lose !0 values. What I'm suggesting above would take care of that case too.

Quoted 11 lines
arch/x86/platform/efi/efi.c +0 -0
 ret:
 	return new_memmap;
 }
drivers/firmware/efi/Kconfig +0 -0
index b0fc7c7..b4d01ad 100644
@@ -36,4 +36,14 @@ config EFI_VARS_PSTORE_DEFAULT_DISABLE
 	  backend for pstore by default. This setting can be overridden
 	  using the efivars module's pstore_disable parameter.
 
Quoted 5 lines

drivers/firmware/efi/Kconfig config EFI_VARS_PSTORE_DEFAULT_DISABLE · line 41

Quoted 3 lines
drivers/firmware/efi/Kconfig +3 -0
+config EFI_RUNTIME_MAP
+	bool "Export efi runtime maps to sysfs" if EXPERT
+	default X86 && EFI

This should depend on KEXEC.

I did not add KEXEC because I thought it can be used for debugging purpose.

Quoted 12 lines
Quoted 12 lines
drivers/firmware/efi/Kconfig +7 -0
+	help
+	  Export efi runtime memory maps to /sys/firmware/efi/runtime-map.
+	  That memory map is used for example by kexec to set up efi virtual
+	  mapping the 2nd kernel, but can also be used for debugging purposes.
+
+	  See also Documentation/ABI/testing/sysfs-firmware-efi-runtime-map.
+
 endmenu
drivers/firmware/efi/Makefile +0 -1
index 99245ab..ecadcc1 100644
--- a/drivers/firmware/efi/Makefile
Quoted 8 lines

drivers/firmware/efi/Makefile line 7

Quoted 6 lines

+++ b/drivers/firmware/efi/Makefile

drivers/firmware/efi/Makefile +1 -0
@@ -4,3 +4,4 @@
 obj-y					+= efi.o vars.o
 obj-$(CONFIG_EFI_VARS)			+= efivars.o
 obj-$(CONFIG_EFI_VARS_PSTORE)		+= efi-pstore.o
+obj-$(CONFIG_EFI_RUNTIME_MAP)		+= runtime-map.o

This will conflict with upstream:

Will check and update, what tree are you using?

Quoted 26 lines

checking file drivers/firmware/efi/Makefile
Hunk #1 FAILED at 4.
1 out of 1 hunk FAILED

It should be:

drivers/firmware/efi/Makefile +1 -0
index 9ba156d3c775..7f05c368b868 100644
@@ -5,3 +5,4 @@ obj-y                                   += efi.o vars.o
 obj-$(CONFIG_EFI_VARS)                 += efivars.o
 obj-$(CONFIG_EFI_VARS_PSTORE)          += efi-pstore.o
 obj-$(CONFIG_UEFI_CPER)                        += cper.o
+obj-$(CONFIG_EFI_RUNTIME_MAP)          += runtime-map.o
Quoted 10 lines
drivers/firmware/efi/efi.c +0 -0
index 5d85956..a480de8 100644
@@ -38,7 +38,8 @@ struct efi __read_mostly efi = {
 };
 EXPORT_SYMBOL(efi);
 
Quoted 6 lines

drivers/firmware/efi/efi.c struct efi __read_mostly efi = { · line 42

Quoted 3 lines
drivers/firmware/efi/efi.c +2 -1
-static struct kobject *efi_kobj;
+struct kobject *efi_kobj;
+EXPORT_SYMBOL_GPL(efi_kobj);

Can we move this private to linux/drivers/efi/runtime-map.c? If yes, then there's no need to export it.

Hmm, it's created in efi.c so I think we can not move it..

Quoted 17 lines
Quoted 17 lines
drivers/firmware/efi/efi.c +0 -0
 static struct kobject *efivars_kobj;
 
 /*
drivers/firmware/efi/runtime-map.c new file +7 -0
new file mode 100644
index 0000000..036d099
@@ -0,0 +1,199 @@
+/*
+ * linux/drivers/efi/runtime-map.c
+ * Copyright (C) 2013 Red Hat, Inc., Dave Young <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License v2.0 as published by
+ * the Free Software Foundation
Quoted 8 lines

drivers/firmware/efi/runtime-map.c line 12

Quoted 5 lines
drivers/firmware/efi/runtime-map.c new file +5 -0
+ *
+ * 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.

Please drop this boilerplate text and do a single sentence saying that it is licensed under GPL v2 and refer to COPYING in the kernel repo.

Ok.

Quoted 92 lines
Quoted 92 lines
drivers/firmware/efi/runtime-map.c new file +91 -0
+ *
+ */
+
+#include <linux/string.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/types.h>
+#include <linux/efi.h>
+#include <linux/slab.h>
+
+#include <asm/setup.h>
+
+struct efi_runtime_map_entry {
+	efi_memory_desc_t md;
+	struct kobject kobj;   /* kobject for each entry */
+};
+
+static struct efi_runtime_map_entry **map_entries;
+
+static ssize_t map_attr_show(struct kobject *kobj,
+				struct attribute *attr, char *buf);
+static ssize_t type_show(struct efi_runtime_map_entry *entry, char *buf);
+static ssize_t phys_addr_show(struct efi_runtime_map_entry *entry, char *buf);
+static ssize_t virt_addr_show(struct efi_runtime_map_entry *entry, char *buf);
+static ssize_t num_pages_show(struct efi_runtime_map_entry *entry, char *buf);
+static ssize_t attribute_show(struct efi_runtime_map_entry *entry, char *buf);
+
+struct map_attribute {
+	struct attribute attr;
+	ssize_t (*show)(struct efi_runtime_map_entry *entry, char *buf);
+};
+
+static struct map_attribute map_type_attr = __ATTR_RO(type);
+static struct map_attribute map_phys_addr_attr   = __ATTR_RO(phys_addr);
+static struct map_attribute map_virt_addr_attr  = __ATTR_RO(virt_addr);
+static struct map_attribute map_num_pages_attr  = __ATTR_RO(num_pages);
+static struct map_attribute map_attribute_attr  = __ATTR_RO(attribute);
+
+/*
+ * These are default attributes that are added for every memmap entry.
+ */
+static struct attribute *def_attrs[] = {
+	&map_type_attr.attr,
+	&map_phys_addr_attr.attr,
+	&map_virt_addr_attr.attr,
+	&map_num_pages_attr.attr,
+	&map_attribute_attr.attr,
+	NULL
+};
+
+static const struct sysfs_ops map_attr_ops = {
+	.show = map_attr_show,
+};
+
+static inline struct efi_runtime_map_entry *to_map_entry(struct kobject *kobj)
+{
+	return container_of(kobj, struct efi_runtime_map_entry, kobj);
+}
+
+static void map_release(struct kobject *kobj)
+{
+	struct efi_runtime_map_entry *entry;
+
+	entry = to_map_entry(kobj);
+	kfree(entry);
+}
+
+static struct kobj_type __refdata map_ktype = {
+	.sysfs_ops	= &map_attr_ops,
+	.default_attrs	= def_attrs,
+	.release	= map_release,
+};
+
+static ssize_t type_show(struct efi_runtime_map_entry *entry, char *buf)
+{
+	return snprintf(buf, PAGE_SIZE, "0x%x\n", entry->md.type);
+}
+
+static ssize_t phys_addr_show(struct efi_runtime_map_entry *entry, char *buf)
+{
+	return snprintf(buf, PAGE_SIZE, "0x%llx\n", entry->md.phys_addr);
+}
+
+static ssize_t virt_addr_show(struct efi_runtime_map_entry *entry, char *buf)
+{
+	return snprintf(buf, PAGE_SIZE, "0x%llx\n", entry->md.virt_addr);
+}
+
+static ssize_t num_pages_show(struct efi_runtime_map_entry *entry, char *buf)
+{
+	return snprintf(buf, PAGE_SIZE, "0x%llx\n", entry->md.num_pages);
Quoted 8 lines

drivers/firmware/efi/runtime-map.c line 109

Quoted 6 lines
drivers/firmware/efi/runtime-map.c new file +6 -0
+}
+
+static ssize_t attribute_show(struct efi_runtime_map_entry *entry, char *buf)
+{
+	return snprintf(buf, PAGE_SIZE, "0x%llx\n", entry->md.attribute);
+}

I can see the macro here writing itself too :)

Thanks for catching, actually I have changed it in my local tree since your last comment about other file.

Quoted 15 lines
Quoted 15 lines
drivers/firmware/efi/runtime-map.c new file +14 -0
+
+static inline struct map_attribute *to_map_attr(struct attribute *attr)
+{
+	return container_of(attr, struct map_attribute, attr);
+}
+
+static ssize_t map_attr_show(struct kobject *kobj,
+				struct attribute *attr, char *buf)
+{
+	struct efi_runtime_map_entry *entry = to_map_entry(kobj);
+	struct map_attribute *map_attr = to_map_attr(attr);
+
+	return map_attr->show(entry, buf);
+}
Quoted 8 lines

drivers/firmware/efi/runtime-map.c line 129

Quoted 6 lines
drivers/firmware/efi/runtime-map.c new file +6 -0
+
+static struct kset *map_kset;
+
+/*
+ * Add map entry on sysfs
+ */

The function name should be enough - no need for the comment.

Ok.

Quoted 19 lines
Quoted 19 lines
drivers/firmware/efi/runtime-map.c new file +18 -0
+static struct efi_runtime_map_entry *add_sysfs_runtime_map_entry(int nr)
+{
+	int ret;
+	unsigned long desc_size;
+	struct efi_runtime_map_entry *entry;
+	struct efi_info *e = &boot_params.efi_info;
+
+	if (!map_kset) {
+		map_kset = kset_create_and_add("runtime-map", NULL,
+				efi_kobj);
+		if (!map_kset)
+			return ERR_PTR(-ENOMEM);
+	}
+
+	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
+	if (!entry) {
+		kset_unregister(map_kset);
+		return entry;
Quoted 8 lines

drivers/firmware/efi/runtime-map.c line 152

Quoted 5 lines
drivers/firmware/efi/runtime-map.c new file +5 -0
+	}
+
+	desc_size = e->efi_memdesc_size;
+	memcpy(&entry->md, efi_runtime_map + nr * desc_size,
+		sizeof(efi_memory_desc_t));

If you're going to break this line anyway, you don't really need the local desc_size variable:

Ok

Quoted 33 lines
	memcpy(&entry->md,
	       efi_runtime_map + nr * e->efi_memdesc_size,
	       sizeof(efi_memory_desc_t));
Quoted 15 lines
drivers/firmware/efi/runtime-map.c new file +15 -0
+
+	kobject_init(&entry->kobj, &map_ktype);
+	entry->kobj.kset = map_kset;
+	ret = kobject_add(&entry->kobj, NULL, "%d", nr);
+	if (ret) {
+		kobject_put(&entry->kobj);
+		kset_unregister(map_kset);
+		return ERR_PTR(ret);
+	}
+
+	return entry;
+}
+
+static int __init efi_runtime_map_init(void)
+{

drivers/firmware/efi/runtime-map.c line 177

Quoted 10 lines
drivers/firmware/efi/runtime-map.c new file +10 -0
+	int i, j, ret = 0;
+	struct efi_runtime_map_entry *entry;
+
+	if (!efi_runtime_map) {
+		pr_warn("no efi_runtime_map found\n");
+		return -EINVAL;
+	}
+
+	map_entries = kzalloc(nr_efi_runtime_map *
+		sizeof(struct efi_runtime_map_entry *), GFP_KERNEL);

Shorten:

Quoted 1 line
	map_entries = kzalloc(nr_efi_runtime_map * sizeof(entry), GFP_KERNEL);

Will do.

Quoted 19 lines
Quoted 6 lines
drivers/firmware/efi/runtime-map.c new file +5 -0
+	if (!map_entries)
+		return -ENOMEM;
+
+	for (i = 0; i < nr_efi_runtime_map; i++) {
+		entry = add_sysfs_runtime_map_entry(i);

drivers/firmware/efi/runtime-map.c line 192

Quoted 10 lines
drivers/firmware/efi/runtime-map.c new file +10 -0
+		if (IS_ERR(entry)) {
+			for (j = i - 1; j > 0; j--) {
+				entry = *(map_entries + j);
+				kobject_put(&entry->kobj);
+			}
+			if (map_kset)
+				kset_unregister(map_kset);
+			ret = PTR_ERR(entry);
+			goto out;
+		}

You can move this error unwinding path away from the main loop:

Quoted 4 lines
	if (IS_ERR(entry))
		goto out;

and make it more readable.

Ok.

Quoted 10 lines

drivers/firmware/efi/runtime-map.c line 199

Quoted 8 lines
drivers/firmware/efi/runtime-map.c new file +7 -0
+		*(map_entries + i) = entry;
+	}
+
+out:
+	return ret;
+}
+late_initcall(efi_runtime_map_init);

Why an initcall? Can't we run this from efisubsys_init()?

Yes, we can, will do.

Thanks for review
Dave

maintainer
#17 lore raw

Re: [PATCH v4 06/12] efi: export efi runtime memory mapping to sysfs

Quoted 7 lines · Dave Young

Matt are suggesting to below one, is it ok to you? I'm fine with both.

The efi runtime services can only be switched to virtual mode once without rebooting. The kexec kernel must maintain the same physical to virtual address mappings as the first kernel. The mappings are exported to sysfs so userspace tools can reassemble them and pass them into the kexec kernel.

His is explaining it better so take his :)

Quoted 1 line

Is it always 4K, how about "in pages"?

Actually no, so "in pages" is better.

Quoted 6 lines

Since we do not know the exact number of ranges, introducing a list or count the totoal range numbers before will add more code. Do you really think it's necessary?

I tend to not fix every warnings from checkpatch if it's not a must-fix one.

No need for the list - I actually meant you simply use a tmp pointer for each krealloc invocation - just look around the tree for examples.

Also think about what happens to the pointed-to memory when krealloc returns NULL.

Quoted 2 lines

suppose kexec runtime map preparing fails we still can get 1st kernel boot ok.

Yeah, nothing stops that - especially if you iterate over the mapped regions a second time separately, for kexec.

Which reminds me, all that save_runtime_map code needs to be behind CONFIG_KEXEC. We don't want to be doing this if KEXEC is not enabled.

Quoted 6 lines
Quoted 4 lines

What would be much cleaner IMO would be if you let efi_map_regions() finish, *and*, *only* if it was successful iterate again over the regions and do efi_save_runtime_map() on the runtime ones. This will make the code much more readable too.

For simplifying the code, will do.

Right, and hide it behind CONFIG_KEXEC too, while you're at it.

Quoted 2 lines

I did not add KEXEC because I thought it can be used for debugging purpose.

mfleming is thinking about it :)

Quoted 1 line

Will check and update, what tree are you using?

Always current Linus tree and tip/master. This helps catch issues like that.

Quoted 1 line

Hmm, it's created in efi.c so I think we can not move it..

Ok, so we apparently export naked kobjects like that. All right, fair enough.

...

Quoted 2 lines

Thanks for catching, actually I have changed it in my local tree since your last comment about other file.

:)

Thanks.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
#18 lore raw

Re: [PATCH v4 06/12] efi: export efi runtime memory mapping to sysfs

Quoted 4 lines · Borislav Petkov
Quoted 2 lines

I did not add KEXEC because I thought it can be used for debugging purpose.

mfleming is thinking about it :)

I finally finished my thought. The sysfs code should be automatically enabled only if CONFIG_KEXEC is enabled, and not on my default because it is a debug interface. While it's true that the code may be useful for debugging the memory mappings of EFI systems, it doesn't make sense to always turn it on just for CONFIG_X86 and CONFIG_EFI.

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

Re: [PATCH v4 06/12] efi: export efi runtime memory mapping to sysfs

Quoted 11 lines · Matt Fleming
Quoted 4 lines · Borislav Petkov
Quoted 2 lines

I did not add KEXEC because I thought it can be used for debugging purpose.

mfleming is thinking about it :)

I finally finished my thought. The sysfs code should be automatically enabled only if CONFIG_KEXEC is enabled, and not on my default because it is a debug interface. While it's true that the code may be useful for debugging the memory mappings of EFI systems, it doesn't make sense to always turn it on just for CONFIG_X86 and CONFIG_EFI.

Ok, will enable it only for CONFIG_X86 and CONFIG_EFI and CONFIG_KEXEC

Quoted 3 lines
Signature · 2 lines
-- 
Matt Fleming, Intel Open Source Technology Center
author
#20 lore raw

Re: [PATCH v4 06/12] efi: export efi runtime memory mapping to sysfs

In reply to Borislav Petkov
Quoted 5 lines · Borislav Petkov

No need for the list - I actually meant you simply use a tmp pointer for each krealloc invocation - just look around the tree for examples.

Also think about what happens to the pointed-to memory when krealloc returns NULL.

Ok, thanks for the explaination.

Quoted 9 lines
Quoted 3 lines

suppose kexec runtime map preparing fails we still can get 1st kernel boot ok.

Yeah, nothing stops that - especially if you iterate over the mapped regions a second time separately, for kexec.

Which reminds me, all that save_runtime_map code needs to be behind CONFIG_KEXEC. We don't want to be doing this if KEXEC is not enabled.

They are only called if there's setup_data SETUP_EFI passed in. Yes, I think only kexec need that part of code. But I hesitated to mess the code with a lot of #if. Will think about it.

Thanks
Dave

maintainer
#21 lore raw

Re: [PATCH v4 06/12] efi: export efi runtime memory mapping to sysfs

Quoted 3 lines · Dave Young

They are only called if there's setup_data SETUP_EFI passed in. Yes, I think only kexec need that part of code. But I hesitated to mess the code with a lot of #if. Will think about it.

Well, the accepted strategy with the efi code is to have function calls in efi.c and then have different versions of those functions defined for 32 and 64-bit, i.e. efi_32.c and efi_64.c.

I can imagine adding an efi_kexec.c could make the whole deal very clearly arranged without ifdeffery.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
author
#22 lore raw

Re: [PATCH v4 06/12] efi: export efi runtime memory mapping to sysfs

Hi, all

Update my status:

I have finished most of thecode related changes including the krealloc fixes (both for original code and my new code). And I'm slowly moving the kexec related stuff to efi_kexec.c, this involves some other cleanups, such as:
efi_systab_init:
-> move 64bit code to efi_64.c, move 32bit code to efi_32.c

efi_setup global variable is changing to physical address, ioremapping it when necessary instead of directly using the virt addr because previously the virt addr will be freed until entering virtual mode, it's possible a leak in case something is wrong in the middle also it occupies the ioremap slot a little bit long.

There's also other problems, so I still need a few days probably a week to carefully restructure the code.

Thanks
Dave

Sending from web interface so sorry for the email format.
----- Original Message -----
From: "Borislav Petkov" <[email protected]>
To: "Dave Young" <[email protected]>
Cc: [email protected], [email protected], "toshi kani" <[email protected]>, matt-HNK1S37rvNbeXh+fF434Mdi2O/[email protected], [email protected], [email protected], [email protected], [email protected], "James Bottomley" <James.Bottomley-d9PhHud1JfjCXq6kfMZ53/[email protected]>, horms-/[email protected], ebiederm-aS9lmoZGLiVWk0Htik3J/[email protected], [email protected], [email protected]
Sent: Monday, December 2, 2013 5:40:20 PM
Subject: Re: [PATCH v4 06/12] efi: export efi runtime memory mapping to sysfs

Quoted 3 lines · Dave Young

They are only called if there's setup_data SETUP_EFI passed in. Yes, I think only kexec need that part of code. But I hesitated to mess the code with a lot of #if. Will think about it.

Well, the accepted strategy with the efi code is to have function calls in efi.c and then have different versions of those functions defined for 32 and 64-bit, i.e. efi_32.c and efi_64.c.

I can imagine adding an efi_kexec.c could make the whole deal very clearly arranged without ifdeffery.

Signature · 10 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
maintainer
#23 lore raw

Re: [PATCH v4 06/12] efi: export efi runtime memory mapping to sysfs

Quoted 11 lines · Dave Young

Hi, all

Update my status:

I have finished most of thecode related changes including the krealloc fixes (both for original code and my new code). And I'm slowly moving the kexec related stuff to efi_kexec.c, this involves some other cleanups, such as:
efi_systab_init:
-> move 64bit code to efi_64.c, move 32bit code to efi_32.c

Quoted 8 lines

efi_setup global variable is changing to physical address, ioremapping it when necessary instead of directly using the virt addr because previously the virt addr will be freed until entering virtual mode, it's possible a leak in case something is wrong in the middle also it occupies the ioremap slot a little bit long.

There's also other problems, so I still need a few days probably a week to carefully restructure the code.

Those are all lofty goals but beware that trying to solve it all at once can be very painful and can cause unnecessary work for both coders and reviewers. Especially if the patchset grows out of proportion which makes it much harder to review and deal with.

So, from my experience and if I were you, I'd try to address the most important issue I'm trying to fix, have 10ish nice clean patches and put the rest on the TODO list. This is much easier to deal with for everyone involved than trying to solve it all at once.

Besides, if you solve it all at once, what are you going to work on afterwards? :-)

HTH.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
#24 lore raw

Re: [PATCH v4 06/12] efi: export efi runtime memory mapping to sysfs

This is very good advice indeed.

Quoted 39 lines · Borislav Petkov
Quoted 5 lines · Dave Young

Hi, all

Update my status:

I have finished most of thecode related changes including the

krealloc

Quoted 7 lines

fixes (both for original code and my new code). And I'm slowly moving the kexec related stuff to efi_kexec.c, this involves some other cleanups, such as:
efi_systab_init:
-> move 64bit code to efi_64.c, move 32bit code to efi_32.c

efi_setup global variable is changing to physical address,

ioremapping

Quoted 7 lines

it when necessary instead of directly using the virt addr because previously the virt addr will be freed until entering virtual mode, it's possible a leak in case something is wrong in the middle also it occupies the ioremap slot a little bit long.

There's also other problems, so I still need a few days probably a week to carefully restructure the code.

Those are all lofty goals but beware that trying to solve it all at once can be very painful and can cause unnecessary work for both coders and reviewers. Especially if the patchset grows out of proportion which makes it much harder to review and deal with.

So, from my experience and if I were you, I'd try to address the most important issue I'm trying to fix, have 10ish nice clean patches and put
the rest on the TODO list. This is much easier to deal with for everyone
involved than trying to solve it all at once.

Besides, if you solve it all at once, what are you going to work on afterwards? :-)

HTH.

Signature · 1 line
Sent from my mobile phone.  Please pardon brevity and lack of formatting.
author
#25 lore raw

Re: [PATCH v4 06/12] efi: export efi runtime memory mapping to sysfs

In reply to Borislav Petkov
Quoted 26 lines · Borislav Petkov
Quoted 11 lines · Dave Young

Hi, all

Update my status:

I have finished most of thecode related changes including the krealloc fixes (both for original code and my new code). And I'm slowly moving the kexec related stuff to efi_kexec.c, this involves some other cleanups, such as:
efi_systab_init:
-> move 64bit code to efi_64.c, move 32bit code to efi_32.c

Quoted 8 lines

efi_setup global variable is changing to physical address, ioremapping it when necessary instead of directly using the virt addr because previously the virt addr will be freed until entering virtual mode, it's possible a leak in case something is wrong in the middle also it occupies the ioremap slot a little bit long.

There's also other problems, so I still need a few days probably a week to carefully restructure the code.

Those are all lofty goals but beware that trying to solve it all at once can be very painful and can cause unnecessary work for both coders and reviewers. Especially if the patchset grows out of proportion which makes it much harder to review and deal with.

Quoted 8 lines

So, from my experience and if I were you, I'd try to address the most important issue I'm trying to fix, have 10ish nice clean patches and put the rest on the TODO list. This is much easier to deal with for everyone involved than trying to solve it all at once.

Besides, if you solve it all at once, what are you going to work on afterwards? :-)

I like your suggestion for splitting kexec stuff to another file efi_kexec.c. but yes it needs more work and is bloating the patchset, I'm fine to leave it in TODO list.

Thanks
Dave

author
#26 lore raw

PATCH v4 7/12 efi: passing kexec necessary efi data via setup_data

In reply to Dave Young

Add a new setup_data type SETUP_EFI for kexec use. Passing the saved fw_vendor, runtime, config tables and efi runtime mappings.

When entering virtual mode, directly mapping the efi runtime ragions which we passed in previously. And skip the step to call SetVirtualAddressMap.

Specially for HP z420 workstation it need another variable saving, it's the smbios physical address, the HP bios also update the SMBIOS address after entering virtual mode besides of the standard fw_vendor,runtime and config table.

Tested on ovmf+qemu, lenovo thinkpad, a dell laptop and an HP z420 workstation.

v2: refresh based on previous patch changes, code cleanup.
v3: use ioremap instead of phys_to_virt for esdata

---
 arch/x86/include/asm/efi.h            |  12 +++
 arch/x86/include/uapi/asm/bootparam.h |   1 +
 arch/x86/kernel/setup.c               |   3 +
 arch/x86/platform/efi/efi.c           | 161 ++++++++++++++++++++++++++++++----
 4 files changed, 160 insertions(+), 17 deletions(-)
arch/x86/include/asm/efi.h +12 -0
index 9fbaeb2..73d5643 100644
@@ -133,6 +133,18 @@ extern void efi_sync_low_kernel_mappings(void);
 extern void efi_setup_page_tables(void);
 extern void __init old_map_region(efi_memory_desc_t *md);
 
+struct efi_setup_data {
+	u64 fw_vendor;
+	u64 runtime;
+	u64 tables;
+	u64 smbios;
+	u64 reserved[8];
+	efi_memory_desc_t map[0];
+};
+
+extern void parse_efi_setup(u64 phys_addr, u32 data_len);
+extern struct efi_setup_data *esdata;
+
 #ifdef CONFIG_EFI
 
 static inline bool efi_is_native(void)
arch/x86/include/uapi/asm/bootparam.h +1 -0
index c15ddaf..ed6afc9 100644
@@ -6,6 +6,7 @@
 #define SETUP_E820_EXT			1
 #define SETUP_DTB			2
 #define SETUP_PCI			3
+#define SETUP_EFI			4
 
 /* ram_size flags */
 #define RAMDISK_IMAGE_START_MASK	0x07FF
arch/x86/kernel/setup.c +3 -0
index f0de629..ecf225e 100644
@@ -447,6 +447,9 @@ static void __init parse_setup_data(void)
 		case SETUP_DTB:
 			add_dtb(pa_data);
 			break;
+		case SETUP_EFI:
+			parse_efi_setup(pa_data, data_len);
+			break;
 		default:
 			break;
 		}
arch/x86/platform/efi/efi.c +144 -17

224 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#27 lore raw

Re: [PATCH v4 07/12] efi: passing kexec necessary efi data via setup_data

Quoted 26 lines · Dave Young

Add a new setup_data type SETUP_EFI for kexec use. Passing the saved fw_vendor, runtime, config tables and efi runtime mappings.

When entering virtual mode, directly mapping the efi runtime ragions which we passed in previously. And skip the step to call SetVirtualAddressMap.

Specially for HP z420 workstation it need another variable saving, it's the smbios physical address, the HP bios also update the SMBIOS address after entering virtual mode besides of the standard fw_vendor,runtime and config table.

Tested on ovmf+qemu, lenovo thinkpad, a dell laptop and an HP z420 workstation.

v2: refresh based on previous patch changes, code cleanup.
v3: use ioremap instead of phys_to_virt for esdata

---
 arch/x86/include/asm/efi.h            |  12 +++
 arch/x86/include/uapi/asm/bootparam.h |   1 +
 arch/x86/kernel/setup.c               |   3 +
 arch/x86/platform/efi/efi.c           | 161 ++++++++++++++++++++++++++++++----
 4 files changed, 160 insertions(+), 17 deletions(-)

[...]

arch/x86/platform/efi/efi.c static int __init setup_storage_paranoia(char *arg) · line 128

Quoted 10 lines
arch/x86/platform/efi/efi.c +10 -0
+void __init parse_efi_setup(u64 phys_addr, u32 data_len)
+{
+	int size;
+	struct setup_data *sdata;
+	u64 esdata_phys;
+
+	if (!efi_enabled(EFI_64BIT)) {
+		pr_warn("skipping setup_data on EFI 32BIT!");
+		return;
+	}

Hmm... this warning could be more informative. Perhaps something along the lines of,

"SETUP_EFI not supported on 32-bit\n"

because the reason we skip is because it isn't supported.

Quoted 20 lines
arch/x86/platform/efi/efi.c +16 -0
+
+	sdata = early_memremap(phys_addr, data_len);
+	if (!sdata)
+		return;
+
+	size = data_len - sizeof(struct setup_data);
+
+	esdata_phys = phys_addr + sizeof(struct setup_data);
+
+	nr_efi_runtime_map = (size - sizeof(struct efi_setup_data)) /
+			sizeof(efi_memory_desc_t);
+	early_iounmap(sdata, data_len);
+
+	/* iounmap esdata in function efi_enter_virtual_mode */
+	esdata = early_memremap(esdata_phys, size);
+}
 
 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
 {

arch/x86/platform/efi/efi.c static int __init efi_systab_init(void *phys) · line 535

Quoted 7 lines
arch/x86/platform/efi/efi.c +2 -2
@@ -504,8 +531,12 @@ static int __init efi_systab_init(void *phys)
 		}
 
 		efi_systab.hdr = systab64->hdr;
-		efi_systab.fw_vendor = systab64->fw_vendor;
-		tmp |= systab64->fw_vendor;
+
+		if (esdata)

Could we name this something more explicit? 'efi_setup' perhaps?

Quoted 21 lines
arch/x86/platform/efi/efi.c +17 -0
@@ -631,6 +670,41 @@ static int __init efi_memmap_init(void)
 	return 0;
 }
 
+static int __init efi_reuse_config(u64 tables, int nr_tables)
+{
+	void *p, *tablep;
+	int i, sz;
+
+	if (!efi_enabled(EFI_64BIT))
+		return 0;
+
+	sz = sizeof(efi_config_table_64_t);
+
+	p = tablep = early_memremap(tables, nr_tables * sz);
+	if (!p) {
+		pr_err("Could not map Configuration table!\n");
+		return -ENOMEM;
+	}
+
+	for (i = 0; i < efi.systab->nr_tables; i++) {

arch/x86/platform/efi/efi.c static int __init efi_memmap_init(void) · line 699

Quoted 10 lines
arch/x86/platform/efi/efi.c +10 -0
+		efi_guid_t guid;
+
+		guid = ((efi_config_table_64_t *)p)->guid;
+
+		/*
+		HP z420 workstation smbios will be convert to
+		virtual address after enter virtual mode.
+		Thus in case kexec/kdump the physical address
+		will be passed in setup_data.
+		*/

This isn't the correct multi-line comment format,

		/*
		 * This is the preferred way to to write a multi-line
		 * comment.
		 */
Quoted 12 lines
arch/x86/platform/efi/efi.c +8 -0
+		if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
+			((efi_config_table_64_t *)p)->table = esdata->smbios;
+		p += sz;
+	}
+	early_iounmap(tablep, nr_tables * sz);
+	return 0;
+}
+
 void __init efi_init(void)
 {
 	efi_char16_t *c16;

arch/x86/platform/efi/efi.c void __init efi_init(void) · line 755

Quoted 6 lines
arch/x86/platform/efi/efi.c +3 -0
@@ -676,6 +750,9 @@ void __init efi_init(void)
 		efi.systab->hdr.revision >> 16,
 		efi.systab->hdr.revision & 0xffff, vendor);
 
+	if (esdata && esdata->smbios)
+		efi_reuse_config(efi.systab->tables, efi.systab->nr_tables);
+

Would it make sense to move the ->smbios check inside efi_reuse_config() in case we need to extend the number of tables in the future?

Quoted 16 lines
arch/x86/platform/efi/efi.c +9 -0
 	if (efi_config_init(arch_tables))
 		return;
 
@@ -886,6 +963,43 @@ ret:
 }
 
 /*
+ * map efi regions which was passed via setup_data
+ * the virt_addr is a fixed addr which was used in
+ * 1st kernel of kexec boot.
+ */
+static void __init efi_map_regions_fixed(void)
+{
+	int i;
+	unsigned long size;
+	efi_memory_desc_t *md;

arch/x86/platform/efi/efi.c ret: · line 984

Quoted 10 lines
arch/x86/platform/efi/efi.c +10 -0
+	u64 end, systab;
+	void *p;
+
+	efi_runtime_map = kzalloc(nr_efi_runtime_map * memmap.desc_size,
+				GFP_KERNEL);
+	if (!efi_runtime_map)
+		pr_err("Out of memory, EFI runtime on nested kexec non-functional!\n");
+
+	for (i = 0, p = efi_runtime_map; i < nr_efi_runtime_map; i++) {
+		md = esdata->map + i;

Heh, I read that as 'esdata->map + 1' the first few times.

Quoted 4 lines
arch/x86/platform/efi/efi.c +4 -0
+		efi_map_region_fixed(md);
+		size = md->num_pages << PAGE_SHIFT;
+		end = md->phys_addr + size;
+

arch/x86/platform/efi/efi.c ret: · line 998

Quoted 10 lines
arch/x86/platform/efi/efi.c +10 -0
+		systab = (u64) (unsigned long) efi_phys.systab;
+		if (md->phys_addr <= systab && systab < end) {
+			systab += md->virt_addr - md->phys_addr;
+			efi.systab =
+				(efi_system_table_t *) (unsigned long) systab;
+		}
+		if (efi_runtime_map) {
+			memcpy(p, md, memmap.desc_size);
+			p += memmap.desc_size;
+		}

Is this if () needed? Is it possible to enter the loop and have 'efi_runtime_map' be NULL?

Quoted 53 lines
arch/x86/platform/efi/efi.c +21 -11
+	}
+}
+
+/*
  * This function will switch the EFI runtime services to virtual mode.
  * Essentially, we look through the EFI memmap and map every region that
  * has the runtime attribute bit set in its memory descriptor into the
@@ -901,6 +1015,10 @@ ret:
  * so that we're in a different address space when calling a runtime
  * function. For function arguments passing we do copy the PGDs of the
  * kernel page table into ->trampoline_pgd prior to each call.
+ *
+ * Specially for kexec boot efi runtime maps in previous kernel should
+ * be passed in via setup_data. In that case runtime ranges will be mapped
+ * to fixed virtual addresses exactly same as the ones in previous kernel.
  */
 void __init efi_enter_virtual_mode(void)
 {
@@ -919,12 +1037,15 @@ void __init efi_enter_virtual_mode(void)
 		return;
 	}
 
-	efi_merge_regions();
-
-	new_memmap = efi_map_regions(&count);
-	if (!new_memmap) {
-		pr_err("Error reallocating memory, EFI runtime non-functional!\n");
-		return;
+	if (esdata)
+		efi_map_regions_fixed();
+	else {
+		efi_merge_regions();
+		new_memmap = efi_map_regions(&count);
+		if (!new_memmap) {
+			pr_err("Error reallocating memory, EFI runtime non-functional!\n");
+			return;
+		}
 	}
 
 	BUG_ON(!efi.systab);
@@ -932,11 +1053,17 @@ void __init efi_enter_virtual_mode(void)
 	efi_setup_page_tables();
 	efi_sync_low_kernel_mappings();
 
-	status = phys_efi_set_virtual_address_map(
-		memmap.desc_size * count,
-		memmap.desc_size,
-		memmap.desc_version,
-		(efi_memory_desc_t *)__pa(new_memmap));
+	if (esdata) {
+		status = EFI_SUCCESS;
+		early_iounmap(esdata, sizeof(struct efi_setup_data) +
+			      nr_efi_runtime_map *

arch/x86/platform/efi/efi.c void __init efi_enter_virtual_mode(void) · line 1069

Quoted 10 lines
arch/x86/platform/efi/efi.c +7 -0
+			      sizeof(efi_memory_desc_t));
+	} else
+		status = phys_efi_set_virtual_address_map(
+			memmap.desc_size * count,
+			memmap.desc_size,
+			memmap.desc_version,
+			(efi_memory_desc_t *)__pa(new_memmap));
 
 	if (status != EFI_SUCCESS) {
 		pr_alert("Unable to switch EFI into virtual mode "

Please fold the pr_alert() into the else branch so that you don't have to do the 'status = EFI_SUCCESS' dance.

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

Re: [PATCH v4 07/12] efi: passing kexec necessary efi data via setup_data

Quoted 44 lines · Matt Fleming
Quoted 26 lines · Dave Young

Add a new setup_data type SETUP_EFI for kexec use. Passing the saved fw_vendor, runtime, config tables and efi runtime mappings.

When entering virtual mode, directly mapping the efi runtime ragions which we passed in previously. And skip the step to call SetVirtualAddressMap.

Specially for HP z420 workstation it need another variable saving, it's the smbios physical address, the HP bios also update the SMBIOS address after entering virtual mode besides of the standard fw_vendor,runtime and config table.

Tested on ovmf+qemu, lenovo thinkpad, a dell laptop and an HP z420 workstation.

v2: refresh based on previous patch changes, code cleanup.
v3: use ioremap instead of phys_to_virt for esdata

---
 arch/x86/include/asm/efi.h            |  12 +++
 arch/x86/include/uapi/asm/bootparam.h |   1 +
 arch/x86/kernel/setup.c               |   3 +
 arch/x86/platform/efi/efi.c           | 161 ++++++++++++++++++++++++++++++----
 4 files changed, 160 insertions(+), 17 deletions(-)

[...]

arch/x86/platform/efi/efi.c static int __init setup_storage_paranoia(char *arg) · line 128

Quoted 10 lines
arch/x86/platform/efi/efi.c +10 -0
+void __init parse_efi_setup(u64 phys_addr, u32 data_len)
+{
+	int size;
+	struct setup_data *sdata;
+	u64 esdata_phys;
+
+	if (!efi_enabled(EFI_64BIT)) {
+		pr_warn("skipping setup_data on EFI 32BIT!");
+		return;
+	}

Hmm... this warning could be more informative. Perhaps something along the lines of,

Quoted 1 line

"SETUP_EFI not supported on 32-bit\n"

Will do

Quoted 25 lines

because the reason we skip is because it isn't supported.

Quoted 22 lines
arch/x86/platform/efi/efi.c +16 -0
+
+	sdata = early_memremap(phys_addr, data_len);
+	if (!sdata)
+		return;
+
+	size = data_len - sizeof(struct setup_data);
+
+	esdata_phys = phys_addr + sizeof(struct setup_data);
+
+	nr_efi_runtime_map = (size - sizeof(struct efi_setup_data)) /
+			sizeof(efi_memory_desc_t);
+	early_iounmap(sdata, data_len);
+
+	/* iounmap esdata in function efi_enter_virtual_mode */
+	esdata = early_memremap(esdata_phys, size);
+}
 
 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
 {
@@ -504,8 +531,12 @@ static int __init efi_systab_init(void *phys)
 		}
 
Quoted 7 lines

arch/x86/platform/efi/efi.c static int __init efi_systab_init(void *phys) · line 535

Quoted 5 lines
arch/x86/platform/efi/efi.c +2 -2
 		efi_systab.hdr = systab64->hdr;
-		efi_systab.fw_vendor = systab64->fw_vendor;
-		tmp |= systab64->fw_vendor;
+
+		if (esdata)

Could we name this something more explicit? 'efi_setup' perhaps?

Sure, just efi_setup_data is a bit long, will use efi_setup in next version.

Quoted 26 lines
Quoted 26 lines
arch/x86/platform/efi/efi.c +21 -0
@@ -631,6 +670,41 @@ static int __init efi_memmap_init(void)
 	return 0;
 }
 
+static int __init efi_reuse_config(u64 tables, int nr_tables)
+{
+	void *p, *tablep;
+	int i, sz;
+
+	if (!efi_enabled(EFI_64BIT))
+		return 0;
+
+	sz = sizeof(efi_config_table_64_t);
+
+	p = tablep = early_memremap(tables, nr_tables * sz);
+	if (!p) {
+		pr_err("Could not map Configuration table!\n");
+		return -ENOMEM;
+	}
+
+	for (i = 0; i < efi.systab->nr_tables; i++) {
+		efi_guid_t guid;
+
+		guid = ((efi_config_table_64_t *)p)->guid;
+
Quoted 8 lines

arch/x86/platform/efi/efi.c static int __init efi_memmap_init(void) · line 699

Quoted 6 lines
arch/x86/platform/efi/efi.c +6 -0
+		/*
+		HP z420 workstation smbios will be convert to
+		virtual address after enter virtual mode.
+		Thus in case kexec/kdump the physical address
+		will be passed in setup_data.
+		*/

This isn't the correct multi-line comment format,

Will update

Quoted 21 lines
		/*
		 * This is the preferred way to to write a multi-line
		 * comment.
		 */
Quoted 15 lines
arch/x86/platform/efi/efi.c +8 -0
+		if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
+			((efi_config_table_64_t *)p)->table = esdata->smbios;
+		p += sz;
+	}
+	early_iounmap(tablep, nr_tables * sz);
+	return 0;
+}
+
 void __init efi_init(void)
 {
 	efi_char16_t *c16;
@@ -676,6 +750,9 @@ void __init efi_init(void)
 		efi.systab->hdr.revision >> 16,
 		efi.systab->hdr.revision & 0xffff, vendor);
 
Quoted 6 lines

arch/x86/platform/efi/efi.c void __init efi_init(void) · line 755

Quoted 3 lines
arch/x86/platform/efi/efi.c +3 -0
+	if (esdata && esdata->smbios)
+		efi_reuse_config(efi.systab->tables, efi.systab->nr_tables);
+

Would it make sense to move the ->smbios check inside efi_reuse_config() in case we need to extend the number of tables in the future?

Good suggestion, will do.

Quoted 21 lines
Quoted 21 lines
arch/x86/platform/efi/efi.c +13 -0
 	if (efi_config_init(arch_tables))
 		return;
 
@@ -886,6 +963,43 @@ ret:
 }
 
 /*
+ * map efi regions which was passed via setup_data
+ * the virt_addr is a fixed addr which was used in
+ * 1st kernel of kexec boot.
+ */
+static void __init efi_map_regions_fixed(void)
+{
+	int i;
+	unsigned long size;
+	efi_memory_desc_t *md;
+	u64 end, systab;
+	void *p;
+
+	efi_runtime_map = kzalloc(nr_efi_runtime_map * memmap.desc_size,
Quoted 8 lines

arch/x86/platform/efi/efi.c ret: · line 984

Quoted 6 lines
arch/x86/platform/efi/efi.c +6 -0
+				GFP_KERNEL);
+	if (!efi_runtime_map)
+		pr_err("Out of memory, EFI runtime on nested kexec non-functional!\n");
+
+	for (i = 0, p = efi_runtime_map; i < nr_efi_runtime_map; i++) {
+		md = esdata->map + i;

Heh, I read that as 'esdata->map + 1' the first few times.

To make it more readable, I will change them like below:

p = efi_runtime_map;
md = efi_setup->map;

for (i = 0; i < nr_efi_runtime_map; i++) {
	[...]
	md += 1;
}
Quoted 10 lines
Quoted 10 lines
arch/x86/platform/efi/efi.c +9 -0
+		efi_map_region_fixed(md);
+		size = md->num_pages << PAGE_SHIFT;
+		end = md->phys_addr + size;
+
+		systab = (u64) (unsigned long) efi_phys.systab;
+		if (md->phys_addr <= systab && systab < end) {
+			systab += md->virt_addr - md->phys_addr;
+			efi.systab =
+				(efi_system_table_t *) (unsigned long) systab;
Quoted 8 lines

arch/x86/platform/efi/efi.c ret: · line 998

Quoted 5 lines
arch/x86/platform/efi/efi.c +5 -0
+		}
+		if (efi_runtime_map) {
+			memcpy(p, md, memmap.desc_size);
+			p += memmap.desc_size;
+		}

Is this if () needed? Is it possible to enter the loop and have 'efi_runtime_map' be NULL?

Yes, it is needed. if efi_runtime_map kmalloc fails I only print error, do not return so kernel can still boot, just kexec on efi will not work that has been put in the error message.

Quoted 62 lines
Quoted 62 lines
arch/x86/platform/efi/efi.c +28 -11
+	}
+}
+
+/*
  * This function will switch the EFI runtime services to virtual mode.
  * Essentially, we look through the EFI memmap and map every region that
  * has the runtime attribute bit set in its memory descriptor into the
@@ -901,6 +1015,10 @@ ret:
  * so that we're in a different address space when calling a runtime
  * function. For function arguments passing we do copy the PGDs of the
  * kernel page table into ->trampoline_pgd prior to each call.
+ *
+ * Specially for kexec boot efi runtime maps in previous kernel should
+ * be passed in via setup_data. In that case runtime ranges will be mapped
+ * to fixed virtual addresses exactly same as the ones in previous kernel.
  */
 void __init efi_enter_virtual_mode(void)
 {
@@ -919,12 +1037,15 @@ void __init efi_enter_virtual_mode(void)
 		return;
 	}
 
-	efi_merge_regions();
-
-	new_memmap = efi_map_regions(&count);
-	if (!new_memmap) {
-		pr_err("Error reallocating memory, EFI runtime non-functional!\n");
-		return;
+	if (esdata)
+		efi_map_regions_fixed();
+	else {
+		efi_merge_regions();
+		new_memmap = efi_map_regions(&count);
+		if (!new_memmap) {
+			pr_err("Error reallocating memory, EFI runtime non-functional!\n");
+			return;
+		}
 	}
 
 	BUG_ON(!efi.systab);
@@ -932,11 +1053,17 @@ void __init efi_enter_virtual_mode(void)
 	efi_setup_page_tables();
 	efi_sync_low_kernel_mappings();
 
-	status = phys_efi_set_virtual_address_map(
-		memmap.desc_size * count,
-		memmap.desc_size,
-		memmap.desc_version,
-		(efi_memory_desc_t *)__pa(new_memmap));
+	if (esdata) {
+		status = EFI_SUCCESS;
+		early_iounmap(esdata, sizeof(struct efi_setup_data) +
+			      nr_efi_runtime_map *
+			      sizeof(efi_memory_desc_t));
+	} else
+		status = phys_efi_set_virtual_address_map(
+			memmap.desc_size * count,
+			memmap.desc_size,
+			memmap.desc_version,
+			(efi_memory_desc_t *)__pa(new_memmap));
 
Quoted 5 lines

arch/x86/platform/efi/efi.c void __init efi_enter_virtual_mode(void) · line 1069

Quoted 2 lines
arch/x86/platform/efi/efi.c +0 -0
 	if (status != EFI_SUCCESS) {
 		pr_alert("Unable to switch EFI into virtual mode "

Please fold the pr_alert() into the else branch so that you don't have to do the 'status = EFI_SUCCESS' dance.

Good suggestion, will do.

Thanks for review
Dave

#29 lore raw

Re: [PATCH v4 07/12] efi: passing kexec necessary efi data via setup_data

Quoted 8 lines · Dave Young

To make it more readable, I will change them like below:

p = efi_runtime_map;
md = efi_setup->map;

for (i = 0; i < nr_efi_runtime_map; i++) {
	[...]
	md += 1;
}

Actually, md++ is the canonical way to write this.

Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
arch/x86/platform/efi/efi.c +9 -0
+		efi_map_region_fixed(md);
+		size = md->num_pages << PAGE_SHIFT;
+		end = md->phys_addr + size;
+
+		systab = (u64) (unsigned long) efi_phys.systab;
+		if (md->phys_addr <= systab && systab < end) {
+			systab += md->virt_addr - md->phys_addr;
+			efi.systab =
+				(efi_system_table_t *) (unsigned long) systab;
Quoted 8 lines

arch/x86/platform/efi/efi.c ret: · line 998

Quoted 5 lines
arch/x86/platform/efi/efi.c +5 -0
+		}
+		if (efi_runtime_map) {
+			memcpy(p, md, memmap.desc_size);
+			p += memmap.desc_size;
+		}

Is this if () needed? Is it possible to enter the loop and have 'efi_runtime_map' be NULL?

Quoted 3 lines

Yes, it is needed. if efi_runtime_map kmalloc fails I only print error, do not return so kernel can still boot, just kexec on efi will not work that has been put in the error message.

OK. On second thought, is there any way to turn the above code into a call to efi_save_runtime_map()? Because you've basically duplicated that code and I can definitely envisage the two code paths fragmenting over time, e.g. when someone makes changes to one but not the other.

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

Re: [PATCH v4 07/12] efi: passing kexec necessary efi data via setup_data

Quoted 11 lines · Matt Fleming
Quoted 8 lines · Dave Young

To make it more readable, I will change them like below:

p = efi_runtime_map;
md = efi_setup->map;

for (i = 0; i < nr_efi_runtime_map; i++) {
	[...]
	md += 1;
}

Actually, md++ is the canonical way to write this.

Ok.

Quoted 20 lines
Quoted 20 lines
Quoted 19 lines
Quoted 6 lines
arch/x86/platform/efi/efi.c +4 -0
+		efi_map_region_fixed(md);
+		size = md->num_pages << PAGE_SHIFT;
+		end = md->phys_addr + size;
+

arch/x86/platform/efi/efi.c ret: · line 998

Quoted 10 lines
arch/x86/platform/efi/efi.c +10 -0
+		systab = (u64) (unsigned long) efi_phys.systab;
+		if (md->phys_addr <= systab && systab < end) {
+			systab += md->virt_addr - md->phys_addr;
+			efi.systab =
+				(efi_system_table_t *) (unsigned long) systab;
+		}
+		if (efi_runtime_map) {
+			memcpy(p, md, memmap.desc_size);
+			p += memmap.desc_size;
+		}

Is this if () needed? Is it possible to enter the loop and have 'efi_runtime_map' be NULL?

Quoted 8 lines
Quoted 3 lines

Yes, it is needed. if efi_runtime_map kmalloc fails I only print error, do not return so kernel can still boot, just kexec on efi will not work that has been put in the error message.

OK. On second thought, is there any way to turn the above code into a call to efi_save_runtime_map()? Because you've basically duplicated that code and I can definitely envisage the two code paths fragmenting over time, e.g. when someone makes changes to one but not the other.

OK, will consider to reuse the function.

Thanks
Dave

maintainer
#31 lore raw

Re: [PATCH v4 07/12] efi: passing kexec necessary efi data via setup_data

In reply to Dave Young
Quoted 10 lines · Dave Young

Add a new setup_data type SETUP_EFI for kexec use. Passing the saved fw_vendor, runtime, config tables and efi runtime mappings.

When entering virtual mode, directly mapping the efi runtime ragions which we passed in previously. And skip the step to call SetVirtualAddressMap.

Specially for HP z420 workstation it need another variable saving,

Why the special handling? Does that mean, this is going to be the case for other HP UEFI implementations too?

Quoted 23 lines

it's the smbios physical address, the HP bios
also update the SMBIOS address after entering virtual mode besides of the standard fw_vendor,runtime and config table.

Tested on ovmf+qemu, lenovo thinkpad, a dell laptop and an HP z420 workstation.

v2: refresh based on previous patch changes, code cleanup.
v3: use ioremap instead of phys_to_virt for esdata

---
 arch/x86/include/asm/efi.h            |  12 +++
 arch/x86/include/uapi/asm/bootparam.h |   1 +
 arch/x86/kernel/setup.c               |   3 +
 arch/x86/platform/efi/efi.c           | 161 ++++++++++++++++++++++++++++++----
 4 files changed, 160 insertions(+), 17 deletions(-)
arch/x86/include/asm/efi.h +0 -0
index 9fbaeb2..73d5643 100644

arch/x86/include/asm/efi.h extern void efi_sync_low_kernel_mappings(void); · line 141

Quoted 9 lines
arch/x86/include/asm/efi.h +6 -0
@@ -133,6 +133,18 @@ extern void efi_sync_low_kernel_mappings(void);
 extern void efi_setup_page_tables(void);
 extern void __init old_map_region(efi_memory_desc_t *md);
 
+struct efi_setup_data {
+	u64 fw_vendor;
+	u64 runtime;
+	u64 tables;
+	u64 smbios;
+	u64 reserved[8];

What's that for?

Quoted 9 lines
arch/x86/include/asm/efi.h +6 -0
+	efi_memory_desc_t map[0];
+};
+
+extern void parse_efi_setup(u64 phys_addr, u32 data_len);
+extern struct efi_setup_data *esdata;
+
 #ifdef CONFIG_EFI
 
 static inline bool efi_is_native(void)

[ … ]

Quoted 5 lines
arch/x86/platform/efi/efi.c +0 -0
index c3a2aaa..fafeb40 100644

arch/x86/platform/efi/efi.c static int __init efi_systab_init(void *phys) · line 538

Quoted 10 lines
arch/x86/platform/efi/efi.c +5 -2
@@ -504,8 +531,12 @@ static int __init efi_systab_init(void *phys)
 		}
 
 		efi_systab.hdr = systab64->hdr;
-		efi_systab.fw_vendor = systab64->fw_vendor;
-		tmp |= systab64->fw_vendor;
+
+		if (esdata)
+			efi_systab.fw_vendor = (unsigned long)esdata->fw_vendor;
+		else
+			efi_systab.fw_vendor = systab64->fw_vendor;
		efi_systab.fw_vendor = esdata ? (unsigned long)esdata->fw_vendor
					      : systab64->fw_vendor;
Quoted 6 lines
arch/x86/platform/efi/efi.c +1 -0
+		tmp |= efi_systab.fw_vendor;
 		efi_systab.fw_revision = systab64->fw_revision;
 		efi_systab.con_in_handle = systab64->con_in_handle;
 		tmp |= systab64->con_in_handle;
@@ -519,13 +550,21 @@ static int __init efi_systab_init(void *phys)
 		tmp |= systab64->stderr_handle;

arch/x86/platform/efi/efi.c static int __init efi_systab_init(void *phys) · line 558

Quoted 10 lines
arch/x86/platform/efi/efi.c +6 -2
 		efi_systab.stderr = systab64->stderr;
 		tmp |= systab64->stderr;
-		efi_systab.runtime = (void *)(unsigned long)systab64->runtime;
-		tmp |= systab64->runtime;
+		if (esdata)
+			efi_systab.runtime =
+				(void *)(unsigned long)esdata->runtime;
+		else
+			efi_systab.runtime =
+				(void *)(unsigned long)systab64->runtime;

Ditto. Which would take care of these linebreaks which are ugly.

arch/x86/platform/efi/efi.c static int __init efi_systab_init(void *phys) · line 566

Quoted 10 lines
arch/x86/platform/efi/efi.c +5 -2
+		tmp |= (unsigned long)efi_systab.runtime;
 		efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
 		tmp |= systab64->boottime;
 		efi_systab.nr_tables = systab64->nr_tables;
-		efi_systab.tables = systab64->tables;
-		tmp |= systab64->tables;
+		if (esdata)
+			efi_systab.tables = (unsigned long)esdata->tables;
+		else
+			efi_systab.tables = systab64->tables;

Ditto.

arch/x86/platform/efi/efi.c static int __init efi_memmap_init(void) · line 673

Quoted 9 lines
arch/x86/platform/efi/efi.c +2 -0
+		tmp |= efi_systab.tables;
 
 		early_iounmap(systab64, sizeof(*systab64));
 #ifdef CONFIG_X86_32
@@ -631,6 +670,41 @@ static int __init efi_memmap_init(void)
 	return 0;
 }
 
+static int __init efi_reuse_config(u64 tables, int nr_tables)

Static function - no need for "efi_" prefix.

Quoted 15 lines
arch/x86/platform/efi/efi.c +15 -0
+{
+	void *p, *tablep;
+	int i, sz;
+
+	if (!efi_enabled(EFI_64BIT))
+		return 0;
+
+	sz = sizeof(efi_config_table_64_t);
+
+	p = tablep = early_memremap(tables, nr_tables * sz);
+	if (!p) {
+		pr_err("Could not map Configuration table!\n");
+		return -ENOMEM;
+	}
+

arch/x86/platform/efi/efi.c static int __init efi_memmap_init(void) · line 698

Quoted 10 lines
arch/x86/platform/efi/efi.c +10 -0
+	for (i = 0; i < efi.systab->nr_tables; i++) {
+		efi_guid_t guid;
+
+		guid = ((efi_config_table_64_t *)p)->guid;
+
+		/*
+		HP z420 workstation smbios will be convert to
+		virtual address after enter virtual mode.
+		Thus in case kexec/kdump the physical address
+		will be passed in setup_data.

Is that what the commit message above says? I'm having a hard time parsing this text.

arch/x86/platform/efi/efi.c static int __init efi_memmap_init(void) · line 701

Quoted 3 lines
arch/x86/platform/efi/efi.c +3 -0
+		*/
+		if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
+			((efi_config_table_64_t *)p)->table = esdata->smbios;

...and yet we do this for *every* UEFI box. Why not HP only?

Quoted 20 lines
arch/x86/platform/efi/efi.c +9 -0
+		p += sz;
+	}
+	early_iounmap(tablep, nr_tables * sz);
+	return 0;
+}
+
 void __init efi_init(void)
 {
 	efi_char16_t *c16;
@@ -676,6 +750,9 @@ void __init efi_init(void)
 		efi.systab->hdr.revision >> 16,
 		efi.systab->hdr.revision & 0xffff, vendor);
 
+	if (esdata && esdata->smbios)
+		efi_reuse_config(efi.systab->tables, efi.systab->nr_tables);
+
 	if (efi_config_init(arch_tables))
 		return;
 

arch/x86/platform/efi/efi.c ret: · line 969

Quoted 7 lines
arch/x86/platform/efi/efi.c +4 -0
@@ -886,6 +963,43 @@ ret:
 }
 
 /*
+ * map efi regions which was passed via setup_data
+ * the virt_addr is a fixed addr which was used in
+ * 1st kernel of kexec boot.
+ */

Comment to 80 cols pls.

arch/x86/platform/efi/efi.c ret: · line 970

Quoted 1 line
arch/x86/platform/efi/efi.c +1 -0
+static void __init efi_map_regions_fixed(void)

Also no need for "efi_" prefix here.

arch/x86/platform/efi/efi.c ret: · line 979

Quoted 9 lines
arch/x86/platform/efi/efi.c +9 -0
+{
+	int i;
+	unsigned long size;
+	efi_memory_desc_t *md;
+	u64 end, systab;
+	void *p;
+
+	efi_runtime_map = kzalloc(nr_efi_runtime_map * memmap.desc_size,
+				GFP_KERNEL);

Arg alignment.

arch/x86/platform/efi/efi.c ret: · line 985

Quoted 6 lines
arch/x86/platform/efi/efi.c +6 -0
+	if (!efi_runtime_map)
+		pr_err("Out of memory, EFI runtime on nested kexec non-functional!\n");
+
+	for (i = 0, p = efi_runtime_map; i < nr_efi_runtime_map; i++) {
+		md = esdata->map + i;
+		efi_map_region_fixed(md);

Gaah, this function should probably have a retval which signalizes success/failure. For that I should probably teach __map_region to do that too. On the TODO list.

arch/x86/platform/efi/efi.c ret: · line 993

Quoted 8 lines
arch/x86/platform/efi/efi.c +8 -0
+		size = md->num_pages << PAGE_SHIFT;
+		end = md->phys_addr + size;
+
+		systab = (u64) (unsigned long) efi_phys.systab;
+		if (md->phys_addr <= systab && systab < end) {
+			systab += md->virt_addr - md->phys_addr;
+			efi.systab =
+				(efi_system_table_t *) (unsigned long) systab;

CHECK: No space is necessary after a cast
#219: FILE: arch/x86/platform/efi/efi.c:993:
+ efi.systab =
+ (efi_system_table_t *) (unsigned long) systab;

And also, those broken lines are ugly. Just let it stick out over 80 cols.

Quoted 13 lines
arch/x86/platform/efi/efi.c +9 -0
+		}
+		if (efi_runtime_map) {
+			memcpy(p, md, memmap.desc_size);
+			p += memmap.desc_size;
+		}
+	}
+}
+
+/*
  * This function will switch the EFI runtime services to virtual mode.
  * Essentially, we look through the EFI memmap and map every region that
  * has the runtime attribute bit set in its memory descriptor into the

arch/x86/platform/efi/efi.c ret: · line 1019

Quoted 5 lines
arch/x86/platform/efi/efi.c +2 -0
@@ -901,6 +1015,10 @@ ret:
  * so that we're in a different address space when calling a runtime
  * function. For function arguments passing we do copy the PGDs of the
  * kernel page table into ->trampoline_pgd prior to each call.
+ *
+ * Specially for kexec boot efi runtime maps in previous kernel should
			  boot, ...

arch/x86/platform/efi/efi.c ret: · line 1020

Quoted 1 line
arch/x86/platform/efi/efi.c +1 -0
+ * be passed in via setup_data. In that case runtime ranges will be mapped
					   case, ...

arch/x86/platform/efi/efi.c ret: · line 1021

Quoted 1 line
arch/x86/platform/efi/efi.c +1 -0
+ * to fixed virtual addresses exactly same as the ones in previous kernel.

"... to the same virtual addresses as the first kernel."

Quoted 12 lines
arch/x86/platform/efi/efi.c +0 -5
  */
 void __init efi_enter_virtual_mode(void)
 {
@@ -919,12 +1037,15 @@ void __init efi_enter_virtual_mode(void)
 		return;
 	}
 
-	efi_merge_regions();
-
-	new_memmap = efi_map_regions(&count);
-	if (!new_memmap) {
-		pr_err("Error reallocating memory, EFI runtime non-functional!\n");

arch/x86/platform/efi/efi.c void __init efi_enter_virtual_mode(void) · line 1048

Quoted 10 lines
arch/x86/platform/efi/efi.c +9 -1
-		return;
+	if (esdata)
+		efi_map_regions_fixed();
+	else {
+		efi_merge_regions();
+		new_memmap = efi_map_regions(&count);
+		if (!new_memmap) {
+			pr_err("Error reallocating memory, EFI runtime non-functional!\n");
+			return;
+		}

CHECK: braces {} should be used on all arms of this statement #253: FILE: arch/x86/platform/efi/efi.c:1040:
+ if (esdata)
[...]
+ else {
[...]

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
author
#32 lore raw

Re: [PATCH v4 07/12] efi: passing kexec necessary efi data via setup_data

Quoted 9 lines · Borislav Petkov
Quoted 8 lines · Dave Young

Add a new setup_data type SETUP_EFI for kexec use. Passing the saved fw_vendor, runtime, config tables and efi runtime mappings.

When entering virtual mode, directly mapping the efi runtime ragions which we passed in previously. And skip the step to call SetVirtualAddressMap.

Quoted 5 lines
Quoted 2 lines

Specially for HP z420 workstation it need another variable saving,

Why the special handling? Does that mean, this is going to be the case for other HP UEFI implementations too?

I have only one HP machine for testing, Maybe Toshi can help to verify on other machines. Just comment out the function efi_reuse_config to see if kexec kernel panic.

Quoted 27 lines
Quoted 27 lines

it's the smbios physical address, the HP bios
also update the SMBIOS address after entering virtual mode besides of the standard fw_vendor,runtime and config table.

Tested on ovmf+qemu, lenovo thinkpad, a dell laptop and an HP z420 workstation.

v2: refresh based on previous patch changes, code cleanup.
v3: use ioremap instead of phys_to_virt for esdata

---
 arch/x86/include/asm/efi.h            |  12 +++
 arch/x86/include/uapi/asm/bootparam.h |   1 +
 arch/x86/kernel/setup.c               |   3 +
 arch/x86/platform/efi/efi.c           | 161 ++++++++++++++++++++++++++++++----
 4 files changed, 160 insertions(+), 17 deletions(-)
arch/x86/include/asm/efi.h +0 -0
index 9fbaeb2..73d5643 100644
@@ -133,6 +133,18 @@ extern void efi_sync_low_kernel_mappings(void);
 extern void efi_setup_page_tables(void);
 extern void __init old_map_region(efi_memory_desc_t *md);
 
Quoted 8 lines

arch/x86/include/asm/efi.h extern void efi_sync_low_kernel_mappings(void); · line 141

Quoted 6 lines
arch/x86/include/asm/efi.h +6 -0
+struct efi_setup_data {
+	u64 fw_vendor;
+	u64 runtime;
+	u64 tables;
+	u64 smbios;
+	u64 reserved[8];

What's that for?

That's reserved for future extension use, who knows if we will need to pass other fields in the future.

Quoted 20 lines
Quoted 10 lines
arch/x86/include/asm/efi.h +6 -0
+	efi_memory_desc_t map[0];
+};
+
+extern void parse_efi_setup(u64 phys_addr, u32 data_len);
+extern struct efi_setup_data *esdata;
+
 #ifdef CONFIG_EFI
 
 static inline bool efi_is_native(void)

[ … ]

Quoted 7 lines
arch/x86/platform/efi/efi.c +0 -0
index c3a2aaa..fafeb40 100644
@@ -504,8 +531,12 @@ static int __init efi_systab_init(void *phys)
 		}
 
Quoted 11 lines

arch/x86/platform/efi/efi.c static int __init efi_systab_init(void *phys) · line 538

Quoted 8 lines
arch/x86/platform/efi/efi.c +5 -2
 		efi_systab.hdr = systab64->hdr;
-		efi_systab.fw_vendor = systab64->fw_vendor;
-		tmp |= systab64->fw_vendor;
+
+		if (esdata)
+			efi_systab.fw_vendor = (unsigned long)esdata->fw_vendor;
+		else
+			efi_systab.fw_vendor = systab64->fw_vendor;
		efi_systab.fw_vendor = esdata ? (unsigned long)esdata->fw_vendor
					      : systab64->fw_vendor;

Ok, will update.

Quoted 11 lines
Quoted 11 lines
arch/x86/platform/efi/efi.c +1 -2
+		tmp |= efi_systab.fw_vendor;
 		efi_systab.fw_revision = systab64->fw_revision;
 		efi_systab.con_in_handle = systab64->con_in_handle;
 		tmp |= systab64->con_in_handle;
@@ -519,13 +550,21 @@ static int __init efi_systab_init(void *phys)
 		tmp |= systab64->stderr_handle;
 		efi_systab.stderr = systab64->stderr;
 		tmp |= systab64->stderr;
-		efi_systab.runtime = (void *)(unsigned long)systab64->runtime;
-		tmp |= systab64->runtime;
Quoted 8 lines

arch/x86/platform/efi/efi.c static int __init efi_systab_init(void *phys) · line 558

Quoted 6 lines
arch/x86/platform/efi/efi.c +6 -0
+		if (esdata)
+			efi_systab.runtime =
+				(void *)(unsigned long)esdata->runtime;
+		else
+			efi_systab.runtime =
+				(void *)(unsigned long)systab64->runtime;

Ditto. Which would take care of these linebreaks which are ugly.

Will do.

Quoted 13 lines

arch/x86/platform/efi/efi.c static int __init efi_systab_init(void *phys) · line 566

Quoted 11 lines
arch/x86/platform/efi/efi.c +5 -2
+		tmp |= (unsigned long)efi_systab.runtime;
 		efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
 		tmp |= systab64->boottime;
 		efi_systab.nr_tables = systab64->nr_tables;
-		efi_systab.tables = systab64->tables;
-		tmp |= systab64->tables;
+		if (esdata)
+			efi_systab.tables = (unsigned long)esdata->tables;
+		else
+			efi_systab.tables = systab64->tables;

Ditto.

Will do

Quoted 9 lines
Quoted 9 lines
arch/x86/platform/efi/efi.c +1 -0
+		tmp |= efi_systab.tables;
 
 		early_iounmap(systab64, sizeof(*systab64));
 #ifdef CONFIG_X86_32
@@ -631,6 +670,41 @@ static int __init efi_memmap_init(void)
 	return 0;
 }
 
Quoted 3 lines

arch/x86/platform/efi/efi.c static int __init efi_memmap_init(void) · line 673

Quoted 1 line
arch/x86/platform/efi/efi.c +1 -0
+static int __init efi_reuse_config(u64 tables, int nr_tables)

Static function - no need for "efi_" prefix.

Ok. I'm not very satisfied with the function name, any better suggestion?

reuse_config
use_old_config_phys_addr
update_config_entry_with_phys_addr
...

Quoted 21 lines
Quoted 21 lines
arch/x86/platform/efi/efi.c +20 -0
+{
+	void *p, *tablep;
+	int i, sz;
+
+	if (!efi_enabled(EFI_64BIT))
+		return 0;
+
+	sz = sizeof(efi_config_table_64_t);
+
+	p = tablep = early_memremap(tables, nr_tables * sz);
+	if (!p) {
+		pr_err("Could not map Configuration table!\n");
+		return -ENOMEM;
+	}
+
+	for (i = 0; i < efi.systab->nr_tables; i++) {
+		efi_guid_t guid;
+
+		guid = ((efi_config_table_64_t *)p)->guid;
+
Quoted 8 lines

arch/x86/platform/efi/efi.c static int __init efi_memmap_init(void) · line 698

Quoted 5 lines
arch/x86/platform/efi/efi.c +5 -0
+		/*
+		HP z420 workstation smbios will be convert to
+		virtual address after enter virtual mode.
+		Thus in case kexec/kdump the physical address
+		will be passed in setup_data.

Is that what the commit message above says? I'm having a hard time parsing this text.

Since Matt suggest to extend the function for other possible field other than smbios, so I would like to move comment to the front of the function like below:
/*
* For kexec kernel there's some special config table entries which will be
* converted to virtual addresses after entering virtual mode. In kexec kernel
* we need the physical addresses instead, thus passing them via setup_data
* and update the entries to physical addresses in this function. *
* Currently only handles smbios which is necessary for HP z420. */
static int __init efi_reuse_config(u64 tables, int nr_tables)

Quoted 6 lines

arch/x86/platform/efi/efi.c static int __init efi_memmap_init(void) · line 701

Quoted 4 lines
arch/x86/platform/efi/efi.c +3 -0
+		*/
+		if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
+			((efi_config_table_64_t *)p)->table = esdata->smbios;

...and yet we do this for *every* UEFI box. Why not HP only?

Is there any idea in your mind how to add the code for HP only?

Quoted 23 lines
Quoted 23 lines
arch/x86/platform/efi/efi.c +9 -0
+		p += sz;
+	}
+	early_iounmap(tablep, nr_tables * sz);
+	return 0;
+}
+
 void __init efi_init(void)
 {
 	efi_char16_t *c16;
@@ -676,6 +750,9 @@ void __init efi_init(void)
 		efi.systab->hdr.revision >> 16,
 		efi.systab->hdr.revision & 0xffff, vendor);
 
+	if (esdata && esdata->smbios)
+		efi_reuse_config(efi.systab->tables, efi.systab->nr_tables);
+
 	if (efi_config_init(arch_tables))
 		return;
 
@@ -886,6 +963,43 @@ ret:
 }
 
Quoted 7 lines

arch/x86/platform/efi/efi.c ret: · line 969

Quoted 5 lines
arch/x86/platform/efi/efi.c +4 -0
 /*
+ * map efi regions which was passed via setup_data
+ * the virt_addr is a fixed addr which was used in
+ * 1st kernel of kexec boot.
+ */

Comment to 80 cols pls.

Will do

Quoted 4 lines

arch/x86/platform/efi/efi.c ret: · line 970

Quoted 2 lines
arch/x86/platform/efi/efi.c +1 -0
+static void __init efi_map_regions_fixed(void)

Also no need for "efi_" prefix here.

Ok.

Quoted 12 lines

arch/x86/platform/efi/efi.c ret: · line 979

Quoted 10 lines
arch/x86/platform/efi/efi.c +9 -0
+{
+	int i;
+	unsigned long size;
+	efi_memory_desc_t *md;
+	u64 end, systab;
+	void *p;
+
+	efi_runtime_map = kzalloc(nr_efi_runtime_map * memmap.desc_size,
+				GFP_KERNEL);

Arg alignment.

Will do.

Quoted 11 lines

arch/x86/platform/efi/efi.c ret: · line 985

Quoted 7 lines
arch/x86/platform/efi/efi.c +6 -0
+	if (!efi_runtime_map)
+		pr_err("Out of memory, EFI runtime on nested kexec non-functional!\n");
+
+	for (i = 0, p = efi_runtime_map; i < nr_efi_runtime_map; i++) {
+		md = esdata->map + i;
+		efi_map_region_fixed(md);

Gaah, this function should probably have a retval which signalizes success/failure. For that I should probably teach __map_region to do that too. On the TODO list.

Yes, __map_region return an error code will be better. Will you send a patch for __map_region? or I can add one more patch along with next version.

Quoted 12 lines

arch/x86/platform/efi/efi.c ret: · line 993

Quoted 9 lines
arch/x86/platform/efi/efi.c +8 -0
+		size = md->num_pages << PAGE_SHIFT;
+		end = md->phys_addr + size;
+
+		systab = (u64) (unsigned long) efi_phys.systab;
+		if (md->phys_addr <= systab && systab < end) {
+			systab += md->virt_addr - md->phys_addr;
+			efi.systab =
+				(efi_system_table_t *) (unsigned long) systab;

CHECK: No space is necessary after a cast
#219: FILE: arch/x86/platform/efi/efi.c:993:

Will fix

Quoted 5 lines
arch/x86/platform/efi/efi.c +2 -0
+                       efi.systab =
+                               (efi_system_table_t *) (unsigned long) systab;

And also, those broken lines are ugly. Just let it stick out over 80 cols.

Sure.

Quoted 13 lines
Quoted 13 lines
arch/x86/platform/efi/efi.c +9 -0
+		}
+		if (efi_runtime_map) {
+			memcpy(p, md, memmap.desc_size);
+			p += memmap.desc_size;
+		}
+	}
+}
+
+/*
  * This function will switch the EFI runtime services to virtual mode.
  * Essentially, we look through the EFI memmap and map every region that
  * has the runtime attribute bit set in its memory descriptor into the
Quoted 8 lines

arch/x86/platform/efi/efi.c ret: · line 1019

Quoted 6 lines
arch/x86/platform/efi/efi.c +2 -0
@@ -901,6 +1015,10 @@ ret:
  * so that we're in a different address space when calling a runtime
  * function. For function arguments passing we do copy the PGDs of the
  * kernel page table into ->trampoline_pgd prior to each call.
+ *
+ * Specially for kexec boot efi runtime maps in previous kernel should
			  boot, ...

Will update.

Quoted 8 lines

arch/x86/platform/efi/efi.c ret: · line 1020

Quoted 2 lines
arch/x86/platform/efi/efi.c +1 -0
+ * be passed in via setup_data. In that case runtime ranges will be mapped
					   case, ...

arch/x86/platform/efi/efi.c ret: · line 1021

Quoted 1 line
arch/x86/platform/efi/efi.c +1 -0
+ * to fixed virtual addresses exactly same as the ones in previous kernel.

"... to the same virtual addresses as the first kernel."

Will do.

Quoted 18 lines
Quoted 18 lines
arch/x86/platform/efi/efi.c +4 -6
  */
 void __init efi_enter_virtual_mode(void)
 {
@@ -919,12 +1037,15 @@ void __init efi_enter_virtual_mode(void)
 		return;
 	}
 
-	efi_merge_regions();
-
-	new_memmap = efi_map_regions(&count);
-	if (!new_memmap) {
-		pr_err("Error reallocating memory, EFI runtime non-functional!\n");
-		return;
+	if (esdata)
+		efi_map_regions_fixed();
+	else {
+		efi_merge_regions();
Quoted 8 lines

arch/x86/platform/efi/efi.c void __init efi_enter_virtual_mode(void) · line 1048

Quoted 5 lines
arch/x86/platform/efi/efi.c +5 -0
+		new_memmap = efi_map_regions(&count);
+		if (!new_memmap) {
+			pr_err("Error reallocating memory, EFI runtime non-functional!\n");
+			return;
+		}

CHECK: braces {} should be used on all arms of this statement #253: FILE: arch/x86/platform/efi/efi.c:1040:

Ok.

arch/x86/platform/efi/efi.c void __init efi_enter_virtual_mode(void) · line 1042

Quoted 5 lines
arch/x86/platform/efi/efi.c +2 -0
+       if (esdata)
[...]
+       else {
[...]

Thanks for carefully review
Dave

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

maintainer
#33 lore raw

Re: [PATCH v4 07/12] efi: passing kexec necessary efi data via setup_data

Quoted 2 lines · Dave Young

That's reserved for future extension use, who knows if we will need to pass other fields in the future.

Hrrmmm, 8*64 = 64 Bytes?? And you can't change it later because of the situation where machines might be using older kexec-tools?

Quoted 8 lines
Quoted 1 line

Static function - no need for "efi_" prefix.

Ok. I'm not very satisfied with the function name, any better suggestion?

reuse_config
use_old_config_phys_addr
update_config_entry_with_phys_addr

Looks like we're applying quirks so apply_quirks()?

And those kexec-specific quirks need to be behind CONFIG_KEXEC too, btw.

Quoted 11 lines

Since Matt suggest to extend the function for other possible field other than smbios, so I would like to move comment to the front of the function like below:
/*
* For kexec kernel there's some special config table entries which will be
* converted to virtual addresses after entering virtual mode. In kexec kernel
* we need the physical addresses instead, thus passing them via setup_data
* and update the entries to physical addresses in this function. *
* Currently only handles smbios which is necessary for HP z420. */

That's better.

Quoted 1 line

Is there any idea in your mind how to add the code for HP only?

DMI?

Quoted 3 lines

Yes, __map_region return an error code will be better. Will you send a patch for __map_region? or I can add one more patch along with next version.

I was going to wait for your patches to settle down first so that I don't disturb your work.

But if you want to fix this, I certainly wouldn't want to stop you :) but please do so in a prepatch before your series which addresses only this issue.

Thanks.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
author
#34 lore raw

Re: [PATCH v4 07/12] efi: passing kexec necessary efi data via setup_data

Quoted 6 lines · Borislav Petkov
Quoted 2 lines · Dave Young

That's reserved for future extension use, who knows if we will need to pass other fields in the future.

Hrrmmm, 8*64 = 64 Bytes?? And you can't change it later because of the situation where machines might be using older kexec-tools?

Right, compatibility is the reason.

Quoted 11 lines
Quoted 9 lines
Quoted 2 lines

Static function - no need for "efi_" prefix.

Ok. I'm not very satisfied with the function name, any better suggestion?

reuse_config
use_old_config_phys_addr
update_config_entry_with_phys_addr

Looks like we're applying quirks so apply_quirks()?

Ok, it looks better, will use apply_quirks, thank you.

Quoted 2 lines

And those kexec-specific quirks need to be behind CONFIG_KEXEC too, btw.

All these setup_data passing, remapping etc. is mostly for kexec, add a lot of contiditional #if #endif makes the code a mess. I would prefer to not do this if you are not strongly object.

Quoted 13 lines
Quoted 12 lines

Since Matt suggest to extend the function for other possible field other than smbios, so I would like to move comment to the front of the function like below:
/*
* For kexec kernel there's some special config table entries which will be
* converted to virtual addresses after entering virtual mode. In kexec kernel
* we need the physical addresses instead, thus passing them via setup_data
* and update the entries to physical addresses in this function. *
* Currently only handles smbios which is necessary for HP z420. */

Quoted 5 lines

That's better.

Quoted 1 line

Is there any idea in your mind how to add the code for HP only?

DMI?

I'm confused how to connect this with DMI..

Quoted 11 lines
Quoted 4 lines

Yes, __map_region return an error code will be better. Will you send a patch for __map_region? or I can add one more patch along with next version.

I was going to wait for your patches to settle down first so that I don't disturb your work.

But if you want to fix this, I certainly wouldn't want to stop you :) but please do so in a prepatch before your series which addresses only this issue.

Ok, so I will leave this to you after this series settle down.

Thanks
Dave

maintainer
#35 lore raw

Re: [PATCH v4 07/12] efi: passing kexec necessary efi data via setup_data

Quoted 3 lines · Dave Young

All these setup_data passing, remapping etc. is mostly for kexec, add a lot of contiditional #if #endif makes the code a mess. I would prefer to not do this if you are not strongly object.

Right, having efi_kexec.c could solve all that, see my other mail.

Quoted 1 line

I'm confused how to connect this with DMI..

This HP platform - like any other platform out there - has specific DMI strings identifying it. Look for examples calling dmi_check_system().

Quoted 1 line

Ok, so I will leave this to you after this series settle down.

Ok,

/me puts it on the TODO list :)

Thanks.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
#36 lore raw

Re: [PATCH v4 07/12] efi: passing kexec necessary efi data via setup_data

In reply to Dave Young
Quoted 13 lines · Dave Young
Quoted 12 lines · Borislav Petkov
Quoted 10 lines · Dave Young

Add a new setup_data type SETUP_EFI for kexec use. Passing the saved fw_vendor, runtime, config tables and efi runtime mappings.

When entering virtual mode, directly mapping the efi runtime ragions which we passed in previously. And skip the step to call SetVirtualAddressMap.

Specially for HP z420 workstation it need another variable saving,

Quoted 6 lines
Quoted 2 lines

Why the special handling? Does that mean, this is going to be the case for other HP UEFI implementations too?

I have only one HP machine for testing, Maybe Toshi can help to verify on other machines. Just comment out the function efi_reuse_config to see if kexec kernel panic.

My system (HP prototype server) did not need the special handling in efi_reuse_config(). I will check with firmware team to get more information on this.

Thanks,
-Toshi

#37 lore raw

Re: [PATCH v4 07/12] efi: passing kexec necessary efi data via setup_data

Quoted 17 lines · Toshi Kani
Quoted 16 lines · Dave Young
Quoted 14 lines · Borislav Petkov
Quoted 10 lines · Dave Young

Add a new setup_data type SETUP_EFI for kexec use. Passing the saved fw_vendor, runtime, config tables and efi runtime mappings.

When entering virtual mode, directly mapping the efi runtime ragions which we passed in previously. And skip the step to call SetVirtualAddressMap.

Specially for HP z420 workstation it need another variable saving,

Why the special handling? Does that mean, this is going to be the case for other HP UEFI implementations too?

Quoted 7 lines
Quoted 3 lines

I have only one HP machine for testing, Maybe Toshi can help to verify on other machines. Just comment out the function efi_reuse_config to see if kexec kernel panic.

My system (HP prototype server) did not need the special handling in efi_reuse_config(). I will check with firmware team to get more information on this.

I have one question. I think this special handling assumes that efi.smbios contains a virtual address on HP z420. So, how does the 1st kernel set a virtual address to efi.smbios in the first place?

Thanks,
-Toshi

author
#38 lore raw

Re: [PATCH v4 07/12] efi: passing kexec necessary efi data via setup_data

Quoted 22 lines · Toshi Kani
Quoted 21 lines · Toshi Kani
Quoted 19 lines · Dave Young
Quoted 9 lines · Borislav Petkov
Quoted 8 lines · Dave Young

Add a new setup_data type SETUP_EFI for kexec use. Passing the saved fw_vendor, runtime, config tables and efi runtime mappings.

When entering virtual mode, directly mapping the efi runtime ragions which we passed in previously. And skip the step to call SetVirtualAddressMap.

Quoted 5 lines
Quoted 2 lines

Specially for HP z420 workstation it need another variable saving,

Why the special handling? Does that mean, this is going to be the case for other HP UEFI implementations too?

I have only one HP machine for testing, Maybe Toshi can help to verify on other machines. Just comment out the function efi_reuse_config to see if kexec kernel panic.

Quoted 7 lines
Quoted 3 lines

My system (HP prototype server) did not need the special handling in efi_reuse_config(). I will check with firmware team to get more information on this.

I have one question. I think this special handling assumes that efi.smbios contains a virtual address on HP z420. So, how does the 1st kernel set a virtual address to efi.smbios in the first place?

Hi, Toshi,

It's originally a physical address, I think kernel does not change it. As the fw_vendor, runtime etc. are changed to virt addr by firmware, so I assume smbios on Hp z420 is also changed by firmware after becoming virtual mode.

Thanks
Dave

#39 lore raw

Re: [PATCH v4 07/12] efi: passing kexec necessary efi data via setup_data

Quoted 31 lines · Dave Young
Quoted 22 lines · Toshi Kani
Quoted 21 lines · Toshi Kani
Quoted 19 lines · Dave Young
Quoted 9 lines · Borislav Petkov
Quoted 8 lines · Dave Young

Add a new setup_data type SETUP_EFI for kexec use. Passing the saved fw_vendor, runtime, config tables and efi runtime mappings.

When entering virtual mode, directly mapping the efi runtime ragions which we passed in previously. And skip the step to call SetVirtualAddressMap.

Quoted 5 lines
Quoted 2 lines

Specially for HP z420 workstation it need another variable saving,

Why the special handling? Does that mean, this is going to be the case for other HP UEFI implementations too?

I have only one HP machine for testing, Maybe Toshi can help to verify on other machines. Just comment out the function efi_reuse_config to see if kexec kernel panic.

Quoted 7 lines
Quoted 3 lines

My system (HP prototype server) did not need the special handling in efi_reuse_config(). I will check with firmware team to get more information on this.

I have one question. I think this special handling assumes that efi.smbios contains a virtual address on HP z420. So, how does the 1st kernel set a virtual address to efi.smbios in the first place?

Quoted 5 lines

Hi, Toshi,

It's originally a physical address, I think kernel does not change it. As the fw_vendor, runtime etc. are changed to virt addr by firmware, so I assume smbios on Hp z420 is also changed by firmware after becoming virtual mode.

Hi Dave,

Oh, I think I now understand what the issue was. The z420 firmware updates the SMBIOS table address in the EFI system table to a virtual address after calling EFI SetVirtualAddressMap. So, you are passing the original physical address of the SMBIOS table from the 1st kernel to the 2nd kernel to put it back to physical. Is that right?

Thanks,
-Toshi

author
#40 lore raw

Re: [PATCH v4 07/12] efi: passing kexec necessary efi data via setup_data

Hi, Toshi

Quoted 5 lines

Oh, I think I now understand what the issue was. The z420 firmware updates the SMBIOS table address in the EFI system table to a virtual address after calling EFI SetVirtualAddressMap. So, you are passing the original physical address of the SMBIOS table from the 1st kernel to the 2nd kernel to put it back to physical. Is that right?

Right.

Thanks
Dave

#41 lore raw

Re: [PATCH v4 07/12] efi: passing kexec necessary efi data via setup_data

Quoted 9 lines · Dave Young

Hi, Toshi

Quoted 5 lines

Oh, I think I now understand what the issue was. The z420 firmware updates the SMBIOS table address in the EFI system table to a virtual address after calling EFI SetVirtualAddressMap. So, you are passing the original physical address of the SMBIOS table from the 1st kernel to the 2nd kernel to put it back to physical. Is that right?

Right.

Hi Dave,

The z420 firmware is based on some UEFI core that may be used by other vendors as well. Since this handling is totally harmless (just redundant), I'd suggest not to have a platform check on this handling.

Thanks,
-Toshi

author
#42 lore raw

Re: [PATCH v4 07/12] efi: passing kexec necessary efi data via setup_data

Quoted 9 lines · Toshi Kani
Quoted 8 lines · Dave Young

Hi, Toshi

Quoted 5 lines

Oh, I think I now understand what the issue was. The z420 firmware updates the SMBIOS table address in the EFI system table to a virtual address after calling EFI SetVirtualAddressMap. So, you are passing the original physical address of the SMBIOS table from the 1st kernel to the 2nd kernel to put it back to physical. Is that right?

Quoted 7 lines
Quoted 1 line

Right.

Hi Dave,

The z420 firmware is based on some UEFI core that may be used by other vendors as well. Since this handling is totally harmless (just redundant), I'd suggest not to have a platform check on this handling.

I have same worry as well, so I agree with you.

Thanks
Dave

maintainer
#43 lore raw

Re: [PATCH v4 07/12] efi: passing kexec necessary efi data via setup_data

Quoted 5 lines · Dave Young
Quoted 3 lines

The z420 firmware is based on some UEFI core that may be used by other vendors as well. Since this handling is totally harmless (just redundant), I'd suggest not to have a platform check on this handling.

I have same worry as well, so I agree with you.

Btw, why does kexec need the ->smbios table at all? I see it being passed in efi_setup_data.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
#44 lore raw

Re: [PATCH v4 07/12] efi: passing kexec necessary efi data via setup_data

Quoted 9 lines · Borislav Petkov
Quoted 5 lines · Dave Young
Quoted 3 lines

The z420 firmware is based on some UEFI core that may be used by other vendors as well. Since this handling is totally harmless (just redundant), I'd suggest not to have a platform check on this handling.

I have same worry as well, so I agree with you.

Btw, why does kexec need the ->smbios table at all? I see it being passed in efi_setup_data.

Hi Boris,

The smbios in efi_setup_data is necessary for kexec to pass the physical address of the SMBIOS table from the 1st kernel to the 2nd kernel.

The kernel boot sequence proceeds in the following order. Step 2 requires efi.smbios to be the physical address. However, Dave found that EFI system table has a virtual address of SMBIOS in step 1. Hence, he sets it back to the physical address with the smbios in efi_setup_data. (When it is still the physical address, it simply sets the same value.)

1. efi_init() - Set efi.smbios from EFI system table
2. dmi_scan_machine() - Temporary map efi.smbios to access SMBIOS table
3. efi_enter_virtual_mode() - Map EFI ranges

Thanks,
-Toshi

maintainer
#45 lore raw

Re: [PATCH v4 07/12] efi: passing kexec necessary efi data via setup_data

Quoted 10 lines · Toshi Kani

The smbios in efi_setup_data is necessary for kexec to pass the physical address of the SMBIOS table from the 1st kernel to the 2nd kernel.

The kernel boot sequence proceeds in the following order. Step 2 requires efi.smbios to be the physical address. However, Dave found that EFI system table has a virtual address of SMBIOS in step 1. Hence, he sets it back to the physical address with the smbios in efi_setup_data. (When it is still the physical address, it simply sets the same value.)

Quoted 3 lines

1. efi_init() - Set efi.smbios from EFI system table
2. dmi_scan_machine() - Temporary map efi.smbios to access SMBIOS table
3. efi_enter_virtual_mode() - Map EFI ranges

Thanks Toshi, it is all clear now.

@Dave: you might want to put Toshi's explanation in the commit message of your patch so that we know *why* the change is needed.

Thanks.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
author
#46 lore raw

Re: [PATCH v4 07/12] efi: passing kexec necessary efi data via setup_data

Quoted 11 lines · Borislav Petkov
Quoted 10 lines · Toshi Kani

The smbios in efi_setup_data is necessary for kexec to pass the physical address of the SMBIOS table from the 1st kernel to the 2nd kernel.

The kernel boot sequence proceeds in the following order. Step 2 requires efi.smbios to be the physical address. However, Dave found that EFI system table has a virtual address of SMBIOS in step 1. Hence, he sets it back to the physical address with the smbios in efi_setup_data. (When it is still the physical address, it simply sets the same value.)

Quoted 8 lines
Quoted 3 lines

1. efi_init() - Set efi.smbios from EFI system table
2. dmi_scan_machine() - Temporary map efi.smbios to access SMBIOS table
3. efi_enter_virtual_mode() - Map EFI ranges

Thanks Toshi, it is all clear now.

@Dave: you might want to put Toshi's explanation in the commit message of your patch so that we know *why* the change is needed.

Ok, will do.

Thanks
Dave

author
#47 lore raw

PATCH v4 8/12 efi: only print saved efi runtime maps instead of all memmap ranges for kexec

In reply to Dave Young

For kexec/kdump kernel efi runtime mappings are saved, printing original whole memmap ranges does not make sense anymore. So introduce a new function to only print runtime maps in case kexec/kdump kernel is used.

---
 arch/x86/platform/efi/efi.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
arch/x86/platform/efi/efi.c +22 -1
index fafeb40..c65b0b8 100644
@@ -430,6 +430,24 @@ int __init efi_memblock_x86_reserve_range(void)
 	return 0;
 }
 
+/* for kexec kernel runtime maps are passed in setup_data */
+static void __init print_saved_runtime_map(void)
+{
+#ifdef EFI_DEBUG
+	int i;
+	efi_memory_desc_t *md;
+
+	for (i = 0; i < nr_efi_runtime_map; i++) {
+		md = esdata->map + i;
+		pr_info("mem%02u: type=%u, attr=0x%llx, "
+			"range=[0x%016llx-0x%016llx) (%lluMB)\n",
+			i, md->type, md->attribute, md->phys_addr,
+			md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
+			(md->num_pages >> (20 - EFI_PAGE_SHIFT)));
+	}
+#endif  /*  EFI_DEBUG  */
+}
+
 static void __init print_efi_memmap(void)
 {
 #ifdef EFI_DEBUG
@@ -782,7 +800,10 @@ void __init efi_init(void)
 		x86_platform.set_wallclock = efi_set_rtc_mmss;
 	}
 #endif
-	print_efi_memmap();
+	if (esdata)
+		print_saved_runtime_map();
+	else
+		print_efi_memmap();
 }
 
 void __init efi_late_init(void)
Signature · 1 line
1.8.3.1
#48 lore raw

Re: [PATCH v4 08/12] efi: only print saved efi runtime maps instead of all memmap ranges for kexec

Quoted 39 lines · Dave Young

For kexec/kdump kernel efi runtime mappings are saved, printing original whole memmap ranges does not make sense anymore. So introduce a new function to only print runtime maps in case kexec/kdump kernel is used.

---
 arch/x86/platform/efi/efi.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
arch/x86/platform/efi/efi.c +18 -0
index fafeb40..c65b0b8 100644
@@ -430,6 +430,24 @@ int __init efi_memblock_x86_reserve_range(void)
 	return 0;
 }
 
+/* for kexec kernel runtime maps are passed in setup_data */
+static void __init print_saved_runtime_map(void)
+{
+#ifdef EFI_DEBUG
+	int i;
+	efi_memory_desc_t *md;
+
+	for (i = 0; i < nr_efi_runtime_map; i++) {
+		md = esdata->map + i;
+		pr_info("mem%02u: type=%u, attr=0x%llx, "
+			"range=[0x%016llx-0x%016llx) (%lluMB)\n",
+			i, md->type, md->attribute, md->phys_addr,
+			md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
+			(md->num_pages >> (20 - EFI_PAGE_SHIFT)));
+	}
+#endif  /*  EFI_DEBUG  */
+}
+
 static void __init print_efi_memmap(void)
 {
 #ifdef EFI_DEBUG

arch/x86/platform/efi/efi.c void __init efi_init(void) · line 807

Quoted 9 lines
arch/x86/platform/efi/efi.c +4 -1
@@ -782,7 +800,10 @@ void __init efi_init(void)
 		x86_platform.set_wallclock = efi_set_rtc_mmss;
 	}
 #endif
-	print_efi_memmap();
+	if (esdata)
+		print_saved_runtime_map();
+	else
+		print_efi_memmap();
 }

Heh, you can probably already guess what I'm going to say here...

How about using a single function to dump the memory ranges irrespective of whether the memory map comes from 'memmap' or 'esdata'? e.g. something along the lines of,

	if (esdata)
		print_efi_memmap(esdata->map, nr_efi_runtime_map,
				 sizeof(esdata->map[0]));
	else
		print_efi_memmap(memmap.map, memmap.nr_map,
				 memmap.desc_size);

?

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

Re: [PATCH v4 08/12] efi: only print saved efi runtime maps instead of all memmap ranges for kexec

Quoted 1 line · Matt Fleming

Heh, you can probably already guess what I'm going to say here...

I guessed :-)

Quoted 10 lines

How about using a single function to dump the memory ranges irrespective of whether the memory map comes from 'memmap' or 'esdata'? e.g. something along the lines of,

	if (esdata)
		print_efi_memmap(esdata->map, nr_efi_runtime_map,
				 sizeof(esdata->map[0]));
	else
		print_efi_memmap(memmap.map, memmap.nr_map,
				 memmap.desc_size);

And while you're at it:

WARNING: quoted string split across lines
#19: FILE: arch/x86/platform/efi/efi.c:443:
+ pr_info("mem%02u: type=%u, attr=0x%llx, "
+ "range=[0x%016llx-0x%016llx) (%lluMB)\n",

Btw, do we really want to dump the same map again in the second kernel?

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
author
#50 lore raw

Re: [PATCH v4 08/12] efi: only print saved efi runtime maps instead of all memmap ranges for kexec

Quoted 20 lines · Borislav Petkov
Quoted 1 line · Matt Fleming

Heh, you can probably already guess what I'm going to say here...

I guessed :-)

Quoted 10 lines

How about using a single function to dump the memory ranges irrespective of whether the memory map comes from 'memmap' or 'esdata'? e.g. something along the lines of,

	if (esdata)
		print_efi_memmap(esdata->map, nr_efi_runtime_map,
				 sizeof(esdata->map[0]));
	else
		print_efi_memmap(memmap.map, memmap.nr_map,
				 memmap.desc_size);

And while you're at it:

WARNING: quoted string split across lines
#19: FILE: arch/x86/platform/efi/efi.c:443:

arch/x86/platform/efi/efi.c int __init efi_memblock_x86_reserve_range(void) · line 443

Quoted 2 lines
arch/x86/platform/efi/efi.c +2 -0
+               pr_info("mem%02u: type=%u, attr=0x%llx, "
+                       "range=[0x%016llx-0x%016llx) (%lluMB)\n",

I see the warnings, because it's originally in print_efi_memmap so I just keep new function same. Anyway after switching to use one function there will be no such warning.

Quoted 3 lines

Btw, do we really want to dump the same map again in the second kernel?

It's for debugging purpose, I think it's helpful.

Thanks for review
Dave

maintainer
#51 lore raw

Re: [PATCH v4 08/12] efi: only print saved efi runtime maps instead of all memmap ranges for kexec

Quoted 1 line · Dave Young

It's for debugging purpose, I think it's helpful.

Why? The first kernel did dump it already.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
author
#52 lore raw

Re: [PATCH v4 08/12] efi: only print saved efi runtime maps instead of all memmap ranges for kexec

Quoted 4 lines · Borislav Petkov
Quoted 1 line · Dave Young

It's for debugging purpose, I think it's helpful.

Why? The first kernel did dump it already.

As for the efi printk though 1st kernel already printed them out, but kexec kernel context is diffrent from 1st kernel, it's a fresh reboot we do need reprint them.

I think kexec reboot is just like a normal reboot from user's point of view.

Thanks
Dave

author
#53 lore raw

Re: [PATCH v4 08/12] efi: only print saved efi runtime maps instead of all memmap ranges for kexec

In reply to Matt Fleming
Quoted 56 lines · Matt Fleming
Quoted 39 lines · Dave Young

For kexec/kdump kernel efi runtime mappings are saved, printing original whole memmap ranges does not make sense anymore. So introduce a new function to only print runtime maps in case kexec/kdump kernel is used.

---
 arch/x86/platform/efi/efi.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
arch/x86/platform/efi/efi.c +18 -0
index fafeb40..c65b0b8 100644
@@ -430,6 +430,24 @@ int __init efi_memblock_x86_reserve_range(void)
 	return 0;
 }
 
+/* for kexec kernel runtime maps are passed in setup_data */
+static void __init print_saved_runtime_map(void)
+{
+#ifdef EFI_DEBUG
+	int i;
+	efi_memory_desc_t *md;
+
+	for (i = 0; i < nr_efi_runtime_map; i++) {
+		md = esdata->map + i;
+		pr_info("mem%02u: type=%u, attr=0x%llx, "
+			"range=[0x%016llx-0x%016llx) (%lluMB)\n",
+			i, md->type, md->attribute, md->phys_addr,
+			md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
+			(md->num_pages >> (20 - EFI_PAGE_SHIFT)));
+	}
+#endif  /*  EFI_DEBUG  */
+}
+
 static void __init print_efi_memmap(void)
 {
 #ifdef EFI_DEBUG

arch/x86/platform/efi/efi.c void __init efi_init(void) · line 807

Quoted 9 lines
arch/x86/platform/efi/efi.c +4 -1
@@ -782,7 +800,10 @@ void __init efi_init(void)
 		x86_platform.set_wallclock = efi_set_rtc_mmss;
 	}
 #endif
-	print_efi_memmap();
+	if (esdata)
+		print_saved_runtime_map();
+	else
+		print_efi_memmap();
 }

Heh, you can probably already guess what I'm going to say here...

How about using a single function to dump the memory ranges irrespective of whether the memory map comes from 'memmap' or 'esdata'? e.g. something along the lines of,

Quoted 8 lines
	if (esdata)
		print_efi_memmap(esdata->map, nr_efi_runtime_map,
				 sizeof(esdata->map[0]));
	else
		print_efi_memmap(memmap.map, memmap.nr_map,
				 memmap.desc_size);

?

Yes, looks better, will do

Thanks for review
Dave

author
#54 lore raw

PATCH v4 9/12 x86: add xloadflags bit for efi runtime support on kexec

In reply to Dave Young

Old kexec-tools can not load new kernel. The reason is previously kexec-tools do not fill efi_info in x86 setup header thus efi init fail and switch to noefi boot. In new kexec-tools it will by default fill efi_info and pass other efi required infomation to 2nd kernel so kexec kernel efi initialization will success finally.

To prevent from breaking userspace, add a new xloadflags bit so kexec-tools will check the flag and switch to old logic.

changelog:
Matt: +&& defined(CONFIG_KEXEC)
HPA: document the flag.

---
 Documentation/x86/boot.txt            | 3 +++
 arch/x86/boot/header.S                | 9 ++++++++-
 arch/x86/include/uapi/asm/bootparam.h | 1 +
 3 files changed, 12 insertions(+), 1 deletion(-)
Documentation/x86/boot.txt +3 -0
index f4f268c..cb81741d 100644
@@ -608,6 +608,9 @@ Protocol:       2.12+
 	- If 1, the kernel supports the 64-bit EFI handoff entry point
           given at handover_offset + 0x200.
 
+  Bit 4 (read): XLF_EFI_KEXEC
+	- If 1, the kernel supports kexec EFI boot with EFI runtime support.
+
 Field name:	cmdline_size
 Type:		read
 Offset/size:	0x238/4
arch/x86/boot/header.S +8 -1
index 9ec06a1..ec3b8ba 100644
@@ -391,7 +391,14 @@ xloadflags:
 #else
 # define XLF23 0
 #endif
-			.word XLF0 | XLF1 | XLF23
+
+#if defined(CONFIG_X86_64) && defined(CONFIG_EFI) && defined(CONFIG_KEXEC)
+# define XLF4 XLF_EFI_KEXEC
+#else
+# define XLF4 0
+#endif
+
+			.word XLF0 | XLF1 | XLF23 | XLF4
 
 cmdline_size:   .long   COMMAND_LINE_SIZE-1     #length of the command line,
                                                 #added with boot protocol
arch/x86/include/uapi/asm/bootparam.h +1 -0
index ed6afc9..505eecf 100644
@@ -24,6 +24,7 @@
 #define XLF_CAN_BE_LOADED_ABOVE_4G	(1<<1)
 #define XLF_EFI_HANDOVER_32		(1<<2)
 #define XLF_EFI_HANDOVER_64		(1<<3)
+#define XLF_EFI_KEXEC			(1<<4)
 
 #ifndef __ASSEMBLY__
 
Signature · 1 line
1.8.3.1
maintainer Acked-by on v4 9/12
#55 lore raw

Re: [PATCH v4 09/12] x86: add xloadflags bit for efi runtime support on kexec

Quoted 1 line · Dave Young

Old kexec-tools can not load new kernel. The reason is previously kexec-tools

The reason is, kexec-tools ... previously, thus efi failed to initialize ...

Quoted 4 lines

do not fill efi_info in x86 setup header thus efi init fail and switch to noefi boot. In new kexec-tools it will by default fill efi_info and pass other efi required infomation to 2nd kernel so kexec kernel efi initialization will success finally.

succeed

Quoted 9 lines

To prevent from breaking userspace, add a new xloadflags bit so kexec-tools will check the flag and switch to old logic.

changelog:
Matt: +&& defined(CONFIG_KEXEC)
HPA: document the flag.

Other than that:

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
author
#56 lore raw

Re: [PATCH v4 09/12] x86: add xloadflags bit for efi runtime support on kexec

Quoted 4 lines · Borislav Petkov
Quoted 1 line · Dave Young

Old kexec-tools can not load new kernel. The reason is previously kexec-tools

The reason is, kexec-tools ... previously, thus efi failed to initialize ...

Good suggestion

Quoted 7 lines
Quoted 5 lines

do not fill efi_info in x86 setup header thus efi init fail and switch to noefi boot. In new kexec-tools it will by default fill efi_info and pass other efi required infomation to 2nd kernel so kexec kernel efi initialization will success finally.

succeed

Will fix

Quoted 9 lines
Quoted 9 lines

To prevent from breaking userspace, add a new xloadflags bit so kexec-tools will check the flag and switch to old logic.

changelog:
Matt: +&& defined(CONFIG_KEXEC)
HPA: document the flag.

Quoted 6 lines
Quoted 1 line

Other than that:

Thanks
Dave

author
#57 lore raw

PATCH v4 10/12 x86: export x86 boot_params to sysfs

In reply to Dave Young

kexec-tools use boot_params for getting the 1st kernel hardware_subarch, the kexec kernel efi runtime support also need read the old efi_info from boot_params. Currently it exists in debugfs which is not a good place for such infomation. Per HPA, we should avoid of "sploit debugfs".

In this patch /sys/kernel/boot_params are exported, also the setup_data is exported as a subdirectory. For original debugfs since it's already there for long time and kexec-tools is using it for hardware_subarch so let's do not remove them for now.

Structure are like below:

/sys/kernel/boot_params

├── data		/* binary data for boot_params */
├── setup_data  	/* subdirectory for setup_data if there's any */
│   ├── 0		/* the first setup_data node */
│   │   ├── data	/* binary data for setup_data node 0 */
│   │   └── type	/* setup_data type of setup_data node 0, hex string */
|   [snip]		/* other setup_data nodes ... */
└── version		/* hex string for boot protocal version */

Changelog:
Greg: use __ATTR_RO() and group attr.

---
 Documentation/ABI/testing/sysfs-kernel-boot_params |  40 +++
 arch/x86/kernel/Makefile                           |   2 +-
 arch/x86/kernel/ksysfs.c                           | 339 +++++++++++++++++++++
 3 files changed, 380 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/ABI/testing/sysfs-kernel-boot_params
 create mode 100644 arch/x86/kernel/ksysfs.c
Documentation/ABI/testing/sysfs-kernel-boot_params new file +40 -0
new file mode 100644
index 0000000..8014a93
@@ -0,0 +1,40 @@
+What:		/sys/kernel/boot_params
+Date:		November 2013
+Contact:	Dave Young <[email protected]>
+Description:
+		The /sys/kernel/boot_params directory contains two
+		files: "data" and "version" and one subdirectory "setup_data".
+		It is used to export the kernel boot parameters of x86
+		platform to user space for kexec and debugging purpose.
+
+		If there's no setup_data in boot_params the subdirectory will
+		not be created.
+
+		"data" file is the binary representation of struct boot_params.
+
+		"version" file is the string representation of boot
+		protocol version.
+
+		"setup_data" subdirectory contains the setup_data data
+		structure in boot_params. setup_data is maintained in kernel
+		as a link list. In "setup_data" subdirectory there's one
+		subdirectory for each link list node named with the number
+		of the list nodes. The list node subdirectory contains two
+		files "type" and "data". "type" file is the string
+		representation of setup_data type. "data" file is the binary
+		representation of setup_data payload.
+
+		The whole boot_params directory structure is like below:
+		/sys/kernel/boot_params
+		├── data
+		├── setup_data
+		│   ├── 0
+		│   │   ├── data
+		│   │   └── type
+		│   └── 1
+		│       ├── data
+		│       └── type
+		└── version
+
+Users:
+		Kexec Mailing List <[email protected]>
arch/x86/kernel/Makefile +1 -1
index a5408b9..473a4bb 100644
@@ -35,7 +35,7 @@ obj-y			+= alternative.o i8253.o pci-nommu.o hw_breakpoint.o
 obj-y			+= tsc.o io_delay.o rtc.o
 obj-y			+= pci-iommu_table.o
 obj-y			+= resource.o
-
+obj-$(CONFIG_SYSFS)	+= ksysfs.o
 obj-y				+= process.o
 obj-y				+= i387.o xsave.o
 obj-y				+= ptrace.o
arch/x86/kernel/ksysfs.c new file +339 -0

339 lines not on this page · raw message

Signature · 7 lines
1.8.3.1


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

Re: [PATCH v4 10/12] x86: export x86 boot_params to sysfs

Review

Quoted 2 lines · Dave Young
(quoted diff) +2 -0
+Users:
+		Kexec Mailing List <[email protected]>

"Kexec" please.

arch/x86/kernel/ksysfs.c line 23

Quoted 2 lines
arch/x86/kernel/ksysfs.c new file +2 -0
+static ssize_t version_show(struct kobject *kobj,
+					struct kobj_attribute *attr, char *buf)

This is pretty strange indentation. Usually we prefer to line up the arguments like you've done elsewhere in this file.

arch/x86/kernel/ksysfs.c line 36

Quoted 13 lines
arch/x86/kernel/ksysfs.c new file +13 -0
+{
+	return sprintf(buf, "0x%04x\n", boot_params.hdr.version);
+}
+
+static struct kobj_attribute boot_params_version_attr = __ATTR_RO(version);
+
+static ssize_t boot_params_data_read(struct file *fp, struct kobject *kobj,
+				     struct bin_attribute *bin_attr,
+				     char *buf, loff_t off, size_t count)
+{
+	memcpy(buf, (void *)&boot_params + off, count);
+	return count;
+}

Don't we need some checks here to ensure we don't read past the end of boot_params?

arch/x86/kernel/ksysfs.c line 116

Quoted 2 lines
arch/x86/kernel/ksysfs.c new file +2 -0
+static ssize_t type_show(struct kobject *kobj,
+					struct kobj_attribute *attr, char *buf)

More whacky indentation.

arch/x86/kernel/ksysfs.c line 210

Quoted 2 lines
arch/x86/kernel/ksysfs.c new file +2 -0
+static int __init create_setup_data_node(struct kobject *parent,
+					struct kobject **kobjp, int nr)

Funky indentation.

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

Re: [PATCH v4 10/12] x86: export x86 boot_params to sysfs

Quoted 5 lines · Matt Fleming

Review

Quoted 2 lines · Dave Young
(quoted diff) +2 -0
+Users:
+		Kexec Mailing List <[email protected]>

"Kexec" please.

Will change

Quoted 6 lines

arch/x86/kernel/ksysfs.c line 23

Quoted 3 lines
arch/x86/kernel/ksysfs.c new file +2 -0
+static ssize_t version_show(struct kobject *kobj,
+					struct kobj_attribute *attr, char *buf)

This is pretty strange indentation. Usually we prefer to line up the arguments like you've done elsewhere in this file.

Ok.

Quoted 17 lines
Quoted 4 lines
arch/x86/kernel/ksysfs.c new file +3 -0
+{
+	return sprintf(buf, "0x%04x\n", boot_params.hdr.version);
+}

arch/x86/kernel/ksysfs.c line 36

Quoted 10 lines
arch/x86/kernel/ksysfs.c new file +10 -0
+
+static struct kobj_attribute boot_params_version_attr = __ATTR_RO(version);
+
+static ssize_t boot_params_data_read(struct file *fp, struct kobject *kobj,
+				     struct bin_attribute *bin_attr,
+				     char *buf, loff_t off, size_t count)
+{
+	memcpy(buf, (void *)&boot_params + off, count);
+	return count;
+}

Don't we need some checks here to ensure we don't read past the end of boot_params?

The size was added below, so I would assume file read will not beyond the file size:
.size = sizeof(boot_params),

Quoted 5 lines

arch/x86/kernel/ksysfs.c line 116

Quoted 3 lines
arch/x86/kernel/ksysfs.c new file +2 -0
+static ssize_t type_show(struct kobject *kobj,
+					struct kobj_attribute *attr, char *buf)

More whacky indentation.

Will fix

Quoted 5 lines

arch/x86/kernel/ksysfs.c line 210

Quoted 3 lines
arch/x86/kernel/ksysfs.c new file +2 -0
+static int __init create_setup_data_node(struct kobject *parent,
+					struct kobject **kobjp, int nr)

Funky indentation.

Will fix

Thanks for review
Dave

maintainer
#60 lore raw

Re: [PATCH v4 10/12] x86: export x86 boot_params to sysfs

In reply to Dave Young
Quoted 9 lines · Dave Young

kexec-tools use boot_params for getting the 1st kernel hardware_subarch, the kexec kernel efi runtime support also need read the old efi_info from boot_params. Currently it exists in debugfs which is not a good place for such infomation. Per HPA, we should avoid of "sploit debugfs".

In this patch /sys/kernel/boot_params are exported, also the setup_data is exported as a subdirectory. For original debugfs since it's already there for long time and kexec-tools is using it for hardware_subarch so let's do not remove them for now.

"kexec-tools is using debugfs for hardware_subarch for a long time now so we're not removing it yet."

Quoted 1 line

Structure are like below:

is

Quoted 3 lines

/sys/kernel/boot_params
├── data /* binary data for boot_params */

You mean "boot_params in binary"?

Quoted 1 line

├── setup_data /* subdirectory for setup_data if there's any */

no need for that comment

Quoted 2 lines
│   ├── 0		/* the first setup_data node */
│   │   ├── data	/* binary data for setup_data node 0 */

"setup_data node 0 in binary"

Quoted 3 lines
│   │   └── type	/* setup_data type of setup_data node 0, hex string */
|   [snip]		/* other setup_data nodes ... */
└── version		/* hex string for boot protocal version */

"boot protocol version (in hex, "0x" prefixed)"

Quoted 19 lines

Changelog:
Greg: use __ATTR_RO() and group attr.

---
 Documentation/ABI/testing/sysfs-kernel-boot_params |  40 +++
 arch/x86/kernel/Makefile                           |   2 +-
 arch/x86/kernel/ksysfs.c                           | 339 +++++++++++++++++++++
 3 files changed, 380 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/ABI/testing/sysfs-kernel-boot_params
 create mode 100644 arch/x86/kernel/ksysfs.c
Documentation/ABI/testing/sysfs-kernel-boot_params new file +0 -0
new file mode 100644
index 0000000..8014a93

Documentation/ABI/testing/sysfs-kernel-boot_params line 7

Quoted 7 lines
Documentation/ABI/testing/sysfs-kernel-boot_params new file +7 -0
@@ -0,0 +1,40 @@
+What:		/sys/kernel/boot_params
+Date:		November 2013
+Contact:	Dave Young <[email protected]>
+Description:
+		The /sys/kernel/boot_params directory contains two
+		files: "data" and "version" and one subdirectory "setup_data".
+		It is used to export the kernel boot parameters of x86

"... of an x86 platform to userspace for kexec and debugging purposes."

Quoted 7 lines
Documentation/ABI/testing/sysfs-kernel-boot_params new file +7 -0
+		platform to user space for kexec and debugging purpose.
+
+		If there's no setup_data in boot_params the subdirectory will
+		not be created.
+
+		"data" file is the binary representation of struct boot_params.
+

Documentation/ABI/testing/sysfs-kernel-boot_params line 24

Quoted 10 lines
Documentation/ABI/testing/sysfs-kernel-boot_params new file +10 -0
+		"version" file is the string representation of boot
+		protocol version.
+
+		"setup_data" subdirectory contains the setup_data data
+		structure in boot_params. setup_data is maintained in kernel
+		as a link list. In "setup_data" subdirectory there's one
+		subdirectory for each link list node named with the number
+		of the list nodes. The list node subdirectory contains two
+		files "type" and "data". "type" file is the string
+		representation of setup_data type.

string or int?

Quoted 22 lines
Documentation/ABI/testing/sysfs-kernel-boot_params new file +17 -0
+		"data" file is the binary
+		representation of setup_data payload.
+
+		The whole boot_params directory structure is like below:
+		/sys/kernel/boot_params
+		├── data
+		├── setup_data
+		│   ├── 0
+		│   │   ├── data
+		│   │   └── type
+		│   └── 1
+		│       ├── data
+		│       └── type
+		└── version
+
+Users:
+		Kexec Mailing List <[email protected]>
arch/x86/kernel/Makefile +0 -0
index a5408b9..473a4bb 100644

arch/x86/kernel/Makefile obj-y += alternative.o i8253.o pci-nommu.o hw_breakpoint.o · line 41

Quoted 8 lines
arch/x86/kernel/Makefile +1 -1
@@ -35,7 +35,7 @@ obj-y			+= alternative.o i8253.o pci-nommu.o hw_breakpoint.o
 obj-y			+= tsc.o io_delay.o rtc.o
 obj-y			+= pci-iommu_table.o
 obj-y			+= resource.o
-
+obj-$(CONFIG_SYSFS)	+= ksysfs.o
 obj-y				+= process.o
 obj-y				+= i387.o xsave.o
 obj-y				+= ptrace.o

Conflict with upstream, it should be:

arch/x86/kernel/Makefile +1 -0
index 9b0a34e2cd79..851dcd1218fc 100644
@@ -35,6 +35,7 @@ obj-y                 += alternative.o i8253.o pci-nommu.o hw_breakpoint.o
 obj-y                  += tsc.o io_delay.o rtc.o
 obj-y                  += pci-iommu_table.o
 obj-y                  += resource.o
+obj-$(CONFIG_SYSFS)    += ksysfs.o
 
 obj-$(CONFIG_PREEMPT)  += preempt.o
 

arch/x86/kernel/ksysfs.c line 2

Quoted 8 lines
arch/x86/kernel/ksysfs.c new file +2 -0
new file mode 100644
index 0000000..3f91207
@@ -0,0 +1,339 @@
+/*
+ * Architecture specific sysfs attributes in /sys/kernel

Architecture-specific

...

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
author
#61 lore raw

Re: [PATCH v4 10/12] x86: export x86 boot_params to sysfs

Quoted 6 lines · Borislav Petkov
Quoted 5 lines · Dave Young

kexec-tools use boot_params for getting the 1st kernel hardware_subarch, the kexec kernel efi runtime support also need read the old efi_info from boot_params. Currently it exists in debugfs which is not a good place for such infomation. Per HPA, we should avoid of "sploit debugfs".

Quoted 7 lines
Quoted 4 lines

In this patch /sys/kernel/boot_params are exported, also the setup_data is exported as a subdirectory. For original debugfs since it's already there for long time and kexec-tools is using it for hardware_subarch so let's do not remove them for now.

"kexec-tools is using debugfs for hardware_subarch for a long time now so we're not removing it yet."

Will take above log, thanks.

Quoted 4 lines
Quoted 2 lines

Structure are like below:

is

Will fix

Quoted 6 lines
Quoted 4 lines

/sys/kernel/boot_params
├── data /* binary data for boot_params */

You mean "boot_params in binary"?

Yes, "boot_params in binary" is better, will use

Quoted 4 lines
Quoted 2 lines

├── setup_data /* subdirectory for setup_data if there's any */

no need for that comment

Ok

Quoted 5 lines
Quoted 3 lines
│   ├── 0		/* the first setup_data node */
│   │   ├── data	/* binary data for setup_data node 0 */

"setup_data node 0 in binary"

Will do.

Quoted 6 lines
Quoted 4 lines
│   │   └── type	/* setup_data type of setup_data node 0, hex string */
|   [snip]		/* other setup_data nodes ... */
└── version		/* hex string for boot protocal version */

"boot protocol version (in hex, "0x" prefixed)"

Will fix.

Quoted 14 lines
Quoted 14 lines

Changelog:
Greg: use __ATTR_RO() and group attr.

---
 Documentation/ABI/testing/sysfs-kernel-boot_params |  40 +++
 arch/x86/kernel/Makefile                           |   2 +-
 arch/x86/kernel/ksysfs.c                           | 339 +++++++++++++++++++++
 3 files changed, 380 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/ABI/testing/sysfs-kernel-boot_params
 create mode 100644 arch/x86/kernel/ksysfs.c
Quoted 15 lines

Documentation/ABI/testing/sysfs-kernel-boot_params line 7

Quoted 13 lines
Documentation/ABI/testing/sysfs-kernel-boot_params new file +7 -0
new file mode 100644
index 0000000..8014a93
@@ -0,0 +1,40 @@
+What:		/sys/kernel/boot_params
+Date:		November 2013
+Contact:	Dave Young <[email protected]>
+Description:
+		The /sys/kernel/boot_params directory contains two
+		files: "data" and "version" and one subdirectory "setup_data".
+		It is used to export the kernel boot parameters of x86

"... of an x86 platform to userspace for kexec and debugging purposes."

Will update

Quoted 12 lines
Quoted 12 lines
Documentation/ABI/testing/sysfs-kernel-boot_params new file +11 -0
+		platform to user space for kexec and debugging purpose.
+
+		If there's no setup_data in boot_params the subdirectory will
+		not be created.
+
+		"data" file is the binary representation of struct boot_params.
+
+		"version" file is the string representation of boot
+		protocol version.
+
+		"setup_data" subdirectory contains the setup_data data
Quoted 8 lines

Documentation/ABI/testing/sysfs-kernel-boot_params line 24

Quoted 6 lines
Documentation/ABI/testing/sysfs-kernel-boot_params new file +6 -0
+		structure in boot_params. setup_data is maintained in kernel
+		as a link list. In "setup_data" subdirectory there's one
+		subdirectory for each link list node named with the number
+		of the list nodes. The list node subdirectory contains two
+		files "type" and "data". "type" file is the string
+		representation of setup_data type.

string or int?

It's printed as a string. Kernel data type is int, here means string from sysfs point of view. On the other hand, "data" is a binary..

Quoted 24 lines
Quoted 24 lines
Documentation/ABI/testing/sysfs-kernel-boot_params new file +17 -0
+		"data" file is the binary
+		representation of setup_data payload.
+
+		The whole boot_params directory structure is like below:
+		/sys/kernel/boot_params
+		├── data
+		├── setup_data
+		│   ├── 0
+		│   │   ├── data
+		│   │   └── type
+		│   └── 1
+		│       ├── data
+		│       └── type
+		└── version
+
+Users:
+		Kexec Mailing List <[email protected]>
arch/x86/kernel/Makefile +0 -0
index a5408b9..473a4bb 100644
@@ -35,7 +35,7 @@ obj-y			+= alternative.o i8253.o pci-nommu.o hw_breakpoint.o
 obj-y			+= tsc.o io_delay.o rtc.o
 obj-y			+= pci-iommu_table.o
Quoted 8 lines

arch/x86/kernel/Makefile obj-y += alternative.o i8253.o pci-nommu.o hw_breakpoint.o · line 41

Quoted 6 lines
arch/x86/kernel/Makefile +1 -1
 obj-y			+= resource.o
-
+obj-$(CONFIG_SYSFS)	+= ksysfs.o
 obj-y				+= process.o
 obj-y				+= i387.o xsave.o
 obj-y				+= ptrace.o

Conflict with upstream, it should be:

Will update

Quoted 14 lines
arch/x86/kernel/Makefile +1 -0
index 9b0a34e2cd79..851dcd1218fc 100644
@@ -35,6 +35,7 @@ obj-y                 += alternative.o i8253.o pci-nommu.o hw_breakpoint.o
 obj-y                  += tsc.o io_delay.o rtc.o
 obj-y                  += pci-iommu_table.o
 obj-y                  += resource.o
+obj-$(CONFIG_SYSFS)    += ksysfs.o
 
 obj-$(CONFIG_PREEMPT)  += preempt.o
 
Quoted 10 lines

arch/x86/kernel/ksysfs.c line 2

Quoted 8 lines
arch/x86/kernel/ksysfs.c new file +2 -0
new file mode 100644
index 0000000..3f91207
@@ -0,0 +1,339 @@
+/*
+ * Architecture specific sysfs attributes in /sys/kernel

Architecture-specific

Ok.

Thanks a lot for the review
Dave

author
#62 lore raw

PATCH v4 11/12 x86: reserve setup_data ranges late after parsing memmap cmdline

In reply to Dave Young

Kdump kernel using memmap=exactmap, move e820_reserve_setup_data after parsing early params so they can be set reserved finally. Or kdump kernel will warn about ioremap a normal ram range.

---
 arch/x86/kernel/setup.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/setup.c +2 -2
index ecf225e..4a18f2f 100644
@@ -927,8 +927,6 @@ void __init setup_arch(char **cmdline_p)
 	iomem_resource.end = (1ULL << boot_cpu_data.x86_phys_bits) - 1;
 	setup_memory_map();
 	parse_setup_data();
-	/* update the e820_saved too */
-	e820_reserve_setup_data();
 
 	copy_edd();
 
@@ -990,6 +988,8 @@ void __init setup_arch(char **cmdline_p)
 		early_dump_pci_devices();
 #endif
 
+	/* update the e820_saved too */
+	e820_reserve_setup_data();
 	finish_e820_parsing();
 
 	if (efi_enabled(EFI_BOOT))
Signature · 1 line
1.8.3.1
maintainer
#63 lore raw

Re: [PATCH v4 11/12] x86: reserve setup_data ranges late after parsing memmap cmdline

Quoted 3 lines · Dave Young

Kdump kernel using memmap=exactmap, move e820_reserve_setup_data after parsing early params so they can be set reserved finally. Or kdump kernel will warn about ioremap a normal ram range.

I had to stare for a while at this commit message, rereading it a couple of times and looking at the code in order to understand why you're moving the call further down.

Please rewrite it into something more readable.

The usual structure which you could use is:

1. Currently we have A
2. We need B
3. In order to achieve it, we do C

or something to that effect.

Thanks.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
author
#64 lore raw

Re: [PATCH v4 11/12] x86: reserve setup_data ranges late after parsing memmap cmdline

Quoted 11 lines · Borislav Petkov
Quoted 3 lines · Dave Young

Kdump kernel using memmap=exactmap, move e820_reserve_setup_data after parsing early params so they can be set reserved finally. Or kdump kernel will warn about ioremap a normal ram range.

I had to stare for a while at this commit message, rereading it a couple of times and looking at the code in order to understand why you're moving the call further down.

Please rewrite it into something more readable.

Quoted 7 lines

The usual structure which you could use is:

1. Currently we have A
2. We need B
3. In order to achieve it, we do C

or something to that effect.

Is below changes ok to you?

Currently e820_reserve_setup_data is called before parsing early params, it works in normal case. But for memmap=exactmap, the final memory ranges are created after parsing memmap= cmdline params, so the previous e820_reserve_setup_data has no effect. For example for setup_data ranges will still be marked as normal system ram, thus when later sysfs driver ioremap them kernel will warn about mapping normal ram.

This patch fix it by moving the e820_reserve_setup_data callback after parsing early params so they can be set as reserved ranges and later ioremap will be fine with it.

Thanks
Dave

maintainer
#65 lore raw

Re: [PATCH v4 11/12] x86: reserve setup_data ranges late after parsing memmap cmdline

Quoted 1 line · Dave Young

Is below changes ok to you?

Almost perfect! Just a micro-nitpick below:

Quoted 4 lines

Currently e820_reserve_setup_data is called before parsing early params, it works in normal case. But for memmap=exactmap, the final memory ranges are created after parsing memmap= cmdline params, so the previous e820_reserve_setup_data has no effect. For example for setup_data ranges

s/ for/,/

Quoted 6 lines

will still be marked as normal system ram, thus when later sysfs driver ioremap them kernel will warn about mapping normal ram.

This patch fix it by moving the e820_reserve_setup_data callback after parsing early params so they can be set as reserved ranges and later ioremap will be fine with it.

Thanks.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
author
#66 lore raw

PATCH v4 12/12 x86: kdebugfs do not use __va for getting setup_data virt addr

In reply to Dave Young

__va does not work in case memmap=exactmap, so let's always use ioremap_cache.

---
 arch/x86/kernel/kdebugfs.c | 35 +++++++++++------------------------
 1 file changed, 11 insertions(+), 24 deletions(-)
arch/x86/kernel/kdebugfs.c +11 -24
index dc1404b..185e1ca 100644
@@ -33,7 +33,6 @@ static ssize_t setup_data_read(struct file *file, char __user *user_buf,
 	struct setup_data_node *node = file->private_data;
 	unsigned long remain;
 	loff_t pos = *ppos;
-	struct page *pg;
 	void *p;
 	u64 pa;
 
@@ -47,18 +46,12 @@ static ssize_t setup_data_read(struct file *file, char __user *user_buf,
 		count = node->len - pos;
 
 	pa = node->paddr + sizeof(struct setup_data) + pos;
-	pg = pfn_to_page((pa + count - 1) >> PAGE_SHIFT);
-	if (PageHighMem(pg)) {
-		p = ioremap_cache(pa, count);
-		if (!p)
-			return -ENXIO;
-	} else
-		p = __va(pa);
+	p = ioremap_cache(pa, count);
+	if (!p)
+		return -ENXIO;
 
 	remain = copy_to_user(user_buf, p, count);
-
-	if (PageHighMem(pg))
-		iounmap(p);
+	iounmap(p);
 
 	if (remain)
 		return -EFAULT;
@@ -109,7 +102,6 @@ static int __init create_setup_data_nodes(struct dentry *parent)
 	struct setup_data *data;
 	int error;
 	struct dentry *d;
-	struct page *pg;
 	u64 pa_data;
 	int no = 0;
 
@@ -126,16 +118,12 @@ static int __init create_setup_data_nodes(struct dentry *parent)
 			goto err_dir;
 		}
 
-		pg = pfn_to_page((pa_data+sizeof(*data)-1) >> PAGE_SHIFT);
-		if (PageHighMem(pg)) {
-			data = ioremap_cache(pa_data, sizeof(*data));
-			if (!data) {
-				kfree(node);
-				error = -ENXIO;
-				goto err_dir;
-			}
-		} else
-			data = __va(pa_data);
+		data = ioremap_cache(pa_data, sizeof(*data));
+		if (!data) {
+			kfree(node);
+			error = -ENXIO;
+			goto err_dir;
+		}
 
 		node->paddr = pa_data;
 		node->type = data->type;
@@ -143,8 +131,7 @@ static int __init create_setup_data_nodes(struct dentry *parent)
 		error = create_setup_data_node(d, no, node);
 		pa_data = data->next;
 
-		if (PageHighMem(pg))
-			iounmap(data);
+		iounmap(data);
 		if (error)
 			goto err_dir;
 		no++;
Signature · 1 line
1.8.3.1
#68 lore raw
In reply to Dave Young
Quoted 12 lines · Dave Young

Hi,

Here is the V4 resend for supporting kexec kernel efi runtime. Per pervious discussion I pass the 1st kernel efi runtime mapping via setup_data to 2nd kernel. Besides of the runtime mapping info I also pass the fw_vendor, runtime, config table, smbios physical address in setup_data. EFI spec mentioned fw_vendor, runtime, config table addresses will be converted to virt address after entering virtual mode, but we will use it as physical address in efi_init. For smbios EFI spec did not mention about the address updating, but during my test on a HP workstation, the bios will convert it to Virt addr, thus pass it in setup_data as well.

Dave, your commits introduce a bunch of new sparse warnings,

/home/build/git/efi/arch/x86/platform/efi/efi.c:130:15: warning: incorrect type in assignment (different address spaces)
/home/build/git/efi/arch/x86/platform/efi/efi.c:130:15: expected struct setup_data *sdata
/home/build/git/efi/arch/x86/platform/efi/efi.c:130:15: got void [noderef] <asn:2>*
/home/build/git/efi/arch/x86/platform/efi/efi.c:140:23: warning: incorrect type in argument 1 (different address spaces)
/home/build/git/efi/arch/x86/platform/efi/efi.c:140:23: expected void [noderef] <asn:2>*addr
/home/build/git/efi/arch/x86/platform/efi/efi.c:140:23: got struct setup_data *sdata
/home/build/git/efi/arch/x86/platform/efi/efi.c:143:16: warning: incorrect type in assignment (different address spaces)
/home/build/git/efi/arch/x86/platform/efi/efi.c:143:16: expected struct efi_setup_data *[addressable] [toplevel] esdata
/home/build/git/efi/arch/x86/platform/efi/efi.c:143:16: got void [noderef] <asn:2>*

/home/build/git/efi/arch/x86/platform/efi/efi.c:701:20: warning: incorrect type in assignment (different address spaces)
/home/build/git/efi/arch/x86/platform/efi/efi.c:701:20: expected void *tablep
/home/build/git/efi/arch/x86/platform/efi/efi.c:701:20: got void [noderef] <asn:2>*
/home/build/git/efi/arch/x86/platform/efi/efi.c:722:23: warning: incorrect type in argument 1 (different address spaces)
/home/build/git/efi/arch/x86/platform/efi/efi.c:722:23: expected void [noderef] <asn:2>*addr
/home/build/git/efi/arch/x86/platform/efi/efi.c:722:23: got void *tablep

/home/build/git/efi/arch/x86/platform/efi/efi.c:1079:31: warning: incorrect type in argument 1 (different address spaces)
/home/build/git/efi/arch/x86/platform/efi/efi.c:1079:31: expected void [noderef] <asn:2>*addr
/home/build/git/efi/arch/x86/platform/efi/efi.c:1079:31: got struct efi_setup_data *[addressable] [toplevel] esdata

Signature · 1 line
Matt Fleming, Intel Open Source Technology Center
author
#69 lore raw
Quoted 14 lines · Matt Fleming
Quoted 12 lines · Dave Young

Hi,

Here is the V4 resend for supporting kexec kernel efi runtime. Per pervious discussion I pass the 1st kernel efi runtime mapping via setup_data to 2nd kernel. Besides of the runtime mapping info I also pass the fw_vendor, runtime, config table, smbios physical address in setup_data. EFI spec mentioned fw_vendor, runtime, config table addresses will be converted to virt address after entering virtual mode, but we will use it as physical address in efi_init. For smbios EFI spec did not mention about the address updating, but during my test on a HP workstation, the bios will convert it to Virt addr, thus pass it in setup_data as well.

Quoted 1 line

Dave, your commits introduce a bunch of new sparse warnings,

Hi, Matt

Will fix them, thanks.

Quoted 24 lines

/home/build/git/efi/arch/x86/platform/efi/efi.c:130:15: warning: incorrect type in assignment (different address spaces)
/home/build/git/efi/arch/x86/platform/efi/efi.c:130:15: expected struct setup_data *sdata
/home/build/git/efi/arch/x86/platform/efi/efi.c:130:15: got void [noderef] <asn:2>*
/home/build/git/efi/arch/x86/platform/efi/efi.c:140:23: warning: incorrect type in argument 1 (different address spaces)
/home/build/git/efi/arch/x86/platform/efi/efi.c:140:23: expected void [noderef] <asn:2>*addr
/home/build/git/efi/arch/x86/platform/efi/efi.c:140:23: got struct setup_data *sdata
/home/build/git/efi/arch/x86/platform/efi/efi.c:143:16: warning: incorrect type in assignment (different address spaces)
/home/build/git/efi/arch/x86/platform/efi/efi.c:143:16: expected struct efi_setup_data *[addressable] [toplevel] esdata
/home/build/git/efi/arch/x86/platform/efi/efi.c:143:16: got void [noderef] <asn:2>*

/home/build/git/efi/arch/x86/platform/efi/efi.c:701:20: warning: incorrect type in assignment (different address spaces)
/home/build/git/efi/arch/x86/platform/efi/efi.c:701:20: expected void *tablep
/home/build/git/efi/arch/x86/platform/efi/efi.c:701:20: got void [noderef] <asn:2>*
/home/build/git/efi/arch/x86/platform/efi/efi.c:722:23: warning: incorrect type in argument 1 (different address spaces)
/home/build/git/efi/arch/x86/platform/efi/efi.c:722:23: expected void [noderef] <asn:2>*addr
/home/build/git/efi/arch/x86/platform/efi/efi.c:722:23: got void *tablep

/home/build/git/efi/arch/x86/platform/efi/efi.c:1079:31: warning: incorrect type in argument 1 (different address spaces)
/home/build/git/efi/arch/x86/platform/efi/efi.c:1079:31: expected void [noderef] <asn:2>*addr
/home/build/git/efi/arch/x86/platform/efi/efi.c:1079:31: got struct efi_setup_data *[addressable] [toplevel] esdata

Signature · 1 line
Matt Fleming, Intel Open Source Technology Center
author
#70 lore raw
Quoted 15 lines · Dave Young
Quoted 14 lines · Matt Fleming
Quoted 12 lines · Dave Young

Hi,

Here is the V4 resend for supporting kexec kernel efi runtime. Per pervious discussion I pass the 1st kernel efi runtime mapping via setup_data to 2nd kernel. Besides of the runtime mapping info I also pass the fw_vendor, runtime, config table, smbios physical address in setup_data. EFI spec mentioned fw_vendor, runtime, config table addresses will be converted to virt address after entering virtual mode, but we will use it as physical address in efi_init. For smbios EFI spec did not mention about the address updating, but during my test on a HP workstation, the bios will convert it to Virt addr, thus pass it in setup_data as well.

Quoted 27 lines
Quoted 1 line

Dave, your commits introduce a bunch of new sparse warnings,

Hi, Matt

Will fix them, thanks.

Quoted 21 lines

/home/build/git/efi/arch/x86/platform/efi/efi.c:130:15: warning: incorrect type in assignment (different address spaces)
/home/build/git/efi/arch/x86/platform/efi/efi.c:130:15: expected struct setup_data *sdata
/home/build/git/efi/arch/x86/platform/efi/efi.c:130:15: got void [noderef] <asn:2>*
/home/build/git/efi/arch/x86/platform/efi/efi.c:140:23: warning: incorrect type in argument 1 (different address spaces)
/home/build/git/efi/arch/x86/platform/efi/efi.c:140:23: expected void [noderef] <asn:2>*addr
/home/build/git/efi/arch/x86/platform/efi/efi.c:140:23: got struct setup_data *sdata
/home/build/git/efi/arch/x86/platform/efi/efi.c:143:16: warning: incorrect type in assignment (different address spaces)
/home/build/git/efi/arch/x86/platform/efi/efi.c:143:16: expected struct efi_setup_data *[addressable] [toplevel] esdata
/home/build/git/efi/arch/x86/platform/efi/efi.c:143:16: got void [noderef] <asn:2>*

/home/build/git/efi/arch/x86/platform/efi/efi.c:701:20: warning: incorrect type in assignment (different address spaces)
/home/build/git/efi/arch/x86/platform/efi/efi.c:701:20: expected void *tablep
/home/build/git/efi/arch/x86/platform/efi/efi.c:701:20: got void [noderef] <asn:2>*
/home/build/git/efi/arch/x86/platform/efi/efi.c:722:23: warning: incorrect type in argument 1 (different address spaces)
/home/build/git/efi/arch/x86/platform/efi/efi.c:722:23: expected void [noderef] <asn:2>*addr
/home/build/git/efi/arch/x86/platform/efi/efi.c:722:23: got void *tablep

/home/build/git/efi/arch/x86/platform/efi/efi.c:1079:31: warning: incorrect type in argument 1 (different address spaces)
/home/build/git/efi/arch/x86/platform/efi/efi.c:1079:31: expected void [noderef] <asn:2>*addr
/home/build/git/efi/arch/x86/platform/efi/efi.c:1079:31: got struct efi_setup_data *[addressable] [toplevel] esdata

For the early_memremap, these warnings are safe, but it create a lot of warnings and looks anoise.

I think for anyone who are using early_memremap should know this is a normal kernel memory instead of real __iomem, so I'd prefer below fix, it will suppress most of original and new warnings:

arch/x86/include/asm/io.h +2 -1
index 34f69cb..1db414f 100644
@@ -325,9 +325,10 @@ extern void early_ioremap_init(void);
 extern void early_ioremap_reset(void);
 extern void __iomem *early_ioremap(resource_size_t phys_addr,
 				   unsigned long size);
-extern void __iomem *early_memremap(resource_size_t phys_addr,
+extern void *early_memremap(resource_size_t phys_addr,
 				    unsigned long size);
 extern void early_iounmap(void __iomem *addr, unsigned long size);
+extern void early_memunmap(void *addr, unsigned long size);
 extern void fixup_early_ioremap(void);
 extern bool is_early_ioremap_ptep(pte_t *ptep);
 
arch/x86/mm/ioremap.c +7 -2
index 799580c..92a896652 100644
@@ -562,10 +562,10 @@ early_ioremap(resource_size_t phys_addr, unsigned long size)
 }
 
 /* Remap memory */
-void __init __iomem *
+void __init *
 early_memremap(resource_size_t phys_addr, unsigned long size)
 {
-	return __early_ioremap(phys_addr, size, PAGE_KERNEL);
+	return (__force void __kernel *) __early_ioremap(phys_addr, size, PAGE_KERNEL);
 }
 
 void __init early_iounmap(void __iomem *addr, unsigned long size)
@@ -620,3 +620,8 @@ void __init early_iounmap(void __iomem *addr, unsigned long size)
 	}
 	prev_map[slot] = NULL;
 }
+
+void __init early_memunmap(void *addr, unsigned long size)
+{
+	early_iounmap((__force void __iomem *)addr, size);
+}
maintainer
#71 lore raw
Quoted 2 lines · Dave Young

I think for anyone who are using early_memremap should know this is a normal kernel memory instead of real __iomem

You can never ever assume that people are using kernel interfaces correctly.

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
author
#72 lore raw
Quoted 6 lines · Borislav Petkov
Quoted 2 lines · Dave Young

I think for anyone who are using early_memremap should know this is a normal kernel memory instead of real __iomem

You can never ever assume that people are using kernel interfaces correctly.

But the semantic of early_memreamp should be that it will just return a normal memory pointer instead of iomem so the fix to convert the internal implementation to return a __force __kernel make sense to me.

For iounmap I think since we have early_memremap, we have reason to add early_memunmap() so sparse will be happy.

Thanks
Dave