Skip to contentSkip to conversation

efi: arm-stub: Correct FDT and initrd allocation rules for arm64

PATCH New Ard Biesheuvel 5 participants 12 messages Feb 9, 2017 – Mar 20, 2017 1 message not in the archive

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

PATCH v2 1/2 efi: arm-stub: Correct FDT and initrd allocation rules for arm64

On arm64, we have made some changes over the past year to the way the kernel itself is allocated and to how it deals with the initrd and FDT. This patch brings the allocation logic in the EFI stub in line with that, which is necessary because the introduction of KASLR has created the possibility for the initrd to be allocated in a place where the kernel may not be able to map it. (This is mostly a theoretical scenario, since it only affects systems where the physical memory footprint exceeds the size of the linear mapping.)

Since we know the kernel itself will be covered by the linear mapping, choose a suitably sized window (i.e., based on the size of the linear region) covering the kernel when allocating memory for the initrd.

The FDT may be anywhere in memory on arm64 now that we map it via the fixmap, so we can lift the address restriction there completely.

---
 arch/arm/include/asm/efi.h              | 14 +++++++++++++-
 arch/arm64/include/asm/efi.h            | 18 +++++++++++++++++-
 drivers/firmware/efi/libstub/arm-stub.c |  7 ++++---
 3 files changed, 34 insertions(+), 5 deletions(-)
arch/arm/include/asm/efi.h +13 -1
index 0b06f5341b45..2de0195dfd1e 100644
@@ -84,6 +84,18 @@ static inline void efifb_setup_from_dmi(struct screen_info *si, const char *opt)
  */
 #define ZIMAGE_OFFSET_LIMIT	SZ_128M
 #define MIN_ZIMAGE_OFFSET	MAX_UNCOMP_KERNEL_SIZE
-#define MAX_FDT_OFFSET		ZIMAGE_OFFSET_LIMIT
+
+/* on ARM, the FDT should be located in the first 128 MB of RAM */
+static inline unsigned long efi_get_max_fdt_addr(unsigned long dram_base)
+{
+	return dram_base + ZIMAGE_OFFSET_LIMIT;
+}
+
+/* on ARM, the initrd should be loaded in a lowmem region */
+static inline unsigned long efi_get_max_initrd_addr(unsigned long dram_base,
+						    unsigned long image_addr)
+{
+	return dram_base + SZ_512M;
+}
 
 #endif /* _ASM_ARM_EFI_H */
arch/arm64/include/asm/efi.h +17 -1
index 0b6b1633017f..342e90d6d204 100644
@@ -46,7 +46,23 @@ int efi_set_mapping_permissions(struct mm_struct *mm, efi_memory_desc_t *md);
  * 2MiB so we know it won't cross a 2MiB boundary.
  */
 #define EFI_FDT_ALIGN	SZ_2M   /* used by allocate_new_fdt_and_exit_boot() */
-#define MAX_FDT_OFFSET	SZ_512M
+
+/* on arm64, the FDT may be located anywhere in system RAM */
+static inline unsigned long efi_get_max_fdt_addr(unsigned long dram_base)
+{
+	return ULONG_MAX;
+}
+
+/*
+ * On arm64, we have to ensure that the initrd ends up in the linear region,
+ * which is a 1 GB aligned region of size '1UL << (VA_BITS - 1)' that is
+ * guaranteed to cover the kernel Image.
+ */
+static inline unsigned long efi_get_max_initrd_addr(unsigned long dram_base,
+						    unsigned long image_addr)
+{
+	return (image_addr & ~(SZ_1G - 1UL)) + (1UL << (VA_BITS - 1));
+}
 
 #define efi_call_early(f, ...)		sys_table_arg->boottime->f(__VA_ARGS__)
 #define __efi_call_early(f, ...)	f(__VA_ARGS__)
