[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 09/19] mm: Allow dynamically requesting additional memory regions
From: |
Daniel Axtens |
Subject: |
[PATCH 09/19] mm: Allow dynamically requesting additional memory regions |
Date: |
Tue, 12 Oct 2021 18:29:58 +1100 |
From: Patrick Steinhardt <ps@pks.im>
Currently, all platforms will set up their heap on initialization of the
platform code. While this works mostly fine, it poses some limitations
on memory management on us. Most notably, allocating big chunks of
memory in the gigabyte range would require us to pre-request this many
bytes from the firmware and add it to the heap from the beginning on
some platforms like EFI. As this isn't needed for most configurations,
it is inefficient and may even negatively impact some usecases when,
e.g., chainloading. Nonetheless, allocating big chunks of memory is
required sometimes, where one example is the upcoming support for the
Argon2 key derival function in LUKS2.
In order to avoid pre-allocating big chunks of memory, this commit
implements a runtime mechanism to add more pages to the system. When a
given allocation cannot be currently satisfied, we'll call a given
callback set up by the platform's own memory management subsystem,
asking it to add a memory area with at least `n` bytes. If this
succeeds, we retry searching for a valid memory region, which should now
succeed.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
[dja: add this to the documentation at the top of mm.c]
Signed-off-by: Daniel Axtens <dja@axtens.net>
---
grub-core/kern/mm.c | 13 +++++++++++++
include/grub/mm.h | 16 ++++++++++++++++
2 files changed, 29 insertions(+)
diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c
index 6038c0d0cbb2..58d5b89e8860 100644
--- a/grub-core/kern/mm.c
+++ b/grub-core/kern/mm.c
@@ -28,6 +28,9 @@
- multiple regions may be used as free space. They may not be
contiguous.
+ - if existing regions are insufficient to satisfy an allocation, a new
+ region can be requested from firmware.
+
Regions are managed by a singly linked list, and the meta information is
stored in the beginning of each region. Space after the meta information
is used to allocate memory.
@@ -81,6 +84,7 @@
grub_mm_region_t grub_mm_base;
+grub_mm_add_region_func_t grub_mm_add_region_fn;
/* Get a header from the pointer PTR, and set *P and *R to a pointer
to the header and a pointer to its region, respectively. PTR must
@@ -377,6 +381,15 @@ grub_memalign (grub_size_t align, grub_size_t size)
count++;
goto again;
+ case 1:
+ /* Request additional pages. */
+ count++;
+
+ if (grub_mm_add_region_fn && grub_mm_add_region_fn (size,
GRUB_MM_ADD_REGION_CONSECUTIVE) == GRUB_ERR_NONE)
+ goto again;
+
+ /* fallthrough */
+
default:
break;
}
diff --git a/include/grub/mm.h b/include/grub/mm.h
index 44fde7cb9033..5d916809666c 100644
--- a/include/grub/mm.h
+++ b/include/grub/mm.h
@@ -20,6 +20,7 @@
#ifndef GRUB_MM_H
#define GRUB_MM_H 1
+#include <grub/err.h>
#include <grub/types.h>
#include <grub/symbol.h>
#include <config.h>
@@ -28,6 +29,21 @@
# define NULL ((void *) 0)
#endif
+#define GRUB_MM_ADD_REGION_NONE 0
+#define GRUB_MM_ADD_REGION_CONSECUTIVE (1 << 0)
+
+/*
+ * Function used to request memory regions of `grub_size_t` bytes. The second
+ * parameter is a bitfield of `GRUB_MM_ADD_REGION` flags.
+ */
+typedef grub_err_t (*grub_mm_add_region_func_t) (grub_size_t, unsigned int);
+
+/*
+ * Set this function pointer to enable adding memory-regions at runtime in case
+ * a memory allocation cannot be satisfied with existing regions.
+ */
+extern grub_mm_add_region_func_t EXPORT_VAR(grub_mm_add_region_fn);
+
void grub_mm_init_region (void *addr, grub_size_t size);
void *EXPORT_FUNC(grub_calloc) (grub_size_t nmemb, grub_size_t size);
void *EXPORT_FUNC(grub_malloc) (grub_size_t size);
--
2.30.2
- Re: [PATCH 02/19] grub-shell: pseries: don't pass fw_opt to qemu, (continued)
- [PATCH 05/19] mm: when adding a region, merge with region after as well as before, Daniel Axtens, 2021/10/12
- [PATCH 06/19] configure: properly pass through MM_DEBUG, Daniel Axtens, 2021/10/12
- [PATCH 07/19] Add memtool module with memory allocation stress-test, Daniel Axtens, 2021/10/12
- [PATCH 08/19] mm: Drop unused unloading of modules on OOM, Daniel Axtens, 2021/10/12
- [PATCH 10/19] efi: mm: Always request a fixed number of pages on init, Daniel Axtens, 2021/10/12
- [PATCH 12/19] efi: mm: Pass up errors from `add_memory_regions ()`, Daniel Axtens, 2021/10/12
- [PATCH 09/19] mm: Allow dynamically requesting additional memory regions,
Daniel Axtens <=
- [PATCH 11/19] efi: mm: Extract function to add memory regions, Daniel Axtens, 2021/10/12
- [PATCH 13/19] efi: mm: Implement runtime addition of pages, Daniel Axtens, 2021/10/12
- [PATCH 15/19] ieee1275: drop len -= 1 quirk in heap_init, Daniel Axtens, 2021/10/12
- [PATCH 14/19] ieee1275: request memory with ibm, client-architecture-support, Daniel Axtens, 2021/10/12
- [PATCH 17/19] [not for merge] print more debug info in mm, Daniel Axtens, 2021/10/12
- [PATCH 16/19] ieee1275: support runtime memory claiming, Daniel Axtens, 2021/10/12
- [PATCH 18/19] [not for merge] ieee1275 debugging info, Daniel Axtens, 2021/10/12
- [PATCH 19/19] RFC: Ignore REGION_CONSECUTIVE, Daniel Axtens, 2021/10/12