Skip to contentSkip to conversation

Reset grub_mm_add_region_fn after exiting EFI services

PATCH In review Ruihan Li 2 participants 5 messages Dec 16, 2024 – Dec 23, 2024

lore
Share & tools
lore

Ruihan Li

author
#0 lore raw

PATCH 1/1 Reset grub_mm_add_region_fn after exiting EFI services

Currently, EFI services can be used after they are exited because we allocate memory after exiting EFI services.

An example call stack is:

grub_multiboot_boot

    grub_multiboot2_make_mbi
      grub_efi_finish_boot_services
        b->exit_boot_services
    normal_boot
      grub_relocator32_boot
        grub_relocator_alloc_chunk_align_safe
          grub_relocator_alloc_chunk_align
            grub_malloc
              grub_memalign
                grub_mm_add_region_fn
                [= grub_efi_mm_add_regions]
                  grub_efi_allocate_any_pages
                    grub_efi_allocate_pages_real
                      b->allocate_pages

This can lead to confusing errors. After exiting EFI services, b->allocate_pages may point to the NULL address, resulting in something like:

!!!! X64 Exception Type - 01(#DB - Debug) CPU Apic ID - 00000000 !!!!

RIP  - 000000000000201F, CS  - 0000000000000038, RFLAGS - 0000000000200002
RAX  - 000000007F9EE010, RCX - 0000000000000001, RDX - 0000000000000002
RBX  - 0000000000000006, RSP - 00000000001CFBEC, RBP - 0000000000000000
RSI  - 0000000000000000, RDI - 00000000FFFFFFFF
R8   - 0000000000000006, R9  - 000000007FEDFFB8, R10 - 0000000000000000
R11  - 0000000000000475, R12 - 0000000000000001, R13 - 0000000000000002
R14  - 00000000FFFFFFFF, R15 - 000000007E432C08
DS   - 0000000000000030, ES  - 0000000000000030, FS  - 0000000000000030
GS   - 0000000000000030, SS  - 0000000000000030
CR0  - 0000000080010033, CR2 - 0000000000000000, CR3 - 000000007FC01000
CR4  - 0000000000000668, CR8 - 0000000000000000
DR0  - 0000000000000000, DR1 - 0000000000000000, DR2 - 0000000000000000
DR3  - 0000000000000000, DR6 - 00000000FFFF0FF0, DR7 - 0000000000000400

GDTR - 000000007F9DE000 0000000000000047, LDTR - 0000000000000000
IDTR - 000000007F470018 0000000000000FFF, TR - 0000000000000000
FXSAVE_STATE - 00000000001CF840

Ideally, we would like to avoid all memory allocations after exiting EFI services altogether, but that requires significant code changes. This patch suggests a simple workaround that resets grub_mm_add_region_fn after exiting EFI services, so:

- Memory allocations after exiting EFI services have a better chance of success because grub_memalign will try to reclaim the disk cache if it sees a NULL grub_mm_add_region_fn.

- At worst, it will fail to allocate memory after exiting EFI services, but it will explicitly tell users that it's out of memory, which is still much better than the current situation where it fails in a fairly random way and triggers a CPU exception.

---
 grub-core/kern/efi/mm.c | 6 ++++++
 1 file changed, 6 insertions(+)
grub-core/kern/efi/mm.c +6 -0
index bc97ecd47..15c35e0ce 100644
@@ -297,6 +297,12 @@ grub_efi_finish_boot_services (grub_efi_uintn_t *outbuf_size, void *outbuf,
   if (efi_desc_version)
     *efi_desc_version = finish_desc_version;
 
+  /*
+   * We cannot request new memory regions from the EFI services anymore.
+   * FIXME: Can we completely avoid memory allocations after this?
+   */
+  grub_mm_add_region_fn = NULL;
+
 #if defined (__i386__) || defined (__x86_64__)
   if (is_apple)
     stop_broadcom ();
Signature · 7 lines
2.47.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Reviewed-by Daniel Kiper on v1 1/1
#1 lore raw
Quoted 52 lines · Ruihan Li

Currently, EFI services can be used after they are exited because we allocate memory after exiting EFI services.

An example call stack is:

grub_multiboot_boot

    grub_multiboot2_make_mbi
      grub_efi_finish_boot_services
        b->exit_boot_services
    normal_boot
      grub_relocator32_boot
        grub_relocator_alloc_chunk_align_safe
          grub_relocator_alloc_chunk_align
            grub_malloc
              grub_memalign
                grub_mm_add_region_fn
                [= grub_efi_mm_add_regions]
                  grub_efi_allocate_any_pages
                    grub_efi_allocate_pages_real
                      b->allocate_pages

This can lead to confusing errors. After exiting EFI services, b->allocate_pages may point to the NULL address, resulting in something like:

!!!! X64 Exception Type - 01(#DB - Debug) CPU Apic ID - 00000000 !!!!

RIP  - 000000000000201F, CS  - 0000000000000038, RFLAGS - 0000000000200002
RAX  - 000000007F9EE010, RCX - 0000000000000001, RDX - 0000000000000002
RBX  - 0000000000000006, RSP - 00000000001CFBEC, RBP - 0000000000000000
RSI  - 0000000000000000, RDI - 00000000FFFFFFFF
R8   - 0000000000000006, R9  - 000000007FEDFFB8, R10 - 0000000000000000
R11  - 0000000000000475, R12 - 0000000000000001, R13 - 0000000000000002
R14  - 00000000FFFFFFFF, R15 - 000000007E432C08
DS   - 0000000000000030, ES  - 0000000000000030, FS  - 0000000000000030
GS   - 0000000000000030, SS  - 0000000000000030
CR0  - 0000000080010033, CR2 - 0000000000000000, CR3 - 000000007FC01000
CR4  - 0000000000000668, CR8 - 0000000000000000
DR0  - 0000000000000000, DR1 - 0000000000000000, DR2 - 0000000000000000
DR3  - 0000000000000000, DR6 - 00000000FFFF0FF0, DR7 - 0000000000000400

GDTR - 000000007F9DE000 0000000000000047, LDTR - 0000000000000000
IDTR - 000000007F470018 0000000000000FFF, TR - 0000000000000000
FXSAVE_STATE - 00000000001CF840

Ideally, we would like to avoid all memory allocations after exiting EFI services altogether, but that requires significant code changes. This patch suggests a simple workaround that resets grub_mm_add_region_fn after exiting EFI services, so:

- Memory allocations after exiting EFI services have a better chance of success because grub_memalign will try to reclaim the disk cache if it sees a NULL grub_mm_add_region_fn.

Quoted 6 lines

- At worst, it will fail to allocate memory after exiting EFI services, but it will explicitly tell users that it's out of memory, which is still much better than the current situation where it fails in a fairly random way and triggers a CPU exception.

However, should not we go further and extend the heap with additional memory before EBS? 1 MiB?

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

author
#2 lore raw
Quoted 1 line · Daniel Kiper

Thanks for your review!

Quoted 2 lines

However, should not we go further and extend the heap with additional memory before EBS? 1 MiB?

Yeah, I think it's a good idea. I'm not sure how much memory we should reserve, so I'll use your 1MiB suggestion below.

I can send a v2 patch with the following changes. Since this involves a larger amount of code changes (compared to my original patch), maybe you could take a look to see if I implemented your suggestions correctly first (so I can keep the Reviewed-by tag)? Thanks!

grub-core/kern/efi/mm.c +27 -3
index bc97ecd47..6ffddf3c9 100644
@@ -41,6 +41,9 @@
 /* The default heap size for GRUB itself in bytes.  */
 #define DEFAULT_HEAP_SIZE	0x2000000
 
+/* The heap size reserved when exiting EFI services in bytes. */
+#define EBS_HEAP_SIZE		0x100000
+
 static void *finish_mmap_buf = 0;
 static grub_efi_uintn_t finish_mmap_size = 0;
 static grub_efi_uintn_t finish_key = 0;
@@ -231,6 +234,9 @@ stop_broadcom (void)
 
 #endif
 
+static grub_err_t
+grub_efi_mm_add_regions (grub_size_t required_bytes, unsigned int flags);
+
 grub_err_t
 grub_efi_finish_boot_services (grub_efi_uintn_t *outbuf_size, void *outbuf,
 			       grub_efi_uintn_t *map_key,
@@ -248,24 +254,41 @@ grub_efi_finish_boot_services (grub_efi_uintn_t *outbuf_size, void *outbuf,
 			   apple, sizeof (apple)) == 0);
 #endif
 
+  /*
+   * We can no longer request new memory from EFI Services.
+   * So we reserve some memory in advance.
+   */
+  grub_efi_mm_add_regions (EBS_HEAP_SIZE, GRUB_MM_ADD_REGION_NONE);
+  grub_mm_add_region_fn = NULL;
+
   while (1)
     {
       if (grub_efi_get_memory_map (&finish_mmap_size, finish_mmap_buf, &finish_key,
 				   &finish_desc_size, &finish_desc_version) < 0)
-	return grub_error (GRUB_ERR_IO, "couldn't retrieve memory map");
+	{
+	  grub_mm_add_region_fn = grub_efi_mm_add_regions;
+	  return grub_error (GRUB_ERR_IO, "couldn't retrieve memory map");
+	}
 
       if (outbuf && *outbuf_size < finish_mmap_size)
-	return grub_error (GRUB_ERR_IO, "memory map buffer is too small");
+	{
+	  grub_mm_add_region_fn = grub_efi_mm_add_regions;
+	  return grub_error (GRUB_ERR_IO, "memory map buffer is too small");
+	}
 
       finish_mmap_buf = grub_malloc (finish_mmap_size);
       if (!finish_mmap_buf)
-	return grub_errno;
+	{
+	  grub_mm_add_region_fn = grub_efi_mm_add_regions;
+	  return grub_errno;
+	}
 
       if (grub_efi_get_memory_map (&finish_mmap_size, finish_mmap_buf, &finish_key,
 				   &finish_desc_size, &finish_desc_version) <= 0)
 	{
 	  grub_free (finish_mmap_buf);
 	  finish_mmap_buf = NULL;
+	  grub_mm_add_region_fn = grub_efi_mm_add_regions;
 	  return grub_error (GRUB_ERR_IO, "couldn't retrieve memory map");
 	}
 
@@ -278,6 +301,7 @@ grub_efi_finish_boot_services (grub_efi_uintn_t *outbuf_size, void *outbuf,
 	{
 	  grub_free (finish_mmap_buf);
 	  finish_mmap_buf = NULL;
+	  grub_mm_add_region_fn = grub_efi_mm_add_regions;
 	  return grub_error (GRUB_ERR_IO, "couldn't terminate EFI services");
 	}
 

Thanks,
Ruihan Li

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#3 lore raw
Quoted 10 lines · Ruihan Li
Quoted 1 line · Daniel Kiper

Thanks for your review!

Quoted 2 lines

However, should not we go further and extend the heap with additional memory before EBS? 1 MiB?

Yeah, I think it's a good idea. I'm not sure how much memory we should reserve, so I'll use your 1MiB suggestion below.

I think you could get an idea how much memory is allocated after EBS by commenting out EBS call and printing allocation request sizes post EBS call commented out.

Quoted 4 lines

I can send a v2 patch with the following changes. Since this involves a larger amount of code changes (compared to my original patch), maybe you could take a look to see if I implemented your suggestions correctly first (so I can keep the Reviewed-by tag)? Thanks!

I think the proposed patch should be a separate thing.

grub-core/kern/efi/mm.c line 45

Quoted 10 lines
grub-core/kern/efi/mm.c +2 -0
index bc97ecd47..6ffddf3c9 100644
@@ -41,6 +41,9 @@
 /* The default heap size for GRUB itself in bytes.  */
 #define DEFAULT_HEAP_SIZE	0x2000000
 
+/* The heap size reserved when exiting EFI services in bytes. */
+#define EBS_HEAP_SIZE		0x100000

s/EBS_HEAP_SIZE/POST_EBS_HEAP_SIZE/

Quoted 14 lines
grub-core/kern/efi/mm.c +3 -0
 static void *finish_mmap_buf = 0;
 static grub_efi_uintn_t finish_mmap_size = 0;
 static grub_efi_uintn_t finish_key = 0;
@@ -231,6 +234,9 @@ stop_broadcom (void)
 
 #endif
 
+static grub_err_t
+grub_efi_mm_add_regions (grub_size_t required_bytes, unsigned int flags);
+
 grub_err_t
 grub_efi_finish_boot_services (grub_efi_uintn_t *outbuf_size, void *outbuf,
 			       grub_efi_uintn_t *map_key,

grub-core/kern/efi/mm.c grub_efi_finish_boot_services (grub_efi_uintn_t *outbuf_size, void *outbuf, · line 258

Quoted 5 lines
grub-core/kern/efi/mm.c +2 -0
@@ -248,24 +254,41 @@ grub_efi_finish_boot_services (grub_efi_uintn_t *outbuf_size, void *outbuf,
 			   apple, sizeof (apple)) == 0);
 #endif
 
+  /*
+   * We can no longer request new memory from EFI Services.

s/EFI Services/EFI Boot Services/

Quoted 4 lines
grub-core/kern/efi/mm.c +4 -0
+   * So we reserve some memory in advance.
+   */
+  grub_efi_mm_add_regions (EBS_HEAP_SIZE, GRUB_MM_ADD_REGION_NONE);
+  grub_mm_add_region_fn = NULL;

grub-core/kern/efi/mm.c grub_efi_finish_boot_services (grub_efi_uintn_t *outbuf_size, void *outbuf, · line 271

Quoted 10 lines
grub-core/kern/efi/mm.c +5 -1
+
   while (1)
     {
       if (grub_efi_get_memory_map (&finish_mmap_size, finish_mmap_buf, &finish_key,
 				   &finish_desc_size, &finish_desc_version) < 0)
-	return grub_error (GRUB_ERR_IO, "couldn't retrieve memory map");
+	{
+	  grub_mm_add_region_fn = grub_efi_mm_add_regions;
+	  return grub_error (GRUB_ERR_IO, "couldn't retrieve memory map");
+	}

I am not sure what is a goal of these changes. Could you elaborate?

grub-core/kern/efi/mm.c grub_efi_finish_boot_services (grub_efi_uintn_t *outbuf_size, void *outbuf, · line 277

Quoted 6 lines
grub-core/kern/efi/mm.c +4 -1
       if (outbuf && *outbuf_size < finish_mmap_size)
-	return grub_error (GRUB_ERR_IO, "memory map buffer is too small");
+	{
+	  grub_mm_add_region_fn = grub_efi_mm_add_regions;
+	  return grub_error (GRUB_ERR_IO, "memory map buffer is too small");
+	}

Ditto and below...

Quoted 18 lines
grub-core/kern/efi/mm.c +5 -1
       finish_mmap_buf = grub_malloc (finish_mmap_size);
       if (!finish_mmap_buf)
-	return grub_errno;
+	{
+	  grub_mm_add_region_fn = grub_efi_mm_add_regions;
+	  return grub_errno;
+	}
 
       if (grub_efi_get_memory_map (&finish_mmap_size, finish_mmap_buf, &finish_key,
 				   &finish_desc_size, &finish_desc_version) <= 0)
 	{
 	  grub_free (finish_mmap_buf);
 	  finish_mmap_buf = NULL;
+	  grub_mm_add_region_fn = grub_efi_mm_add_regions;
 	  return grub_error (GRUB_ERR_IO, "couldn't retrieve memory map");
 	}
 

grub-core/kern/efi/mm.c grub_efi_finish_boot_services (grub_efi_uintn_t *outbuf_size, void *outbuf, · line 306

Quoted 6 lines
grub-core/kern/efi/mm.c +1 -0
@@ -278,6 +301,7 @@ grub_efi_finish_boot_services (grub_efi_uintn_t *outbuf_size, void *outbuf,
 	{
 	  grub_free (finish_mmap_buf);
 	  finish_mmap_buf = NULL;
+	  grub_mm_add_region_fn = grub_efi_mm_add_regions;
 	  return grub_error (GRUB_ERR_IO, "couldn't terminate EFI services");
 	}

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

author
#4 lore raw
Quoted 9 lines · Daniel Kiper
Quoted 8 lines · Ruihan Li
Quoted 1 line · Daniel Kiper

Thanks for your review!

Quoted 2 lines

However, should not we go further and extend the heap with additional memory before EBS? 1 MiB?

Quoted 6 lines
Quoted 2 lines

Yeah, I think it's a good idea. I'm not sure how much memory we should reserve, so I'll use your 1MiB suggestion below.

I think you could get an idea how much memory is allocated after EBS by commenting out EBS call and printing allocation request sizes post EBS call commented out.

My local test shows that after EBS only 0x13A9 bytes of memory are allocated. But I think this value can vary from environment to environment.

Quoted 7 lines
Quoted 5 lines

I can send a v2 patch with the following changes. Since this involves a larger amount of code changes (compared to my original patch), maybe you could take a look to see if I implemented your suggestions correctly first (so I can keep the Reviewed-by tag)? Thanks!

I think the proposed patch should be a separate thing.

Yes, I'm totally fine with considering it a separate thing, but what exactly do you mean by "separate" here?

Do you mean that it's not a "v2" patch, or do you mean that you want to do the review separately (sorry if you mean the latter, since the review process already starts here anyway...)?

Quoted 31 lines

grub-core/kern/efi/mm.c line 45

Quoted 11 lines
grub-core/kern/efi/mm.c +2 -0
index bc97ecd47..6ffddf3c9 100644
@@ -41,6 +41,9 @@
 /* The default heap size for GRUB itself in bytes.  */
 #define DEFAULT_HEAP_SIZE	0x2000000
 
+/* The heap size reserved when exiting EFI services in bytes. */
+#define EBS_HEAP_SIZE		0x100000

s/EBS_HEAP_SIZE/POST_EBS_HEAP_SIZE/

Quoted 17 lines
grub-core/kern/efi/mm.c +3 -0
 static void *finish_mmap_buf = 0;
 static grub_efi_uintn_t finish_mmap_size = 0;
 static grub_efi_uintn_t finish_key = 0;
@@ -231,6 +234,9 @@ stop_broadcom (void)
 
 #endif
 
+static grub_err_t
+grub_efi_mm_add_regions (grub_size_t required_bytes, unsigned int flags);
+
 grub_err_t
 grub_efi_finish_boot_services (grub_efi_uintn_t *outbuf_size, void *outbuf,
 			       grub_efi_uintn_t *map_key,
@@ -248,24 +254,41 @@ grub_efi_finish_boot_services (grub_efi_uintn_t *outbuf_size, void *outbuf,
 			   apple, sizeof (apple)) == 0);
 #endif
 
Quoted 5 lines

grub-core/kern/efi/mm.c grub_efi_finish_boot_services (grub_efi_uintn_t *outbuf_size, void *outbuf, · line 258

Quoted 2 lines
grub-core/kern/efi/mm.c +2 -0
+  /*
+   * We can no longer request new memory from EFI Services.

s/EFI Services/EFI Boot Services/

Thanks for revising the naming and the comments!

Quoted 8 lines
Quoted 8 lines
grub-core/kern/efi/mm.c +5 -0
+   * So we reserve some memory in advance.
+   */
+  grub_efi_mm_add_regions (EBS_HEAP_SIZE, GRUB_MM_ADD_REGION_NONE);
+  grub_mm_add_region_fn = NULL;
+
   while (1)
     {
       if (grub_efi_get_memory_map (&finish_mmap_size, finish_mmap_buf, &finish_key,
Quoted 8 lines

grub-core/kern/efi/mm.c grub_efi_finish_boot_services (grub_efi_uintn_t *outbuf_size, void *outbuf, · line 271

Quoted 6 lines
grub-core/kern/efi/mm.c +4 -1
 				   &finish_desc_size, &finish_desc_version) < 0)
-	return grub_error (GRUB_ERR_IO, "couldn't retrieve memory map");
+	{
+	  grub_mm_add_region_fn = grub_efi_mm_add_regions;
+	  return grub_error (GRUB_ERR_IO, "couldn't retrieve memory map");
+	}

I am not sure what is a goal of these changes. Could you elaborate?

From the caller's point of view, if we haven't finished the EFI boot services successfully, I think we should undo the change to grub_mm_add_region_fn?

After all, the caller sees an error code, so it expects this method to fail, so this method should do nothing (instead of doing something "partially", i.e. just clearing the grub_mm_add_region_fn).

I'm also fine with removing these changes if the above argument doesn't make sense. If so, the caller must be aware that grub_mm_add_region_fn can be cleared even if the method fails.

Quoted 27 lines

grub-core/kern/efi/mm.c grub_efi_finish_boot_services (grub_efi_uintn_t *outbuf_size, void *outbuf, · line 277

Quoted 7 lines
grub-core/kern/efi/mm.c +4 -1
       if (outbuf && *outbuf_size < finish_mmap_size)
-	return grub_error (GRUB_ERR_IO, "memory map buffer is too small");
+	{
+	  grub_mm_add_region_fn = grub_efi_mm_add_regions;
+	  return grub_error (GRUB_ERR_IO, "memory map buffer is too small");
+	}

Ditto and below...

Quoted 17 lines
grub-core/kern/efi/mm.c +5 -1
       finish_mmap_buf = grub_malloc (finish_mmap_size);
       if (!finish_mmap_buf)
-	return grub_errno;
+	{
+	  grub_mm_add_region_fn = grub_efi_mm_add_regions;
+	  return grub_errno;
+	}
 
       if (grub_efi_get_memory_map (&finish_mmap_size, finish_mmap_buf, &finish_key,
 				   &finish_desc_size, &finish_desc_version) <= 0)
 	{
 	  grub_free (finish_mmap_buf);
 	  finish_mmap_buf = NULL;
+	  grub_mm_add_region_fn = grub_efi_mm_add_regions;
 	  return grub_error (GRUB_ERR_IO, "couldn't retrieve memory map");
 	}
 
Quoted 9 lines

grub-core/kern/efi/mm.c grub_efi_finish_boot_services (grub_efi_uintn_t *outbuf_size, void *outbuf, · line 306

Quoted 7 lines
grub-core/kern/efi/mm.c +1 -0
@@ -278,6 +301,7 @@ grub_efi_finish_boot_services (grub_efi_uintn_t *outbuf_size, void *outbuf,
 	{
 	  grub_free (finish_mmap_buf);
 	  finish_mmap_buf = NULL;
+	  grub_mm_add_region_fn = grub_efi_mm_add_regions;
 	  return grub_error (GRUB_ERR_IO, "couldn't terminate EFI services");
 	}

Daniel

Thanks,
Ruihan Li

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel