All of lore.kernel.org
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: grub-devel@gnu.org
Cc: Leif Lindholm <leif@nuviainc.com>,
	Daniel Kiper <dkiper@net-space.pl>,
	Stefan Berger <stefanb@linux.ibm.com>
Subject: [PATCH v3 2/6] mm: Allow dynamically requesting additional memory regions
Date: Sun, 15 Aug 2021 13:09:14 +0200	[thread overview]
Message-ID: <3f0ec2a76c1b3aa722efdd540b4d3ecce1789750.1629025332.git.ps@pks.im> (raw)
In-Reply-To: <cover.1629025332.git.ps@pks.im>

[-- Attachment #1: Type: text/plain, Size: 3191 bytes --]

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>
---
 grub-core/kern/mm.c | 10 ++++++++++
 include/grub/mm.h   | 16 ++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c
index e0e580270..2df835392 100644
--- a/grub-core/kern/mm.c
+++ b/grub-core/kern/mm.c
@@ -81,6 +81,7 @@
 \f
 
 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
@@ -360,6 +361,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 9c38dd3ca..afde57d2e 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.32.0


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  parent reply	other threads:[~2021-08-15 11:09 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-15 11:09 [PATCH v3 0/6] Runtime allocation of memory regions Patrick Steinhardt
2021-08-15 11:09 ` [PATCH v3 1/6] mm: Drop unused unloading of modules on OOM Patrick Steinhardt
2021-08-15 11:09 ` Patrick Steinhardt [this message]
2021-09-01 14:48   ` [PATCH v3 2/6] mm: Allow dynamically requesting additional memory regions Daniel Axtens
2021-09-02 12:40     ` Daniel Kiper
2021-09-03 12:23       ` Daniel Axtens
2021-09-12 11:13         ` Patrick Steinhardt
2021-09-06  8:23       ` Daniel Axtens
2021-09-10  0:03         ` Daniel Kiper
2021-09-27 14:18           ` Daniel Axtens
2021-08-15 11:09 ` [PATCH v3 3/6] efi: mm: Always request a fixed number of pages on init Patrick Steinhardt
2021-08-15 11:09 ` [PATCH v3 4/6] efi: mm: Extract function to add memory regions Patrick Steinhardt
2021-08-15 11:09 ` [PATCH v3 5/6] efi: mm: Pass up errors from `add_memory_regions ()` Patrick Steinhardt
2021-08-15 11:09 ` [PATCH v3 6/6] efi: mm: Implement runtime addition of pages Patrick Steinhardt
2021-08-26 15:12 ` [PATCH v3 0/6] Runtime allocation of memory regions Daniel Kiper
2021-08-27  3:39   ` Daniel Axtens
2021-08-30 17:49     ` Daniel Kiper
2021-08-30 18:05       ` Patrick Steinhardt
2021-09-01  5:50         ` Daniel Axtens

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3f0ec2a76c1b3aa722efdd540b4d3ecce1789750.1629025332.git.ps@pks.im \
    --to=ps@pks.im \
    --cc=dkiper@net-space.pl \
    --cc=grub-devel@gnu.org \
    --cc=leif@nuviainc.com \
    --cc=stefanb@linux.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.