Skip to contentSkip to conversation

efi: arm64: Introduce /proc/efi/memreserve to tell the persistent pages

PATCH In review Masayoshi Mizuma 3 participants 5 messages Dec 3, 2019 – Dec 4, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover efi: arm64: Introduce /proc/efi/memreserve to tell the persistent pages

From: Masayoshi Mizuma <[email protected]>

kexec reboot sometime fails in early boot sequence on aarch64 machine. That is because kexec overwrites the LPI property tables and pending tables with the initrd.

To avoid the overwrite, introduce /proc/efi/memreserve to tell the tables region to kexec so that kexec can avoid the memory region to locate initrd.

kexec also needs a patch to handle /proc/efi/memreserve. I'm preparing the patch for kexec.

Changelog
v2: - Change memreserve file location from sysfs to procfs.
memreserve may exceed the PAGE_SIZE in case efi_memreserve_root has a lot of entries. So we cannot use sysfs_kf_seq_show(). Use seq_printf() in procfs instead.

Masayoshi Mizuma (2):
efi: add /proc/efi directory
efi: arm64: Introduce /proc/efi/memreserve to tell the persistent
pages

 drivers/firmware/efi/efi.c | 93 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 92 insertions(+), 1 deletion(-)
Signature · 7 lines
2.18.1


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

PATCH v2 1/2 efi: add /proc/efi directory

From: Masayoshi Mizuma <[email protected]>

Add /proc/efi directory to show some efi internal information.

---
 drivers/firmware/efi/efi.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
drivers/firmware/efi/efi.c +22 -0
index d101f072c..d8157cb34 100644
@@ -325,6 +325,22 @@ static __init int efivar_ssdt_load(void)
 static inline int efivar_ssdt_load(void) { return 0; }
 #endif
 
