Skip to contentSkip to conversation

efi: arm64: Introduce /sys/firmware/efi/memreserve to tell the persistent pages

RFC PATCH Superseded Masayoshi Mizuma 3 participants 4 messages Nov 12, 2019 – Nov 13, 2019

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 efi: arm64: Introduce /sys/firmware/efi/memreserve to tell the persistent pages

From: Masayoshi Mizuma <[email protected]>

kexec reboot stucks because efi_config_parse_tables() refers garbage
(with memblock=debug):

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 pending table and prop table which are allocated by gic_reserve_range().

The destroyer is kexec. kexec locates the inird 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:
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 appropriate 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 pending tables.

Introduce /sys/firmware/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 | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)
drivers/firmware/efi/efi.c +31 -1
index e98bbf8e5..67b21ae7a 100644
@@ -141,6 +141,36 @@ static ssize_t systab_show(struct kobject *kobj,
 
 static struct kobj_attribute efi_attr_systab = __ATTR_RO_MODE(systab, 0400);
 
+static struct linux_efi_memreserve *efi_memreserve_root __ro_after_init;
+static ssize_t memreserve_show(struct kobject *kobj,
+			   struct kobj_attribute *attr, char *buf)
+{
+	struct linux_efi_memreserve *rsv;
+	unsigned long prsv;
+	char *str = buf;
+	int index, i;
+
+	if (!kobj || !buf)
+		return -EINVAL;
+
+	if (!efi_memreserve_root)
+		return -ENODEV;
+
+	for (prsv = efi_memreserve_root->next; prsv; prsv = rsv->next) {
+		rsv = memremap(prsv, sizeof(*rsv), MEMREMAP_WB);
+		index = atomic_read(&rsv->count);
+		for (i = 0; i < index; i++)
+			str += sprintf(str, "%llx-%llx\n",
+				rsv->entry[i].base,
+				rsv->entry[i].base + rsv->entry[i].size - 1);
+		memunmap(rsv);
+	}
+
+	return str - buf;
+}
+
+static struct kobj_attribute efi_attr_memreserve = __ATTR_RO_MODE(memreserve, 0444);
+
 #define EFI_FIELD(var) efi.var
 
 #define EFI_ATTR_SHOW(name) \
@@ -172,6 +202,7 @@ static struct attribute *efi_subsys_attrs[] = {
 	&efi_attr_runtime.attr,
 	&efi_attr_config_table.attr,
 	&efi_attr_fw_platform_size.attr,
+	&efi_attr_memreserve.attr,
 	NULL,
 };
 
@@ -955,7 +986,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
#1 lore raw
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please…

Hi Masayoshi,

[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on efi/next]
[cannot apply to v5.4-rc7 next-20191112]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Masayoshi-Mizuma/efi-arm64-Introduce-sys-firmware-efi-memreserve-to-tell-the-persistent-pages/20191113-132315
base:   https://git.kernel.org/pub/scm/linux/kernel/git/efi/efi.git next
config: i386-defconfig (attached as .config)
compiler: gcc-7 (Debian 7.4.0-14) 7.4.0
reproduce:
        # save the attached .config to linux build tree
        make ARCH=i386 

If you fix the issue, kindly add following tag

All warnings (new ones prefixed by >>):

drivers/firmware/efi/efi.c: In function 'memreserve_show':

>> drivers/firmware/efi/efi.c:163:28: warning: format '%llx' expects argument of type 'long long unsigned int', but argument 3 has type 'phys_addr_t {aka unsigned int}' [-Wformat=]
       str += sprintf(str, "%llx-%llx\n",
                            ~~~^
                            %x
        rsv->entry[i].base,
        ~~~~~~~~~~~~~~~~~~      
   drivers/firmware/efi/efi.c:163:33: warning: format '%llx' expects argument of type 'long long unsigned int', but argument 4 has type 'phys_addr_t {aka unsigned int}' [-Wformat=]
       str += sprintf(str, "%llx-%llx\n",
                                 ~~~^
                                 %x
   drivers/firmware/efi/efi.c:165:5:
        rsv->entry[i].base + rsv->entry[i].size - 1);
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

vim +163 drivers/firmware/efi/efi.c

143
144 static struct linux_efi_memreserve *efi_memreserve_root __ro_after_init;
145 static ssize_t memreserve_show(struct kobject *kobj,
146 struct kobj_attribute *attr, char *buf)
147 {
148 struct linux_efi_memreserve *rsv;
149 unsigned long prsv;
150 char *str = buf;
151 int index, i;
152
153 if (!kobj || !buf)
154 return -EINVAL;
155
156 if (!efi_memreserve_root)
157 return -ENODEV;
158
159 for (prsv = efi_memreserve_root->next; prsv; prsv = rsv->next) {
160 rsv = memremap(prsv, sizeof(*rsv), MEMREMAP_WB);
161 index = atomic_read(&rsv->count);
162 for (i = 0; i < index; i++)

 > 163				str += sprintf(str, "%llx-%llx\n",

164 rsv->entry[i].base,
165 rsv->entry[i].base + rsv->entry[i].size - 1);
166 memunmap(rsv);
167 }
168
169 return str - buf;
170 }
171

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org Intel Corporation

#2 lore raw
In reply to Masayoshi Mizuma
Quoted 89 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Masayoshi Mizuma
Sent: Wednesday, November 13, 2019 1:53 AM
To: Ard Biesheuvel <[email protected]>; [email protected]; [email protected]
Cc: Masayoshi Mizuma <[email protected]>; Mizuma, Masayoshi/水間 理仁 <[email protected]>; [email protected]; [email protected]
Subject: [RFC PATCH] efi: arm64: Introduce /sys/firmware/efi/memreserve to tell the persistent pages

From: Masayoshi Mizuma <[email protected]>

kexec reboot stucks because efi_config_parse_tables() refers garbage
(with memblock=debug):

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 pending table and prop table which are allocated by gic_reserve_range().

The destroyer is kexec. kexec locates the inird 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:
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 appropriate 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 pending tables.

Introduce /sys/firmware/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 | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)
drivers/firmware/efi/efi.c +4 -0
index e98bbf8e5..67b21ae7a 100644
@@ -141,6 +141,36 @@ static ssize_t systab_show(struct kobject *kobj,
 
 static struct kobj_attribute efi_attr_systab = __ATTR_RO_MODE(systab, 0400);
 
+static struct linux_efi_memreserve *efi_memreserve_root __ro_after_init;
+static ssize_t memreserve_show(struct kobject *kobj,
+			   struct kobj_attribute *attr, char *buf)
+{

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

Quoted 10 lines
drivers/firmware/efi/efi.c +10 -0
+	struct linux_efi_memreserve *rsv;
+	unsigned long prsv;
+	char *str = buf;
+	int index, i;
+
+	if (!kobj || !buf)
+		return -EINVAL;
+
+	if (!efi_memreserve_root)
+		return -ENODEV;

Other functions use different conditions.
The latter efi_memreserve_root == (void *)ULONG_MAX is correct?

static int __init efi_memreserve_map_root(void)

{
        if (efi.mem_reserve == EFI_INVALID_TABLE_ADDR)
                return -ENODEV;

int __ref efi_mem_reserve_persistent(phys_addr_t addr, u64 size)

{
        struct linux_efi_memreserve *rsv;
        unsigned long prsv;
        int rc, index;

        if (efi_memreserve_root == (void *)ULONG_MAX)
                return -ENODEV;

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

Quoted 3 lines
drivers/firmware/efi/efi.c +3 -0
+
+	for (prsv = efi_memreserve_root->next; prsv; prsv = rsv->next) {
+		rsv = memremap(prsv, sizeof(*rsv), MEMREMAP_WB);

memremap() could fail with NULL as a return value. You need to deal with such case.

It looks to me efi_mem_reserve_persistent() also doesn't deal with this. Maybe you should fix this, too.

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

Quoted 6 lines
drivers/firmware/efi/efi.c +5 -0
+		index = atomic_read(&rsv->count);
+		for (i = 0; i < index; i++)
+			str += sprintf(str, "%llx-%llx\n",
+				rsv->entry[i].base,
+				rsv->entry[i].base + rsv->entry[i].size -
 1);

Is memreserve supported on 32-bit system?
If so, phy_addr_t could have a type of 4-byte length in such system (not so if with PAE) and then %llx could lead to inconsistent type error. It's enough to add a cast to unsigned long long.

Quoted 29 lines
drivers/firmware/efi/efi.c +8 -0
+		memunmap(rsv);
+	}
+
+	return str - buf;
+}
+
+static struct kobj_attribute efi_attr_memreserve =
 __ATTR_RO_MODE(memreserve, 0444);
+
 #define EFI_FIELD(var) efi.var

#define EFI_ATTR_SHOW(name) \

drivers/firmware/efi/efi.c +1 -2
@@ -172,6 +202,7 @@ static struct attribute *efi_subsys_attrs[] = {
 	&efi_attr_runtime.attr,
 	&efi_attr_config_table.attr,
 	&efi_attr_fw_platform_size.attr,
+	&efi_attr_memreserve.attr,
 	NULL,
 };
 
@@ -955,7 +986,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

#3 lore raw
Quoted 90 lines · On Wed, Nov 13, 2019 at 07:59:04AM +0000, [email protected] wrote:
Quoted 75 lines

From: Masayoshi Mizuma <[email protected]>

kexec reboot stucks because efi_config_parse_tables() refers garbage
(with memblock=debug):

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 pending table and prop table which are allocated by gic_reserve_range().

The destroyer is kexec. kexec locates the inird 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:
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 appropriate 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 pending tables.

Introduce /sys/firmware/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 | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)
drivers/firmware/efi/efi.c +4 -0
index e98bbf8e5..67b21ae7a 100644
@@ -141,6 +141,36 @@ static ssize_t systab_show(struct kobject *kobj,
 
 static struct kobj_attribute efi_attr_systab = __ATTR_RO_MODE(systab, 0400);
 
+static struct linux_efi_memreserve *efi_memreserve_root __ro_after_init;
+static ssize_t memreserve_show(struct kobject *kobj,
+			   struct kobj_attribute *attr, char *buf)
+{

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

Quoted 10 lines
drivers/firmware/efi/efi.c +10 -0
+	struct linux_efi_memreserve *rsv;
+	unsigned long prsv;
+	char *str = buf;
+	int index, i;
+
+	if (!kobj || !buf)
+		return -EINVAL;
+
+	if (!efi_memreserve_root)
+		return -ENODEV;

Other functions use different conditions.
The latter efi_memreserve_root == (void *)ULONG_MAX is correct?

static int __init efi_memreserve_map_root(void)

Quoted 11 lines
{
        if (efi.mem_reserve == EFI_INVALID_TABLE_ADDR)
                return -ENODEV;
int __ref efi_mem_reserve_persistent(phys_addr_t addr, u64 size)
{
        struct linux_efi_memreserve *rsv;
        unsigned long prsv;
        int rc, index;

        if (efi_memreserve_root == (void *)ULONG_MAX)
                return -ENODEV;

I think it's better to add both checks like as:

        if ((efi_memreserve_root == (void *)ULONG_MAX) ||
                        (!efi_memreserve_root))
                return -ENODEV;
Quoted 10 lines

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

Quoted 4 lines
drivers/firmware/efi/efi.c +3 -0
+
+	for (prsv = efi_memreserve_root->next; prsv; prsv = rsv->next) {
+		rsv = memremap(prsv, sizeof(*rsv), MEMREMAP_WB);

memremap() could fail with NULL as a return value. You need to deal with such case.

It looks to me efi_mem_reserve_persistent() also doesn't deal with this. Maybe you should fix this, too.

OK, I'll post a patch sepaletely to add the sanity check.

Quoted 12 lines

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

Quoted 7 lines
drivers/firmware/efi/efi.c +5 -0
+		index = atomic_read(&rsv->count);
+		for (i = 0; i < index; i++)
+			str += sprintf(str, "%llx-%llx\n",
+				rsv->entry[i].base,
+				rsv->entry[i].base + rsv->entry[i].size -
 1);

Is memreserve supported on 32-bit system?
If so, phy_addr_t could have a type of 4-byte length in such system (not so if with PAE) and then %llx could lead to inconsistent type error. It's enough to add a cast to unsigned long long.

Good catch. I'll modify the sprintf() format to
use %pa like as:

phys_addr_t start, end;

  for (i = 0; i < index; i++) {
          start = rsv->entry[i].base;
          end = start + rsv->entry[i].size - 1;
          str += sprintf(str, "%pa-%pa\n", &start, &end);

}

Thanks,
Masa

Quoted 24 lines
Quoted 24 lines
drivers/firmware/efi/efi.c +8 -0
+		memunmap(rsv);
+	}
+
+	return str - buf;
+}
+
+static struct kobj_attribute efi_attr_memreserve =
 __ATTR_RO_MODE(memreserve, 0444);
+
 #define EFI_FIELD(var) efi.var

#define EFI_ATTR_SHOW(name) \

drivers/firmware/efi/efi.c +1 -0
@@ -172,6 +202,7 @@ static struct attribute *efi_subsys_attrs[] = {
 	&efi_attr_runtime.attr,
 	&efi_attr_config_table.attr,
 	&efi_attr_fw_platform_size.attr,
+	&efi_attr_memreserve.attr,
 	NULL,
 };
 
@@ -955,7 +986,6 @@ int efi_status_to_err(efi_status_t status)
 }
 
Quoted 8 lines
Quoted 7 lines
drivers/firmware/efi/efi.c +0 -2
 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)
 {
--

2.18.1

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