[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v3 09/15] efi: mm: Pass up errors from `add_memory_regions ()`
From: |
Daniel Axtens |
Subject: |
[PATCH v3 09/15] efi: mm: Pass up errors from `add_memory_regions ()` |
Date: |
Thu, 21 Apr 2022 15:24:21 +1000 |
From: Patrick Steinhardt <ps@pks.im>
The function `add_memory_regions ()` is currently only called on system
initialization to allocate a fixed amount of pages. As such, it didn't
need to return any errors: in case it failed, we cannot proceed anyway.
This will change with the upcoming support for requesting more memory
from the firmware at runtime, where it doesn't make sense anymore to
fail hard.
Refactor the function to return an error to prepare for this. Note that
this does not change the behaviour when initializing the memory system
because `grub_efi_mm_init ()` knows to call `grub_fatal ()` in case
`grub_efi_mm_add_regions ()` returns an error.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
[dja: clarify error messages]
Signed-off-by: Daniel Axtens <dja@axtens.net>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
---
v2: Thank you Glenn and Daniel K for the feedback.
This and all EFI changes were not tested on real hardware.
---
grub-core/kern/efi/mm.c | 22 +++++++++++++++-------
1 file changed, 15 insertions(+), 7 deletions(-)
diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c
index c8d3968f7f68..4e594659a1ff 100644
--- a/grub-core/kern/efi/mm.c
+++ b/grub-core/kern/efi/mm.c
@@ -478,7 +478,7 @@ filter_memory_map (grub_efi_memory_descriptor_t *memory_map,
}
/* Add memory regions. */
-static void
+static grub_err_t
add_memory_regions (grub_efi_memory_descriptor_t *memory_map,
grub_efi_uintn_t desc_size,
grub_efi_memory_descriptor_t *memory_map_end,
@@ -506,9 +506,9 @@ add_memory_regions (grub_efi_memory_descriptor_t
*memory_map,
GRUB_EFI_ALLOCATE_ADDRESS,
GRUB_EFI_LOADER_CODE);
if (! addr)
- grub_fatal ("cannot allocate conventional memory %p with %u pages",
- (void *) ((grub_addr_t) start),
- (unsigned) pages);
+ return grub_error (GRUB_ERR_OUT_OF_MEMORY,
+ "Memory starting at %p (%u pages) marked as free,
but EFI would not allocate",
+ (void *) ((grub_addr_t) start), (unsigned) pages);
grub_mm_init_region (addr, PAGES_TO_BYTES (pages));
@@ -518,7 +518,11 @@ add_memory_regions (grub_efi_memory_descriptor_t
*memory_map,
}
if (required_pages > 0)
- grub_fatal ("too little memory");
+ return grub_error (GRUB_ERR_OUT_OF_MEMORY,
+ "could not allocate all requested memory: %"
PRIuGRUB_UINT64_T " pages still required after iterating EFI memory map",
+ required_pages);
+
+ return GRUB_ERR_NONE;
}
void
@@ -565,6 +569,7 @@ grub_efi_mm_add_regions (grub_efi_uint64_t required_bytes)
grub_efi_memory_descriptor_t *filtered_memory_map_end;
grub_efi_uintn_t map_size;
grub_efi_uintn_t desc_size;
+ grub_err_t err;
int mm_status;
/* Prepare a memory region to store two memory maps. */
@@ -609,8 +614,11 @@ grub_efi_mm_add_regions (grub_efi_uint64_t required_bytes)
sort_memory_map (filtered_memory_map, desc_size, filtered_memory_map_end);
/* Allocate memory regions for GRUB's memory management. */
- add_memory_regions (filtered_memory_map, desc_size,
- filtered_memory_map_end, BYTES_TO_PAGES (required_bytes));
+ err = add_memory_regions (filtered_memory_map, desc_size,
+ filtered_memory_map_end,
+ BYTES_TO_PAGES (required_bytes));
+ if (err != GRUB_ERR_NONE)
+ return err;
#if 0
/* For debug. */
--
2.32.0
- [PATCH v3 01/15] grub-shell: only pass SeaBIOS fw_opt in for x86 BIOS platforms, (continued)
- [PATCH v3 01/15] grub-shell: only pass SeaBIOS fw_opt in for x86 BIOS platforms, Daniel Axtens, 2022/04/21
- [PATCH v3 02/15] mm: assert that we preserve header vs region alignment, Daniel Axtens, 2022/04/21
- [PATCH v3 03/15] mm: when adding a region, merge with region after as well as before, Daniel Axtens, 2022/04/21
- [PATCH v3 04/15] mm: debug support for region operations, Daniel Axtens, 2022/04/21
- [PATCH v3 05/15] mm: Drop unused unloading of modules on OOM, Daniel Axtens, 2022/04/21
- [PATCH v3 06/15] mm: Allow dynamically requesting additional memory regions, Daniel Axtens, 2022/04/21
- [PATCH v3 07/15] efi: mm: Always request a fixed number of pages on init, Daniel Axtens, 2022/04/21
- [PATCH v3 08/15] efi: mm: Extract function to add memory regions, Daniel Axtens, 2022/04/21
- [PATCH v3 09/15] efi: mm: Pass up errors from `add_memory_regions ()`,
Daniel Axtens <=
- [PATCH v3 10/15] efi: mm: Implement runtime addition of pages, Daniel Axtens, 2022/04/21
- [PATCH v3 11/15] ieee1275: request memory with ibm, client-architecture-support, Daniel Axtens, 2022/04/21
- [PATCH v3 12/15] ieee1275: drop len -= 1 quirk in heap_init, Daniel Axtens, 2022/04/21
- [PATCH v3 13/15] ieee1275: support runtime memory claiming, Daniel Axtens, 2022/04/21
- [PATCH v3 15/15] ibmvtpm: Add support for trusted boot using a vTPM 2.0, Daniel Axtens, 2022/04/21
- [PATCH v3 14/15] [RFC] Add memtool module with memory allocation stress-test, Daniel Axtens, 2022/04/21