+#ifdef CONFIG_PROC_FS
+static struct proc_dir_entry *proc_efi;
+static int __init efi_proc_init(void)
+{
+	proc_efi = proc_mkdir("efi", NULL);
+	if (!proc_efi) {
+		pr_err("/proc/efi: Cannot create /proc/efi directory.\n");
+		return 1;
+	}
+
+	return 0;
+}
+#else
+static inline int efi_proc_init(void) { return 0; }
+#endif /* CONFIG_PROC_FS */
+
 /*
  * We register the efi subsystem with the firmware subsystem and the
  * efivars subsystem with the efi subsystem, if the system was booted with
@@ -381,6 +397,12 @@ static int __init efisubsys_init(void)
 		goto err_remove_group;
 	}
 
+	error = efi_proc_init();
+	if (error) {
+		sysfs_remove_mount_point(efi_kobj, "efivars");
+		goto err_remove_group;
+	}
+
 	return 0;
 
 err_remove_group:
Signature · 7 lines
2.18.1


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

PATCH v2 2/2 efi: arm64: Introduce /proc/efi/memreserve to tell the persistent pages

In reply to Masayoshi Mizuma

From: Masayoshi Mizuma <[email protected]>

kexec reboot stops in early boot sequence because efi_config_parse_tables() refers garbage data. We can see the log with memblock=debug kernel option:

efi: ACPI 2.0=0x9821790014 PROP=0x8757f5c0 SMBIOS 3.0=0x9820740000 MEMRESERVE=0x9820bfdc58
memblock_reserve: [0x0000009820bfdc58-0x0000009820bfdc67] efi_config_parse_tables+0x228/0x278
memblock_reserve: [0x0000000082760000-0x00000000324d07ff] efi_config_parse_tables+0x228/0x278
memblock_reserve: [0xcc4f84ecc0511670-0x5f6e5214a7fd91f9] efi_config_parse_tables+0x244/0x278
memblock_reserve: [0xd2fd4144b9af693d-0xad0c1db1086f40a2] efi_config_parse_tables+0x244/0x278
memblock_reserve: [0x0c719bb159b1fadc-0x5aa6e62a1417ce12] efi_config_parse_tables+0x244/0x278
...

That happens because 0x82760000, struct linux_efi_memreserve, is destroyed. 0x82760000 is pointed from efi.mem_reseve, and efi.mem_reserve points the head page of LPI pending table and LPI property table which are allocated by gic_reserve_range().

The destroyer is kexec. kexec locates the initrd to the area:

  ]# kexec -d -l /boot/vmlinuz-5.4.0-rc7 /boot/initramfs-5.4.0-rc7.img --reuse-cmdline
  ...
  initrd: base 82290000, size 388dd8ah (59301258)
  ...

From dynamic debug log. initrd is located in segment[1]:
machine_kexec_prepare:70:

    kexec kimage info:
      type:        0
      start:       85b30680
      head:        0
      nr_segments: 4
        segment[0]: 0000000080480000 - 0000000082290000, 0x1e10000 bytes, 481 pages
        segment[1]: 0000000082290000 - 0000000085b20000, 0x3890000 bytes, 905 pages
        segment[2]: 0000000085b20000 - 0000000085b30000, 0x10000 bytes, 1 pages
        segment[3]: 0000000085b30000 - 0000000085b40000, 0x10000 bytes, 1 pages

kexec searches the memory region to locate initrd through "System RAM" in /proc/iomem. The pending tables are included in "System RAM" because they are allocated by alloc_pages(), so kexec destroys the LPI pending tables.

Introduce /proc/efi/memreserve to tell the pages pointed by efi.mem_reserve so that kexec can avoid the area to locate initrd.

---
 drivers/firmware/efi/efi.c | 75 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 72 insertions(+), 3 deletions(-)
drivers/firmware/efi/efi.c +72 -3
index d8157cb34..80bbe0b3e 100644
@@ -325,17 +325,87 @@ static __init int efivar_ssdt_load(void)
 static inline int efivar_ssdt_load(void) { return 0; }
 #endif
 
+static struct linux_efi_memreserve *efi_memreserve_root __ro_after_init;
+
 #ifdef CONFIG_PROC_FS
 static struct proc_dir_entry *proc_efi;
+#ifdef CONFIG_KEXEC
+static int memreserve_show(struct seq_file *m, void *v)
+{
+	struct linux_efi_memreserve *rsv;
+	phys_addr_t start, end;
+	unsigned long prsv;
+	int count, i;
+
+	if ((efi_memreserve_root == (void *)ULONG_MAX) ||
+			(!efi_memreserve_root))
+		return -ENODEV;
+
+	for (prsv = efi_memreserve_root->next; prsv; prsv = rsv->next) {
+		rsv = memremap(prsv, sizeof(*rsv), MEMREMAP_WB);
+		if (!rsv) {
+			pr_err("Could not map efi_memreserve\n");
+			return -ENOMEM;
+		}
+		count = atomic_read(&rsv->count);
+		for (i = 0; i < count; i++) {
+			start = rsv->entry[i].base;
+			end = start + rsv->entry[i].size - 1;
+
+			seq_printf(m, "%pa-%pa\n", &start, &end);
+		}
+		memunmap(rsv);
+	}
+
+	return 0;
+}
+
+static int memreserve_open(struct inode *inode, struct file *filp)
+{
+	return single_open(filp, memreserve_show, NULL);
+}
+
+static const struct file_operations memreserve_fops = {
+	.owner		= THIS_MODULE,
+	.open		= memreserve_open,
+	.read		= seq_read,
+	.llseek		= seq_lseek,
+	.release	= single_release,
+};
+
+static int __init efi_proc_memreserve(void)
+{
+	struct proc_dir_entry *pde;
+
+	if ((efi_memreserve_root == (void *)ULONG_MAX) ||
+			(!efi_memreserve_root))
+		return 0;
+
+	pde = proc_create("memreserve", 0444, proc_efi, &memreserve_fops);
+	if (!pde) {
+		pr_err("/proc/efi: Cannot create /proc/efi/memreserve file.\n");
+		return 1;
+	}
+
+	return 0;
+}
+#else
+static inline int efi_proc_memreserve(void) { return 0; }
+#endif /* CONFIG_KEXEC */
+
 static int __init efi_proc_init(void)
 {
+	int error = 1;
+
 	proc_efi = proc_mkdir("efi", NULL);
 	if (!proc_efi) {
 		pr_err("/proc/efi: Cannot create /proc/efi directroy.\n");
-		return 1;
+		return error;
 	}
 
-	return 0;
+	error = efi_proc_memreserve();
+
+	return error;
 }
 #else
 static inline int efi_proc_init(void) { return 0; }
@@ -986,7 +1056,6 @@ int efi_status_to_err(efi_status_t status)
 }
 
 static DEFINE_SPINLOCK(efi_mem_reserve_persistent_lock);