drivers/firmware/efi/libstub/arm-stub.c +4 -3
index b4f7d78f9e8b..557281fe375f 100644
@@ -333,8 +333,9 @@ unsigned long efi_entry(void *handle, efi_system_table_t *sys_table,
 	if (!fdt_addr)
 		pr_efi(sys_table, "Generating empty DTB\n");
 
-	status = handle_cmdline_files(sys_table, image, cmdline_ptr,
-				      "initrd=", dram_base + SZ_512M,
+	status = handle_cmdline_files(sys_table, image, cmdline_ptr, "initrd=",
+				      efi_get_max_initrd_addr(dram_base,
+							      *image_addr),
 				      (unsigned long *)&initrd_addr,
 				      (unsigned long *)&initrd_size);
 	if (status != EFI_SUCCESS)
@@ -344,7 +345,7 @@ unsigned long efi_entry(void *handle, efi_system_table_t *sys_table,
 
 	new_fdt_addr = fdt_addr;
 	status = allocate_new_fdt_and_exit_boot(sys_table, handle,
-				&new_fdt_addr, dram_base + MAX_FDT_OFFSET,
+				&new_fdt_addr, efi_get_max_fdt_addr(dram_base),
 				initrd_addr, initrd_size, cmdline_ptr,
 				fdt_addr, fdt_size);
 
Signature · 1 line
2.7.4
#1 lore raw

PATCH v2 2/2 efi: arm-stub: Round up FDT allocation to mapping size

The FDT is mapped via a fixmap entry that is at least 2 MB in size and 2 MB aligned on 4 KB page size kernels.

On UEFI systems, the FDT allocation may share this 2 MB block with a reserved region, or another memory region that we should never map, unless we account for this in the size of the allocation (the alignment is already 2 MB)

So instead of taking guesses at the needed space, simply allocate 2 MB immediately. The allocation will be recorded as a EFI_LOADER_DATA, and the kernel only memblock_reserve()'s the actual size of the FDT, so the unused space will be released to the kernel.

---
 arch/arm64/include/asm/efi.h       |  1 +
 drivers/firmware/efi/libstub/fdt.c | 57 +++++++++-----------
 2 files changed, 25 insertions(+), 33 deletions(-)
arch/arm64/include/asm/efi.h +1 -0
index 342e90d6d204..f642565fc1d0 100644
@@ -1,6 +1,7 @@
 #ifndef _ASM_EFI_H
 #define _ASM_EFI_H
 
+#include <asm/boot.h>
 #include <asm/cpufeature.h>
 #include <asm/io.h>
 #include <asm/mmu_context.h>
drivers/firmware/efi/libstub/fdt.c +24 -33

85 lines not on this page · raw message

Signature · 1 line
2.7.4

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw

PATCH v2 2/2 efi: arm-stub: Round up FDT allocation to mapping size

Quoted 23 lines · Richard

On 2/9/2017 2:42 PM, Ard Biesheuvel wrote:

The FDT is mapped via a fixmap entry that is at least 2 MB in size and 2 MB aligned on 4 KB page size kernels.

On UEFI systems, the FDT allocation may share this 2 MB block with a reserved region, or another memory region that we should never map, unless we account for this in the size of the allocation (the alignment is already 2 MB)

So instead of taking guesses at the needed space, simply allocate 2 MB immediately. The allocation will be recorded as a EFI_LOADER_DATA, and the kernel only memblock_reserve()'s the actual size of the FDT, so the unused space will be released to the kernel.

---
 arch/arm64/include/asm/efi.h       |  1 +
 drivers/firmware/efi/libstub/fdt.c | 57 +++++++++-----------
 2 files changed, 25 insertions(+), 33 deletions(-)

[..]

Quoted 5 lines

Tested successfully on our QDT2400 system on which we were seeing a failure to allocate memory for initrd.

Thanks

#4 lore raw

PATCH v2 2/2 efi: arm-stub: Round up FDT allocation to mapping size

In reply to Ard Biesheuvel
Quoted 8 lines · Ard Biesheuvel

The FDT is mapped via a fixmap entry that is at least 2 MB in size and 2 MB aligned on 4 KB page size kernels.

On UEFI systems, the FDT allocation may share this 2 MB block with a reserved region, or another memory region that we should never map, unless we account for this in the size of the allocation (the alignment is already 2 MB)

Quoted 7 lines

So instead of taking guesses at the needed space, simply allocate 2 MB immediately. The allocation will be recorded as a EFI_LOADER_DATA, and the kernel only memblock_reserve()'s the actual size of the FDT, so the unused space will be released to the kernel.

---

Quoted 110 lines
 arch/arm64/include/asm/efi.h       |  1 +
 drivers/firmware/efi/libstub/fdt.c | 57 +++++++++-----------
 2 files changed, 25 insertions(+), 33 deletions(-)
arch/arm64/include/asm/efi.h +1 -0
index 342e90d6d204..f642565fc1d0 100644
@@ -1,6 +1,7 @@
 #ifndef _ASM_EFI_H
 #define _ASM_EFI_H
 
+#include <asm/boot.h>
 #include <asm/cpufeature.h>
 #include <asm/io.h>
 #include <asm/mmu_context.h>
drivers/firmware/efi/libstub/fdt.c +24 -33
index 260c4b4b492e..41f457be64e8 100644
@@ -206,6 +206,10 @@ static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg,
 	return update_fdt_memmap(p->new_fdt_addr, map);
 }
 
+#ifndef MAX_FDT_SIZE
+#define MAX_FDT_SIZE	SZ_2M
+#endif
+
 /*
  * Allocate memory for a new FDT, then add EFI, commandline, and
  * initrd related fields to the FDT.  This routine increases the
@@ -233,7 +237,6 @@ efi_status_t allocate_new_fdt_and_exit_boot(efi_system_table_t *sys_table,
 	u32 desc_ver;
 	unsigned long mmap_key;
 	efi_memory_desc_t *memory_map, *runtime_map;
-	unsigned long new_fdt_size;
 	efi_status_t status;
 	int runtime_entry_count = 0;
 	struct efi_boot_memmap map;
@@ -262,41 +265,29 @@ efi_status_t allocate_new_fdt_and_exit_boot(efi_system_table_t *sys_table,
 	       "Exiting boot services and installing virtual address map...\n");
 
 	map.map = &memory_map;
+	status = efi_high_alloc(sys_table, MAX_FDT_SIZE, EFI_FDT_ALIGN,
+				new_fdt_addr, max_addr);
+	if (status != EFI_SUCCESS) {
+		pr_efi_err(sys_table,
+			   "Unable to allocate memory for new device tree.\n");
+		goto fail;
+	}
+
 	/*
-	 * Estimate size of new FDT, and allocate memory for it. We
-	 * will allocate a bigger buffer if this ends up being too
-	 * small, so a rough guess is OK here.
+	 * Now that we have done our final memory allocation (and free)
+	 * we can get the memory map key needed for exit_boot_services().
 	 */
-	new_fdt_size = fdt_size + EFI_PAGE_SIZE;
-	while (1) {
-		status = efi_high_alloc(sys_table, new_fdt_size, EFI_FDT_ALIGN,
-					new_fdt_addr, max_addr);
-		if (status != EFI_SUCCESS) {
-			pr_efi_err(sys_table, "Unable to allocate memory for new device tree.\n");
-			goto fail;
-		}
-
-		status = update_fdt(sys_table,
-				    (void *)fdt_addr, fdt_size,
-				    (void *)*new_fdt_addr, new_fdt_size,
-				    cmdline_ptr, initrd_addr, initrd_size);
+	status = efi_get_memory_map(sys_table, &map);
+	if (status != EFI_SUCCESS)
+		goto fail_free_new_fdt;
 
-		/* Succeeding the first time is the expected case. */
-		if (status == EFI_SUCCESS)
-			break;
+	status = update_fdt(sys_table, (void *)fdt_addr, fdt_size,
+			    (void *)*new_fdt_addr, MAX_FDT_SIZE, cmdline_ptr,
+			    initrd_addr, initrd_size);
 
-		if (status == EFI_BUFFER_TOO_SMALL) {
-			/*
-			 * We need to allocate more space for the new
-			 * device tree, so free existing buffer that is
-			 * too small.
-			 */
-			efi_free(sys_table, new_fdt_size, *new_fdt_addr);
-			new_fdt_size += EFI_PAGE_SIZE;
-		} else {
-			pr_efi_err(sys_table, "Unable to construct new device tree.\n");
-			goto fail_free_new_fdt;
-		}
+	if (status != EFI_SUCCESS) {
+		pr_efi_err(sys_table, "Unable to construct new device tree.\n");
+		goto fail_free_new_fdt;
 	}
 
 	priv.runtime_map = runtime_map;
@@ -340,7 +331,7 @@ efi_status_t allocate_new_fdt_and_exit_boot(efi_system_table_t *sys_table,
 	pr_efi_err(sys_table, "Exit boot services failed.\n");
 
 fail_free_new_fdt:
-	efi_free(sys_table, new_fdt_size, *new_fdt_addr);
+	efi_free(sys_table, MAX_FDT_SIZE, *new_fdt_addr);
 
 fail:
 	sys_table->boottime->free_pool(runtime_map);
Signature · 5 lines
Jeffrey Hugo
Qualcomm Datacenter Technologies as an affiliate of Qualcomm 
Technologies, Inc.
Qualcomm Technologies, Inc. is a member of the
Code Aurora Forum, a Linux Foundation Collaborative Project.
#5 lore raw

PATCH v2 2/2 efi: arm-stub: Round up FDT allocation to mapping size

Quoted 15 lines · Jeffrey Hugo
Quoted 14 lines · Ard Biesheuvel

The FDT is mapped via a fixmap entry that is at least 2 MB in size and 2 MB aligned on 4 KB page size kernels.

On UEFI systems, the FDT allocation may share this 2 MB block with a reserved region, or another memory region that we should never map, unless we account for this in the size of the allocation (the alignment is already 2 MB)

So instead of taking guesses at the needed space, simply allocate 2 MB immediately. The allocation will be recorded as a EFI_LOADER_DATA, and the kernel only memblock_reserve()'s the actual size of the FDT, so the unused space will be released to the kernel.

Quoted 6 lines
Quoted 2 lines

---

Thanks, Jeffrey.

Mark: anything to add? Thanks.

Quoted 133 lines
Quoted 122 lines
 arch/arm64/include/asm/efi.h       |  1 +
 drivers/firmware/efi/libstub/fdt.c | 57 +++++++++-----------
 2 files changed, 25 insertions(+), 33 deletions(-)
arch/arm64/include/asm/efi.h +1 -0
index 342e90d6d204..f642565fc1d0 100644
@@ -1,6 +1,7 @@
 #ifndef _ASM_EFI_H
 #define _ASM_EFI_H
 
+#include <asm/boot.h>
 #include <asm/cpufeature.h>
 #include <asm/io.h>
 #include <asm/mmu_context.h>
drivers/firmware/efi/libstub/fdt.c +4 -0
index 260c4b4b492e..41f457be64e8 100644
@@ -206,6 +206,10 @@ static efi_status_t exit_boot_func(efi_system_table_t
 *sys_table_arg,
        return update_fdt_memmap(p->new_fdt_addr, map);
 }
 
+#ifndef MAX_FDT_SIZE
+#define MAX_FDT_SIZE   SZ_2M
+#endif
+
 /*
  * Allocate memory for a new FDT, then add EFI, commandline, and

* initrd related fields to the FDT. This routine increases the

drivers/firmware/efi/libstub/fdt.c +0 -1
@@ -233,7 +237,6 @@ efi_status_t
 allocate_new_fdt_and_exit_boot(efi_system_table_t *sys_table,
        u32 desc_ver;
        unsigned long mmap_key;
        efi_memory_desc_t *memory_map, *runtime_map;
-       unsigned long new_fdt_size;
        efi_status_t status;
        int runtime_entry_count = 0;
        struct efi_boot_memmap map;
drivers/firmware/efi/libstub/fdt.c +16 -29
@@ -262,41 +265,29 @@ efi_status_t
 allocate_new_fdt_and_exit_boot(efi_system_table_t *sys_table,
               "Exiting boot services and installing virtual address
 map...\n");
 
        map.map = &memory_map;
+       status = efi_high_alloc(sys_table, MAX_FDT_SIZE, EFI_FDT_ALIGN,
+                               new_fdt_addr, max_addr);
+       if (status != EFI_SUCCESS) {
+               pr_efi_err(sys_table,
+                          "Unable to allocate memory for new device
 tree.\n");
+               goto fail;
+       }
+
        /*
-        * Estimate size of new FDT, and allocate memory for it. We
-        * will allocate a bigger buffer if this ends up being too
-        * small, so a rough guess is OK here.
+        * Now that we have done our final memory allocation (and free)
+        * we can get the memory map key needed for exit_boot_services().
         */
-       new_fdt_size = fdt_size + EFI_PAGE_SIZE;
-       while (1) {
-               status = efi_high_alloc(sys_table, new_fdt_size,
 EFI_FDT_ALIGN,
-                                       new_fdt_addr, max_addr);
-               if (status != EFI_SUCCESS) {
-                       pr_efi_err(sys_table, "Unable to allocate memory
 for new device tree.\n");
-                       goto fail;
-               }
-
-               status = update_fdt(sys_table,
-                                   (void *)fdt_addr, fdt_size,
-                                   (void *)*new_fdt_addr, new_fdt_size,
-                                   cmdline_ptr, initrd_addr,
 initrd_size);
+       status = efi_get_memory_map(sys_table, &map);
+       if (status != EFI_SUCCESS)
+               goto fail_free_new_fdt;
 
-               /* Succeeding the first time is the expected case. */
-               if (status == EFI_SUCCESS)
-                       break;
+       status = update_fdt(sys_table, (void *)fdt_addr, fdt_size,
+                           (void *)*new_fdt_addr, MAX_FDT_SIZE,
 cmdline_ptr,
+                           initrd_addr, initrd_size);
 
-               if (status == EFI_BUFFER_TOO_SMALL) {
-                       /*
-                        * We need to allocate more space for the new
-                        * device tree, so free existing buffer that is
-                        * too small.
-                        */
-                       efi_free(sys_table, new_fdt_size, *new_fdt_addr);
-                       new_fdt_size += EFI_PAGE_SIZE;
-               } else {
-                       pr_efi_err(sys_table, "Unable to construct new

device tree.\n");

drivers/firmware/efi/libstub/fdt.c +2 -2
-                       goto fail_free_new_fdt;
-               }
+       if (status != EFI_SUCCESS) {
+               pr_efi_err(sys_table, "Unable to construct new device

tree.\n");

drivers/firmware/efi/libstub/fdt.c +2 -1
+               goto fail_free_new_fdt;
        }
 
        priv.runtime_map = runtime_map;
@@ -340,7 +331,7 @@ efi_status_t
 allocate_new_fdt_and_exit_boot(efi_system_table_t *sys_table,
        pr_efi_err(sys_table, "Exit boot services failed.\n");
 
 fail_free_new_fdt:
-       efi_free(sys_table, new_fdt_size, *new_fdt_addr);
+       efi_free(sys_table, MAX_FDT_SIZE, *new_fdt_addr);
 
 fail:
Quoted 2 lines
        sys_table->boottime->free_pool(runtime_map);

--
Jeffrey Hugo

Qualcomm Datacenter Technologies as an affiliate of Qualcomm Technologies, Inc.
Qualcomm Technologies, Inc. is a member of the
Code Aurora Forum, a Linux Foundation Collaborative Project.

#6 lore raw

PATCH v2 2/2 efi: arm-stub: Round up FDT allocation to mapping size

In reply to Ard Biesheuvel
Quoted 8 lines · Ard Biesheuvel

The FDT is mapped via a fixmap entry that is at least 2 MB in size and 2 MB aligned on 4 KB page size kernels.

On UEFI systems, the FDT allocation may share this 2 MB block with a reserved region, or another memory region that we should never map, unless we account for this in the size of the allocation (the alignment is already 2 MB)

Quoted 6 lines

So instead of taking guesses at the needed space, simply allocate 2 MB immediately. The allocation will be recorded as a EFI_LOADER_DATA, and the kernel only memblock_reserve()'s the actual size of the FDT, so the unused space will be released to the kernel.

This patch is over a month old, and it's been reviewed and tested. We really need this in 4.11. Please get this merged into 4.11-rc4.

Signature · 3 lines
Qualcomm Innovation Center, Inc.
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project.
#7 lore raw

PATCH v2 2/2 efi: arm-stub: Round up FDT allocation to mapping size

Quoted 15 lines · Timur Tabi
Quoted 13 lines · Ard Biesheuvel

The FDT is mapped via a fixmap entry that is at least 2 MB in size and 2 MB aligned on 4 KB page size kernels.

On UEFI systems, the FDT allocation may share this 2 MB block with a reserved region, or another memory region that we should never map, unless we account for this in the size of the allocation (the alignment is already 2 MB)

So instead of taking guesses at the needed space, simply allocate 2 MB immediately. The allocation will be recorded as a EFI_LOADER_DATA, and the kernel only memblock_reserve()'s the actual size of the FDT, so the unused space will be released to the kernel.

Quoted 5 lines
Quoted 1 line

This patch is over a month old, and it's been reviewed and tested. We really need this in 4.11. Please get this merged into 4.11-rc4.

We never queue non-critical changes right before the merge window opens, so it was queued for v4.12 instead.

If it is so important that this ends up in v4.11, you should have mentioned this at the time, or over the course of the past four weeks.

#8 lore raw

PATCH v2 1/2 efi: arm-stub: Correct FDT and initrd allocation rules for arm64

In reply to Ard Biesheuvel
Quoted 64 lines · Ard Biesheuvel

On arm64, we have made some changes over the past year to the way the kernel itself is allocated and to how it deals with the initrd and FDT. This patch brings the allocation logic in the EFI stub in line with that, which is necessary because the introduction of KASLR has created the possibility for the initrd to be allocated in a place where the kernel may not be able to map it. (This is mostly a theoretical scenario, since it only affects systems where the physical memory footprint exceeds the size of the linear mapping.)

Since we know the kernel itself will be covered by the linear mapping, choose a suitably sized window (i.e., based on the size of the linear region) covering the kernel when allocating memory for the initrd.

The FDT may be anywhere in memory on arm64 now that we map it via the fixmap, so we can lift the address restriction there completely.

---
 arch/arm/include/asm/efi.h              | 14 +++++++++++++-
 arch/arm64/include/asm/efi.h            | 18 +++++++++++++++++-
 drivers/firmware/efi/libstub/arm-stub.c |  7 ++++---
 3 files changed, 34 insertions(+), 5 deletions(-)
arch/arm/include/asm/efi.h +13 -1
index 0b06f5341b45..2de0195dfd1e 100644
@@ -84,6 +84,18 @@ static inline void efifb_setup_from_dmi(struct screen_info *si, const char *opt)
  */
 #define ZIMAGE_OFFSET_LIMIT	SZ_128M
 #define MIN_ZIMAGE_OFFSET	MAX_UNCOMP_KERNEL_SIZE
-#define MAX_FDT_OFFSET		ZIMAGE_OFFSET_LIMIT
+
+/* on ARM, the FDT should be located in the first 128 MB of RAM */
+static inline unsigned long efi_get_max_fdt_addr(unsigned long dram_base)
+{
+	return dram_base + ZIMAGE_OFFSET_LIMIT;
+}
+
+/* on ARM, the initrd should be loaded in a lowmem region */
+static inline unsigned long efi_get_max_initrd_addr(unsigned long dram_base,
+						    unsigned long image_addr)
+{
+	return dram_base + SZ_512M;
+}
 
 #endif /* _ASM_ARM_EFI_H */
arch/arm64/include/asm/efi.h +8 -1
index 0b6b1633017f..342e90d6d204 100644
@@ -46,7 +46,23 @@ int efi_set_mapping_permissions(struct mm_struct *mm, efi_memory_desc_t *md);
  * 2MiB so we know it won't cross a 2MiB boundary.
  */
 #define EFI_FDT_ALIGN	SZ_2M   /* used by allocate_new_fdt_and_exit_boot() */
-#define MAX_FDT_OFFSET	SZ_512M
+
+/* on arm64, the FDT may be located anywhere in system RAM */
+static inline unsigned long efi_get_max_fdt_addr(unsigned long dram_base)
+{
+	return ULONG_MAX;
+}
+
+/*

arch/arm64/include/asm/efi.h int efi_set_mapping_permissions(struct mm_struct *mm, efi_memory_desc_t *md); · line 66

Quoted 10 lines
arch/arm64/include/asm/efi.h +9 -0
+ * On arm64, we have to ensure that the initrd ends up in the linear region,
+ * which is a 1 GB aligned region of size '1UL << (VA_BITS - 1)' that is
+ * guaranteed to cover the kernel Image.
+ */
+static inline unsigned long efi_get_max_initrd_addr(unsigned long dram_base,
+						    unsigned long image_addr)
+{
+	return (image_addr & ~(SZ_1G - 1UL)) + (1UL << (VA_BITS - 1));
+}
 

Please update booting.txt which specifies a window of 32G for ARM64

Quoted 27 lines
arch/arm64/include/asm/efi.h +0 -0
 #define efi_call_early(f, ...)		sys_table_arg->boottime->f(__VA_ARGS__)
 #define __efi_call_early(f, ...)	f(__VA_ARGS__)
drivers/firmware/efi/libstub/arm-stub.c +4 -3
index b4f7d78f9e8b..557281fe375f 100644
@@ -333,8 +333,9 @@ unsigned long efi_entry(void *handle, efi_system_table_t *sys_table,
 	if (!fdt_addr)
 		pr_efi(sys_table, "Generating empty DTB\n");
 
-	status = handle_cmdline_files(sys_table, image, cmdline_ptr,
-				      "initrd=", dram_base + SZ_512M,
+	status = handle_cmdline_files(sys_table, image, cmdline_ptr, "initrd=",
+				      efi_get_max_initrd_addr(dram_base,
+							      *image_addr),
 				      (unsigned long *)&initrd_addr,
 				      (unsigned long *)&initrd_size);
 	if (status != EFI_SUCCESS)
@@ -344,7 +345,7 @@ unsigned long efi_entry(void *handle, efi_system_table_t *sys_table,
 
 	new_fdt_addr = fdt_addr;
 	status = allocate_new_fdt_and_exit_boot(sys_table, handle,
-				&new_fdt_addr, dram_base + MAX_FDT_OFFSET,
+				&new_fdt_addr, efi_get_max_fdt_addr(dram_base),
 				initrd_addr, initrd_size, cmdline_ptr,
 				fdt_addr, fdt_size);
 
Signature · 3 lines
Qualcomm Datacenter Technologies as an affiliate of Qualcomm Technologies, Inc.
Qualcomm Technologies, Inc. is a member of the
Code Aurora Forum, a Linux Foundation Collaborative Project.
#9 lore raw

PATCH v2 1/2 efi: arm-stub: Correct FDT and initrd allocation rules for arm64

Quoted 73 lines · Richard
Quoted 71 lines · Ard Biesheuvel

On arm64, we have made some changes over the past year to the way the kernel itself is allocated and to how it deals with the initrd and FDT. This patch brings the allocation logic in the EFI stub in line with that, which is necessary because the introduction of KASLR has created the possibility for the initrd to be allocated in a place where the kernel may not be able to map it. (This is mostly a theoretical scenario, since it only affects systems where the physical memory footprint exceeds the size of the linear mapping.)

Since we know the kernel itself will be covered by the linear mapping, choose a suitably sized window (i.e., based on the size of the linear region) covering the kernel when allocating memory for the initrd.

The FDT may be anywhere in memory on arm64 now that we map it via the fixmap, so we can lift the address restriction there completely.

---

arch/arm/include/asm/efi.h              | 14 +++++++++++++-
arch/arm64/include/asm/efi.h            | 18 +++++++++++++++++-
drivers/firmware/efi/libstub/arm-stub.c |  7 ++++---
3 files changed, 34 insertions(+), 5 deletions(-)
arch/arm/include/asm/efi.h +13 -1
index 0b06f5341b45..2de0195dfd1e 100644
@@ -84,6 +84,18 @@ static inline void efifb_setup_from_dmi(struct screen_info *si, const char *opt)
 */
 #define ZIMAGE_OFFSET_LIMIT    SZ_128M
 #define MIN_ZIMAGE_OFFSET    MAX_UNCOMP_KERNEL_SIZE
-#define MAX_FDT_OFFSET        ZIMAGE_OFFSET_LIMIT
+
+/* on ARM, the FDT should be located in the first 128 MB of RAM */
+static inline unsigned long efi_get_max_fdt_addr(unsigned long dram_base)
+{
+    return dram_base + ZIMAGE_OFFSET_LIMIT;
+}
+
+/* on ARM, the initrd should be loaded in a lowmem region */
+static inline unsigned long efi_get_max_initrd_addr(unsigned long dram_base,
+                            unsigned long image_addr)
+{
+    return dram_base + SZ_512M;
+}
 
 #endif /* _ASM_ARM_EFI_H */
arch/arm64/include/asm/efi.h +11 -1
index 0b6b1633017f..342e90d6d204 100644
@@ -46,7 +46,23 @@ int efi_set_mapping_permissions(struct mm_struct *mm, efi_memory_desc_t *md);
 * 2MiB so we know it won't cross a 2MiB boundary.
 */
 #define EFI_FDT_ALIGN    SZ_2M   /* used by allocate_new_fdt_and_exit_boot() */
-#define MAX_FDT_OFFSET    SZ_512M
+
+/* on arm64, the FDT may be located anywhere in system RAM */
+static inline unsigned long efi_get_max_fdt_addr(unsigned long dram_base)
+{
+    return ULONG_MAX;
+}
+
+/*
+ * On arm64, we have to ensure that the initrd ends up in the linear region,
+ * which is a 1 GB aligned region of size '1UL << (VA_BITS - 1)' that is
+ * guaranteed to cover the kernel Image.
Quoted 9 lines

arch/arm64/include/asm/efi.h int efi_set_mapping_permissions(struct mm_struct *mm, efi_memory_desc_t *md); · line 66

Quoted 7 lines
arch/arm64/include/asm/efi.h +6 -0
+ */
+static inline unsigned long efi_get_max_initrd_addr(unsigned long dram_base,
+                            unsigned long image_addr)
+{
+    return (image_addr & ~(SZ_1G - 1UL)) + (1UL << (VA_BITS - 1));
+}
 

Please update booting.txt which specifies a window of 32G for ARM64

No. The efi stub is built into the kernel, so there we can be lax about these things. For boot loaders, 32GB remains a reasonable limit because some configurations require it

Quoted 33 lines
Quoted 27 lines
arch/arm64/include/asm/efi.h +0 -0
 #define efi_call_early(f, ...)        sys_table_arg->boottime->f(__VA_ARGS__)
 #define __efi_call_early(f, ...)    f(__VA_ARGS__)
drivers/firmware/efi/libstub/arm-stub.c +4 -3
index b4f7d78f9e8b..557281fe375f 100644
@@ -333,8 +333,9 @@ unsigned long efi_entry(void *handle, efi_system_table_t *sys_table,
   if (!fdt_addr)
       pr_efi(sys_table, "Generating empty DTB\n");
 
-    status = handle_cmdline_files(sys_table, image, cmdline_ptr,
-                      "initrd=", dram_base + SZ_512M,
+    status = handle_cmdline_files(sys_table, image, cmdline_ptr, "initrd=",
+                      efi_get_max_initrd_addr(dram_base,
+                                  *image_addr),
                     (unsigned long *)&initrd_addr,
                     (unsigned long *)&initrd_size);
   if (status != EFI_SUCCESS)
@@ -344,7 +345,7 @@ unsigned long efi_entry(void *handle, efi_system_table_t *sys_table,
 
   new_fdt_addr = fdt_addr;
   status = allocate_new_fdt_and_exit_boot(sys_table, handle,
-                &new_fdt_addr, dram_base + MAX_FDT_OFFSET,
+                &new_fdt_addr, efi_get_max_fdt_addr(dram_base),
               initrd_addr, initrd_size, cmdline_ptr,
               fdt_addr, fdt_size);
 
Signature · 3 lines
Qualcomm Datacenter Technologies as an affiliate of Qualcomm Technologies, Inc.
Qualcomm Technologies, Inc. is a member of the
Code Aurora Forum, a Linux Foundation Collaborative Project.
reviewer
#10 lore raw

PATCH v2 1/2 efi: arm-stub: Correct FDT and initrd allocation rules for arm64

Quoted 33 lines · Ard Biesheuvel
Quoted 23 lines · Richard
Quoted 22 lines · Ard Biesheuvel
arch/arm64/include/asm/efi.h +11 -1
index 0b6b1633017f..342e90d6d204 100644
@@ -46,7 +46,23 @@ int efi_set_mapping_permissions(struct mm_struct *mm, efi_memory_desc_t *md);
 * 2MiB so we know it won't cross a 2MiB boundary.
 */
 #define EFI_FDT_ALIGN    SZ_2M   /* used by allocate_new_fdt_and_exit_boot() */
-#define MAX_FDT_OFFSET    SZ_512M
+
+/* on arm64, the FDT may be located anywhere in system RAM */
+static inline unsigned long efi_get_max_fdt_addr(unsigned long dram_base)
+{
+    return ULONG_MAX;
+}
+
+/*
+ * On arm64, we have to ensure that the initrd ends up in the linear region,
+ * which is a 1 GB aligned region of size '1UL << (VA_BITS - 1)' that is
+ * guaranteed to cover the kernel Image.
Quoted 9 lines

arch/arm64/include/asm/efi.h int efi_set_mapping_permissions(struct mm_struct *mm, efi_memory_desc_t *md); · line 66

Quoted 7 lines
arch/arm64/include/asm/efi.h +6 -0
+ */
+static inline unsigned long efi_get_max_initrd_addr(unsigned long dram_base,
+                            unsigned long image_addr)
+{
+    return (image_addr & ~(SZ_1G - 1UL)) + (1UL << (VA_BITS - 1));
+}
 

Please update booting.txt which specifies a window of 32G for ARM64

Quoted 2 lines

No. The efi stub is built into the kernel, so there we can be lax about these things.

Sure.

Given this is a source of confusion, can we drop a comment here as to how this is deliberate? e.g.

/*
* On arm64, we have to ensure that the initrd ends up in the linear region,
* which is a 1 GB aligned region of size '1UL << (VA_BITS - 1)' that is
* guaranteed to cover the kernel Image.
*
* Since the EFI stub is part of the kernel Image, we can relax than the
* usual requirements in Documentation/arm64/booting.txt, which still
* apply to other bootloaders, and are required for some kernel
* configurations.
*/

Thanks,
Mark.

#11 lore raw

PATCH v2 1/2 efi: arm-stub: Correct FDT and initrd allocation rules for arm64

Quoted 42 lines · Mark Rutland
Quoted 33 lines · Ard Biesheuvel
Quoted 23 lines · Richard
Quoted 22 lines · Ard Biesheuvel
arch/arm64/include/asm/efi.h +11 -1
index 0b6b1633017f..342e90d6d204 100644
@@ -46,7 +46,23 @@ int efi_set_mapping_permissions(struct mm_struct *mm, efi_memory_desc_t *md);
 * 2MiB so we know it won't cross a 2MiB boundary.
 */
 #define EFI_FDT_ALIGN    SZ_2M   /* used by allocate_new_fdt_and_exit_boot() */
-#define MAX_FDT_OFFSET    SZ_512M
+
+/* on arm64, the FDT may be located anywhere in system RAM */
+static inline unsigned long efi_get_max_fdt_addr(unsigned long dram_base)
+{
+    return ULONG_MAX;
+}
+
+/*
+ * On arm64, we have to ensure that the initrd ends up in the linear region,
+ * which is a 1 GB aligned region of size '1UL << (VA_BITS - 1)' that is
+ * guaranteed to cover the kernel Image.
Quoted 9 lines

arch/arm64/include/asm/efi.h int efi_set_mapping_permissions(struct mm_struct *mm, efi_memory_desc_t *md); · line 66

Quoted 7 lines
arch/arm64/include/asm/efi.h +6 -0
+ */
+static inline unsigned long efi_get_max_initrd_addr(unsigned long dram_base,
+                            unsigned long image_addr)
+{
+    return (image_addr & ~(SZ_1G - 1UL)) + (1UL << (VA_BITS - 1));
+}
 

Please update booting.txt which specifies a window of 32G for ARM64

Quoted 2 lines

No. The efi stub is built into the kernel, so there we can be lax about these things.

Sure.

Given this is a source of confusion, can we drop a comment here as to how this is deliberate? e.g.

Quoted 11 lines

/*
* On arm64, we have to ensure that the initrd ends up in the linear region,
* which is a 1 GB aligned region of size '1UL << (VA_BITS - 1)' that is
* guaranteed to cover the kernel Image.
*
* Since the EFI stub is part of the kernel Image, we can relax than the
* usual requirements in Documentation/arm64/booting.txt, which still
* apply to other bootloaders, and are required for some kernel
* configurations.
*/

Absolutely

#12 lore raw

PATCH v2 1/2 efi: arm-stub: Correct FDT and initrd allocation rules for arm64

In reply to Ard Biesheuvel
Quoted 13 lines · Ard Biesheuvel

On arm64, we have made some changes over the past year to the way the kernel itself is allocated and to how it deals with the initrd and FDT. This patch brings the allocation logic in the EFI stub in line with that, which is necessary because the introduction of KASLR has created the possibility for the initrd to be allocated in a place where the kernel may not be able to map it. (This is mostly a theoretical scenario, since it only affects systems where the physical memory footprint exceeds the size of the linear mapping.)

Since we know the kernel itself will be covered by the linear mapping, choose a suitably sized window (i.e., based on the size of the linear region) covering the kernel when allocating memory for the initrd.

Quoted 5 lines

The FDT may be anywhere in memory on arm64 now that we map it via the fixmap, so we can lift the address restriction there completely.

---

Quoted 84 lines
 arch/arm/include/asm/efi.h              | 14 +++++++++++++-
 arch/arm64/include/asm/efi.h            | 18 +++++++++++++++++-
 drivers/firmware/efi/libstub/arm-stub.c |  7 ++++---
 3 files changed, 34 insertions(+), 5 deletions(-)
arch/arm/include/asm/efi.h +13 -1
index 0b06f5341b45..2de0195dfd1e 100644
@@ -84,6 +84,18 @@ static inline void efifb_setup_from_dmi(struct screen_info *si, const char *opt)
  */
 #define ZIMAGE_OFFSET_LIMIT	SZ_128M
 #define MIN_ZIMAGE_OFFSET	MAX_UNCOMP_KERNEL_SIZE
-#define MAX_FDT_OFFSET		ZIMAGE_OFFSET_LIMIT
+
+/* on ARM, the FDT should be located in the first 128 MB of RAM */
+static inline unsigned long efi_get_max_fdt_addr(unsigned long dram_base)
+{
+	return dram_base + ZIMAGE_OFFSET_LIMIT;
+}
+
+/* on ARM, the initrd should be loaded in a lowmem region */
+static inline unsigned long efi_get_max_initrd_addr(unsigned long dram_base,
+						    unsigned long image_addr)
+{
+	return dram_base + SZ_512M;
+}
 
 #endif /* _ASM_ARM_EFI_H */
arch/arm64/include/asm/efi.h +17 -1
index 0b6b1633017f..342e90d6d204 100644
@@ -46,7 +46,23 @@ int efi_set_mapping_permissions(struct mm_struct *mm, efi_memory_desc_t *md);
  * 2MiB so we know it won't cross a 2MiB boundary.
  */
 #define EFI_FDT_ALIGN	SZ_2M   /* used by allocate_new_fdt_and_exit_boot() */
-#define MAX_FDT_OFFSET	SZ_512M
+
+/* on arm64, the FDT may be located anywhere in system RAM */
+static inline unsigned long efi_get_max_fdt_addr(unsigned long dram_base)
+{
+	return ULONG_MAX;
+}
+
+/*
+ * On arm64, we have to ensure that the initrd ends up in the linear region,
+ * which is a 1 GB aligned region of size '1UL << (VA_BITS - 1)' that is
+ * guaranteed to cover the kernel Image.
+ */
+static inline unsigned long efi_get_max_initrd_addr(unsigned long dram_base,
+						    unsigned long image_addr)
+{
+	return (image_addr & ~(SZ_1G - 1UL)) + (1UL << (VA_BITS - 1));
+}
 
 #define efi_call_early(f, ...)		sys_table_arg->boottime->f(__VA_ARGS__)
 #define __efi_call_early(f, ...)	f(__VA_ARGS__)
drivers/firmware/efi/libstub/arm-stub.c +4 -3
index b4f7d78f9e8b..557281fe375f 100644
@@ -333,8 +333,9 @@ unsigned long efi_entry(void *handle, efi_system_table_t *sys_table,
 	if (!fdt_addr)
 		pr_efi(sys_table, "Generating empty DTB\n");
 
-	status = handle_cmdline_files(sys_table, image, cmdline_ptr,
-				      "initrd=", dram_base + SZ_512M,
+	status = handle_cmdline_files(sys_table, image, cmdline_ptr, "initrd=",
+				      efi_get_max_initrd_addr(dram_base,
+							      *image_addr),
 				      (unsigned long *)&initrd_addr,
 				      (unsigned long *)&initrd_size);
 	if (status != EFI_SUCCESS)
@@ -344,7 +345,7 @@ unsigned long efi_entry(void *handle, efi_system_table_t *sys_table,
 
 	new_fdt_addr = fdt_addr;
 	status = allocate_new_fdt_and_exit_boot(sys_table, handle,
-				&new_fdt_addr, dram_base + MAX_FDT_OFFSET,
+				&new_fdt_addr, efi_get_max_fdt_addr(dram_base),
 				initrd_addr, initrd_size, cmdline_ptr,
 				fdt_addr, fdt_size);
 
Signature · 5 lines
Jeffrey Hugo
Qualcomm Datacenter Technologies as an affiliate of Qualcomm 
Technologies, Inc.
Qualcomm Technologies, Inc. is a member of the
Code Aurora Forum, a Linux Foundation Collaborative Project.