Hi, this is the third version of my patch set to implement runtime allocation of additional memory regions.
Changes compared to v2: - A new preparatory patch was added to remove unused code which unloaded modules on OOM. - Patch 2/4 has been split up into two patches: one to drop the logic where we request a quarter of available memory and then put bounds to it, and one to split apart request of additional regions and initialization of the EFI MM system. - Flags are now `unsigned int` instead of `unsigned`. - `add_memory_regions ()` now gets all flags instead of only a single flag `consecutive`. - Flags are now defines and not an enum anymore. - The callback function is now called `grub_mm_add_region_func_t` instead of `grub_mm_region_func_t`. Flags and its variable have been renamed accordingly. Patrick Patrick Steinhardt (6): mm: Drop unused unloading of modules on OOM mm: Allow dynamically requesting additional memory regions efi: mm: Always request a fixed number of pages on init efi: mm: Extract function to add memory regions efi: mm: Pass up errors from `add_memory_regions ()` efi: mm: Implement runtime addition of pages grub-core/kern/dl.c | 20 ---------- grub-core/kern/efi/mm.c | 83 +++++++++++++++++++---------------------- grub-core/kern/mm.c | 12 +++--- include/grub/dl.h | 1 - include/grub/mm.h | 16 ++++++++ 5 files changed, 61 insertions(+), 71 deletions(-) Range-diff against v2: 1: cf709a8a1 < -: --------- mm: Allow dynamically requesting additional memory regions -: --------- > 1: 7f2738a64 mm: Drop unused unloading of modules on OOM -: --------- > 2: 3f0ec2a76 mm: Allow dynamically requesting additional memory regions 2: 2a9ccee9e ! 3: 5da49ddf1 efi: mm: Extract function to add memory regions @@ Metadata Author: Patrick Steinhardt <p...@pks.im> ## Commit message ## - efi: mm: Extract function to add memory regions + efi: mm: Always request a fixed number of pages on init - In preparation of support for runtime-allocating additional memory - region, this patch extracts the function to retrieve the EFI memory map - and add a subset of it to GRUB's own memory regions. + When initializing the EFI memory subsytem, we will by default request a + quarter of the available memory, bounded by a minimum/maximum value. + Given that we're about to extend the EFI memory system to dynamically + request additional pages from the firmware as required, this scaling of + requested memory based on available memory will not make a lot of sense + anymore. - Note that this commit also changes how many bytes we request by default. - Previously, we would've tried to allocate a quarter of available system - memory, bounded by a minimum/maximum value. As we're about to implement - runtime allocation of memory, we now instead always request the minimum - amount of bytes and let the memory allocator call out to our callback. + Remove this logic as a preparatory patch such that we'll instead defer + to the runtime memory allocator. Note that ideally, we'd want to change + this after dynamic requesting of pages has been implemented for the EFI + platform. But because we'll need to split up initialization of the + memory subsystem and the request of pages from the firmware, we'd have + to duplicate quite some logic at first only to remove it afterwards + again. This seems quite pointless, so we instead have patches slightly + out of order. Signed-off-by: Patrick Steinhardt <p...@pks.im> @@ grub-core/kern/efi/mm.c: filter_memory_map (grub_efi_memory_descriptor_t *memory /* Add memory regions. */ static void add_memory_regions (grub_efi_memory_descriptor_t *memory_map, -@@ grub-core/kern/efi/mm.c: add_memory_regions (grub_efi_memory_descriptor_t *memory_map, - - addr = grub_efi_allocate_pages_real (start, pages, - GRUB_EFI_ALLOCATE_ADDRESS, -- GRUB_EFI_LOADER_CODE); -+ GRUB_EFI_LOADER_CODE); - if (! addr) - grub_fatal ("cannot allocate conventional memory %p with %u pages", - (void *) ((grub_addr_t) start), -@@ grub-core/kern/efi/mm.c: print_memory_map (grub_efi_memory_descriptor_t *memory_map, - } - #endif - --void --grub_efi_mm_init (void) -+static grub_err_t -+grub_efi_mm_add_regions (grub_efi_uint64_t required_bytes) - { - grub_efi_memory_descriptor_t *memory_map; - grub_efi_memory_descriptor_t *memory_map_end; @@ grub-core/kern/efi/mm.c: grub_efi_mm_init (void) grub_efi_memory_descriptor_t *filtered_memory_map_end; grub_efi_uintn_t map_size; @@ grub-core/kern/efi/mm.c: grub_efi_mm_init (void) int mm_status; /* Prepare a memory region to store two memory maps. */ - memory_map = grub_efi_allocate_any_pages (2 * BYTES_TO_PAGES (MEMORY_MAP_SIZE)); - if (! memory_map) -- grub_fatal ("cannot allocate memory"); -+ return grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate memory"); - - /* Obtain descriptors for available memory. */ - map_size = MEMORY_MAP_SIZE; -@@ grub-core/kern/efi/mm.c: grub_efi_mm_init (void) - - memory_map = grub_efi_allocate_any_pages (2 * BYTES_TO_PAGES (map_size)); - if (! memory_map) -- grub_fatal ("cannot allocate memory"); -+ return grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate memory"); - - mm_status = grub_efi_get_memory_map (&map_size, memory_map, 0, - &desc_size, 0); - } - - if (mm_status < 0) -- grub_fatal ("cannot get memory map"); -+ return grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot get memory map"); - - memory_map_end = NEXT_MEMORY_DESCRIPTOR (memory_map, map_size); - @@ grub-core/kern/efi/mm.c: grub_efi_mm_init (void) filtered_memory_map_end = filter_memory_map (memory_map, filtered_memory_map, desc_size, memory_map_end); @@ grub-core/kern/efi/mm.c: grub_efi_mm_init (void) /* Allocate memory regions for GRUB's memory management. */ add_memory_regions (filtered_memory_map, desc_size, - filtered_memory_map_end, required_pages); -+ filtered_memory_map_end, -+ BYTES_TO_PAGES (required_bytes)); ++ filtered_memory_map_end, BYTES_TO_PAGES (DEFAULT_HEAP_SIZE)); #if 0 /* For debug. */ -@@ grub-core/kern/efi/mm.c: grub_efi_mm_init (void) - /* Release the memory maps. */ - grub_efi_free_pages ((grub_addr_t) memory_map, - 2 * BYTES_TO_PAGES (MEMORY_MAP_SIZE)); -+ -+ return GRUB_ERR_NONE; -+} -+ -+void -+grub_efi_mm_init (void) -+{ -+ if (grub_efi_mm_add_regions (DEFAULT_HEAP_SIZE) != GRUB_ERR_NONE) -+ grub_fatal (grub_errmsg); - } - - #if defined (__aarch64__) || defined (__arm__) || defined (__riscv) -: --------- > 4: 7b2f5fcb5 efi: mm: Extract function to add memory regions 3: ff7e5be9f ! 5: e90bbcf04 efi: mm: Pass up errors from `add_memory_regions ()` @@ Commit message 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. + 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 <p...@pks.im> @@ grub-core/kern/efi/mm.c: grub_efi_mm_add_regions (grub_efi_uint64_t required_byt /* 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)); +- 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)); 4: a27f5b047 ! 6: 0a3ef3d2a efi: mm: Implement runtime addition of pages @@ Commit message efi: mm: Implement runtime addition of pages Adjust the interface of `grub_efi_mm_add_regions ()` to take a set of - `GRUB_MM_REGION_*` flags, which most notably is currently only the + `GRUB_MM_ADD_REGION_*` flags, which most notably is currently only the `CONSECUTVE` flag. This allows us to set the function up as callback for the memory subsystem and have it call out to us in case there's not enough pages available in the current heap. @@ grub-core/kern/efi/mm.c: static grub_err_t grub_efi_memory_descriptor_t *memory_map_end, - grub_efi_uint64_t required_pages) + grub_efi_uint64_t required_pages, -+ char consecutive) ++ unsigned int flags) { grub_efi_memory_descriptor_t *desc; @@ grub-core/kern/efi/mm.c: add_memory_regions (grub_efi_memory_descriptor_t *memor start = desc->physical_start; pages = desc->num_pages; + -+ if (pages < required_pages && consecutive) ++ if (pages < required_pages && (flags & GRUB_MM_ADD_REGION_CONSECUTIVE)) + continue; + if (pages > required_pages) @@ grub-core/kern/efi/mm.c: print_memory_map (grub_efi_memory_descriptor_t *memory_ static grub_err_t -grub_efi_mm_add_regions (grub_efi_uint64_t required_bytes) -+grub_efi_mm_add_regions (grub_efi_uint64_t required_bytes, unsigned flags) ++grub_efi_mm_add_regions (grub_efi_uint64_t required_bytes, unsigned int flags) { grub_efi_memory_descriptor_t *memory_map; grub_efi_memory_descriptor_t *memory_map_end; @@ grub-core/kern/efi/mm.c: grub_efi_mm_add_regions (grub_efi_uint64_t required_byt filtered_memory_map_end, - BYTES_TO_PAGES (required_bytes)); + BYTES_TO_PAGES (required_bytes), -+ flags & GRUB_MM_REGION_CONSECUTIVE); ++ flags); if (err != GRUB_ERR_NONE) return err; @@ grub-core/kern/efi/mm.c: grub_efi_mm_add_regions (grub_efi_uint64_t required_byt grub_efi_mm_init (void) { - if (grub_efi_mm_add_regions (DEFAULT_HEAP_SIZE) != GRUB_ERR_NONE) -+ if (grub_efi_mm_add_regions (DEFAULT_HEAP_SIZE, 0) != GRUB_ERR_NONE) - grub_fatal (grub_errmsg); -+ grub_mm_region_fn = grub_efi_mm_add_regions; ++ if (grub_efi_mm_add_regions (DEFAULT_HEAP_SIZE, GRUB_MM_ADD_REGION_NONE) != GRUB_ERR_NONE) + grub_fatal ("%s", grub_errmsg); ++ grub_mm_add_region_fn = grub_efi_mm_add_regions; } #if defined (__aarch64__) || defined (__arm__) || defined (__riscv) -- 2.32.0
signature.asc
Description: PGP signature
_______________________________________________ Grub-devel mailing list Grub-devel@gnu.org https://lists.gnu.org/mailman/listinfo/grub-devel