-static struct linux_efi_memreserve *efi_memreserve_root __ro_after_init;
 
 static int __init efi_memreserve_map_root(void)
 {
Signature · 7 lines
2.18.1


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

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Masayoshi Mizuma
Sent: Wednesday, December 4, 2019 5:14 AM
To: Ard Biesheuvel <[email protected]>; [email protected]; [email protected]
Cc: Masayoshi Mizuma <[email protected]>; Mizuma, Masayoshi/水間 理仁 <[email protected]>; [email protected]; [email protected]; Hatayama, Daisuke/畑山 大輔 <[email protected]>; Eric Biederman <[email protected]>; Matthias Brugger <[email protected]>
Subject: [PATCH v2 2/2] efi: arm64: Introduce /proc/efi/memreserve to tell the persistent pages

From: Masayoshi Mizuma <[email protected]>

kexec reboot stops in early boot sequence because efi_config_parse_tables() refers garbage data. We can see the log with memblock=debug kernel option:

efi: ACPI 2.0=0x9821790014 PROP=0x8757f5c0 SMBIOS 3.0=0x9820740000
MEMRESERVE=0x9820bfdc58
memblock_reserve: [0x0000009820bfdc58-0x0000009820bfdc67]

efi_config_parse_tables+0x228/0x278
  memblock_reserve: [0x0000000082760000-0x00000000324d07ff]
efi_config_parse_tables+0x228/0x278
  memblock_reserve: [0xcc4f84ecc0511670-0x5f6e5214a7fd91f9]
efi_config_parse_tables+0x244/0x278
  memblock_reserve: [0xd2fd4144b9af693d-0xad0c1db1086f40a2]
efi_config_parse_tables+0x244/0x278
  memblock_reserve: [0x0c719bb159b1fadc-0x5aa6e62a1417ce12]
efi_config_parse_tables+0x244/0x278
  ...

That happens because 0x82760000, struct linux_efi_memreserve, is destroyed. 0x82760000 is pointed from efi.mem_reseve, and efi.mem_reserve points the head page of LPI pending table and LPI property table which are allocated by gic_reserve_range().

The destroyer is kexec. kexec locates the initrd to the area:

]# kexec -d -l /boot/vmlinuz-5.4.0-rc7 /boot/initramfs-5.4.0-rc7.img
--reuse-cmdline
...
initrd: base 82290000, size 388dd8ah (59301258)
...

From dynamic debug log. initrd is located in segment[1]:
machine_kexec_prepare:70:

    kexec kimage info:
      type:        0
      start:       85b30680
      head:        0
      nr_segments: 4
        segment[0]: 0000000080480000 - 0000000082290000, 0x1e10000 bytes, 481
pages
        segment[1]: 0000000082290000 - 0000000085b20000, 0x3890000 bytes, 905
pages
        segment[2]: 0000000085b20000 - 0000000085b30000, 0x10000 bytes, 1
pages
        segment[3]: 0000000085b30000 - 0000000085b40000, 0x10000 bytes, 1

pages

kexec searches the memory region to locate initrd through "System RAM" in /proc/iomem. The pending tables are included in "System RAM" because they are allocated by alloc_pages(), so kexec destroys the LPI pending tables.

Introduce /proc/efi/memreserve to tell the pages pointed by efi.mem_reserve so that kexec can avoid the area to locate initrd.

---
 drivers/firmware/efi/efi.c | 75 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 72 insertions(+), 3 deletions(-)
drivers/firmware/efi/efi.c +17 -0
index d8157cb34..80bbe0b3e 100644
@@ -325,17 +325,87 @@ static __init int efivar_ssdt_load(void)
 static inline int efivar_ssdt_load(void) { return 0; }
 #endif
 
+static struct linux_efi_memreserve *efi_memreserve_root __ro_after_init;
+
 #ifdef CONFIG_PROC_FS
 static struct proc_dir_entry *proc_efi;
+#ifdef CONFIG_KEXEC
+static int memreserve_show(struct seq_file *m, void *v)
+{
+	struct linux_efi_memreserve *rsv;
+	phys_addr_t start, end;
+	unsigned long prsv;
+	int count, i;
+
+	if ((efi_memreserve_root == (void *)ULONG_MAX) ||
+			(!efi_memreserve_root))
+		return -ENODEV;
+
+	for (prsv = efi_memreserve_root->next; prsv; prsv = rsv->next) {
+		rsv = memremap(prsv, sizeof(*rsv), MEMREMAP_WB);
+		if (!rsv) {

drivers/firmware/efi/efi.c static __init int efivar_ssdt_load(void) · line 356

Quoted 10 lines
drivers/firmware/efi/efi.c +10 -0
+			pr_err("Could not map efi_memreserve\n");
+			return -ENOMEM;
+		}
+		count = atomic_read(&rsv->count);
+		for (i = 0; i < count; i++) {
+			start = rsv->entry[i].base;
+			end = start + rsv->entry[i].size - 1;
+
+			seq_printf(m, "%pa-%pa\n", &start, &end);
+		}

It looks to me that we reach the same issue as sysfs if memreserve_show() calls seq_printf multiple times this way. Default buffer size of struct seq_file is also PAGE_SIZE. We can configure default buffer size using single_open_size(), but I think it better to do the same thing as /proc/iomem (and /proc/mounts and many others) than choosing a certain size as default value.

Looking into implementation of /proc/iomem, its show method, r_show(), calls seq_printf() only once; in other words, r_show() is called the number of lines of /proc/iomem, which is equal to the number of resource objects. Then, there is no buffer size issue because "%pa-%pa\n" consumes at most (16 + 16 + 2) bytes per line.

How about doing as /proc/iomem?

Quoted 67 lines
drivers/firmware/efi/efi.c +45 -2
+		memunmap(rsv);
+	}
+
+	return 0;
+}
+
+static int memreserve_open(struct inode *inode, struct file *filp)
+{
+	return single_open(filp, memreserve_show, NULL);
+}
+
+static const struct file_operations memreserve_fops = {
+	.owner		= THIS_MODULE,
+	.open		= memreserve_open,
+	.read		= seq_read,
+	.llseek		= seq_lseek,
+	.release	= single_release,
+};
+
+static int __init efi_proc_memreserve(void)
+{
+	struct proc_dir_entry *pde;
+
+	if ((efi_memreserve_root == (void *)ULONG_MAX) ||
+			(!efi_memreserve_root))
+		return 0;
+
+	pde = proc_create("memreserve", 0444, proc_efi, &memreserve_fops);
+	if (!pde) {
+		pr_err("/proc/efi: Cannot create /proc/efi/memreserve
 file.\n");
+		return 1;
+	}
+
+	return 0;
+}
+#else
+static inline int efi_proc_memreserve(void) { return 0; }
+#endif /* CONFIG_KEXEC */
+
 static int __init efi_proc_init(void)
 {
+	int error = 1;
+
 	proc_efi = proc_mkdir("efi", NULL);
 	if (!proc_efi) {
 		pr_err("/proc/efi: Cannot create /proc/efi directroy.\n");
-		return 1;
+		return error;
 	}
 
-	return 0;
+	error = efi_proc_memreserve();
+
+	return error;
 }
 #else

static inline int efi_proc_init(void) { return 0; }

drivers/firmware/efi/efi.c +0 -2
@@ -986,7 +1056,6 @@ int efi_status_to_err(efi_status_t status)
 }
 
 static DEFINE_SPINLOCK(efi_mem_reserve_persistent_lock);
-static struct linux_efi_memreserve *efi_memreserve_root __ro_after_init;
 
 static int __init efi_memreserve_map_root(void)
 {
--
Quoted 1 line

2.18.1

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

#4 lore raw
In reply to Masayoshi Mizuma
Quoted 20 lines · Masayoshi Mizuma

From: Masayoshi Mizuma <[email protected]>

kexec reboot sometime fails in early boot sequence on aarch64 machine. That is because kexec overwrites the LPI property tables and pending tables with the initrd.

To avoid the overwrite, introduce /proc/efi/memreserve to tell the tables region to kexec so that kexec can avoid the memory region to locate initrd.

kexec also needs a patch to handle /proc/efi/memreserve. I'm preparing the patch for kexec.

Changelog
v2: - Change memreserve file location from sysfs to procfs.
memreserve may exceed the PAGE_SIZE in case efi_memreserve_root has a lot of entries. So we cannot use sysfs_kf_seq_show(). Use seq_printf() in procfs instead.

Quoted 5 lines

Masayoshi Mizuma (2):
efi: add /proc/efi directory
efi: arm64: Introduce /proc/efi/memreserve to tell the persistent
pages

Apologies for the tardy response.

Adding /proc/efi is really out of the question. *If* we add any special files to expose this information, it should be under sysfs.

However, this is still only a partial solution, since it only solves the problem for userspace based kexec, and we need something for kexec_file_load() as well.

The fundamental issue here is that /proc/iomem apparently lacks the entries that describe these regions as 'reserved', so we should try to address that instead.

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