Skip to contentSkip to conversation

Requesting more memory from firmware

PATCH In review Daniel Axtens 3 participants 49 messages Oct 12, 2021 – Mar 25, 2022

lore
Share & tools
lore
#0 lore raw

cover Requesting more memory from firmware

Hi,

This extends Patrick's work on adding the ability to dynamically request more memory from firmware.

It now supports powerpc-ieee1275 - I can allocate pretty large chunks of memory and still successfully boot under both SLOF and Power8 PFW.

Structure of the series:

- Patches 1 & 2: little patches to fix tests. Probably mergeable as-is.

- Patch 3: Document some mm structures. Hopefully mergable as-is.

- Patch 4: internal mm consistency check.

- Patch 5: enhance the algorithm for merging a new region into an existing region.

- Patch 6: pass MM_DEBUG from configure into config.h

- Patch 7: a tool to test large memory allocations

- Patches 8-13: Patrick's series with some minor tweaks

- Patches 14-16: ieee1275 support

- Patches 17-18: debug print patches

- Patch 19: an RFC suggesting a possible improvement to Patrick's series.

Kind regards,
Daniel

Daniel Axtens (12):
grub-shell: pseries: don't pass fw_opt to qemu
mm: document grub internal memory management structures
mm: assert that we preserve header vs region alignment
mm: when adding a region, merge with region after as well as before
configure: properly pass through MM_DEBUG
Add memtool module with memory allocation stress-test
ieee1275: request memory with ibm,client-architecture-support
ieee1275: drop len -= 1 quirk in heap_init
ieee1275: support runtime memory claiming
[not for merge] print more debug info in mm
[not for merge] ieee1275 debugging info
RFC: Ignore REGION_CONSECUTIVE

Glenn Washburn (1):
grub-shell: Boot PowerPC using PMU instead of CUDA for power
management

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

#1 lore raw

PATCH 1/19 grub-shell: Boot PowerPC using PMU instead of CUDA for power management

From: Glenn Washburn <[email protected]>

A recent refactoring of CUDA command code has exposed a bug in OpenBIOS[1] which was causing system powerdown and system reset to fail, thus causing the Qemu instance to hang. This in turn caused the grub-shell command to timeout causing it to return an error code when the test actually completed successfully.

Since it could be a while before the patch fixing this issue in OpenBIOS filters down to the average distro, switch to PMU to allow powerdowns and reboots to work as expected.

[1] https://gitlab.com/qemu-project/qemu/-/issues/624

[dja: unbreak pseries]
---
 tests/util/grub-shell.in | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tests/util/grub-shell.in +2 -1
index 93e9f5148408..33590baeb13c 100644
@@ -84,6 +84,7 @@ case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
 	serial_null="-serial null"
 	netbootext=elf
 	trim=1
+	qemuopts="-M mac99,via=pmu $qemuopts"
 	;;
 
     sparc64-ieee1275)
@@ -231,7 +232,7 @@ for option in "$@"; do
 	qemu=qemu-system-ppc64
 	serial_port=ieee1275/hvterm
 	serial_null=
-	qemuopts="$qemuopts -M pseries -no-reboot"
+	qemuopts="$(echo $qemuopts | sed -E 's/-M [^ ]+//') -M pseries -no-reboot"
 	trim=1
 	pseries=y
 	    ;;
Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH 01/19] grub-shell: Boot PowerPC using PMU instead of CUDA for power management

Quoted 34 lines · On Tue, 12 Oct 2021 18:29:50 +1100 Daniel Axtens <[email protected]> wrote:

From: Glenn Washburn <[email protected]>

A recent refactoring of CUDA command code has exposed a bug in OpenBIOS[1] which was causing system powerdown and system reset to fail, thus causing the Qemu instance to hang. This in turn caused the grub-shell command to timeout causing it to return an error code when the test actually completed successfully.

Since it could be a while before the patch fixing this issue in OpenBIOS filters down to the average distro, switch to PMU to allow powerdowns and reboots to work as expected.

[1] https://gitlab.com/qemu-project/qemu/-/issues/624

[dja: unbreak pseries]
---
 tests/util/grub-shell.in | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tests/util/grub-shell.in +1 -0
index 93e9f5148408..33590baeb13c 100644
@@ -84,6 +84,7 @@ case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
 	serial_null="-serial null"
 	netbootext=elf
 	trim=1
+	qemuopts="-M mac99,via=pmu $qemuopts"
 	;;
 
     sparc64-ieee1275)

tests/util/grub-shell.in for option in "$@"; do · line 235

Quoted 5 lines
tests/util/grub-shell.in +1 -1
@@ -231,7 +232,7 @@ for option in "$@"; do
 	qemu=qemu-system-ppc64
 	serial_port=ieee1275/hvterm
 	serial_null=
-	qemuopts="$qemuopts -M pseries -no-reboot"
+	qemuopts="$(echo $qemuopts | sed -E 's/-M [^ ]+//') -M pseries -no-reboot"

LGTM.

Glenn

Quoted 3 lines
tests/util/grub-shell.in +0 -0
 	trim=1
 	pseries=y
 	    ;;
Reviewed-by
#3 lore raw

Re: [PATCH 01/19] grub-shell: Boot PowerPC using PMU instead of CUDA for power management

Quoted 35 lines · Glenn Washburn
Quoted 32 lines · On Tue, 12 Oct 2021 18:29:50 +1100 Daniel Axtens <[email protected]> wrote:

From: Glenn Washburn <[email protected]>

A recent refactoring of CUDA command code has exposed a bug in OpenBIOS[1] which was causing system powerdown and system reset to fail, thus causing the Qemu instance to hang. This in turn caused the grub-shell command to timeout causing it to return an error code when the test actually completed successfully.

Since it could be a while before the patch fixing this issue in OpenBIOS filters down to the average distro, switch to PMU to allow powerdowns and reboots to work as expected.

[1] https://gitlab.com/qemu-project/qemu/-/issues/624

[dja: unbreak pseries]
---
 tests/util/grub-shell.in | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tests/util/grub-shell.in +1 -0
index 93e9f5148408..33590baeb13c 100644
@@ -84,6 +84,7 @@ case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
 	serial_null="-serial null"
 	netbootext=elf
 	trim=1
+	qemuopts="-M mac99,via=pmu $qemuopts"
 	;;
 
Quoted 9 lines

tests/util/grub-shell.in for option in "$@"; do · line 235

Quoted 7 lines
tests/util/grub-shell.in +1 -1
     sparc64-ieee1275)
@@ -231,7 +232,7 @@ for option in "$@"; do
 	qemu=qemu-system-ppc64
 	serial_port=ieee1275/hvterm
 	serial_null=
-	qemuopts="$qemuopts -M pseries -no-reboot"
+	qemuopts="$(echo $qemuopts | sed -E 's/-M [^ ]+//') -M pseries -no-reboot"

LGTM.

Daniel

#4 lore raw

PATCH 2/19 grub-shell: pseries: don't pass fw_opt to qemu

In reply to Daniel Axtens

At some point this started to break the test - qemu just rejects the command line. Just don't pass it in, it's seabios specific.

---
 tests/util/grub-shell.in | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
tests/util/grub-shell.in +5 -1
index 33590baeb13c..92c0b6f320c3 100644
@@ -376,7 +376,11 @@ if test -z "$debug"; then
   # workaround unfortunately causes qemu to issue a warning 'externally
   # provided fw_cfg item names should be prefixed with "opt/"', but there
   # doesn't seem to be a better option.
-  qemuopts="${qemuopts} -fw_cfg name=etc/sercon-port,string=0"
+  #
+  # Don't do this on pseries, it breaks recent qemus.
+  if [ $pseries == n ]; then
+    qemuopts="${qemuopts} -fw_cfg name=etc/sercon-port,string=0"
+  fi
 fi
 
 if [ x$boot != xnet ] && [ x$boot != xemu ]; then
Signature · 1 line
2.30.2
Tested-by on v1 2/19
#5 lore raw

Re: [PATCH 02/19] grub-shell: pseries: don't pass fw_opt to qemu

Quoted 13 lines · On Tue, 12 Oct 2021 18:29:51 +1100 Daniel Axtens <[email protected]> wrote:

At some point this started to break the test - qemu just rejects the command line. Just don't pass it in, it's seabios specific.

---
 tests/util/grub-shell.in | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
tests/util/grub-shell.in +0 -0
index 33590baeb13c..92c0b6f320c3 100644

tests/util/grub-shell.in if test -z "$debug"; then · line 382

Quoted 8 lines
tests/util/grub-shell.in +4 -1
@@ -376,7 +376,11 @@ if test -z "$debug"; then
   # workaround unfortunately causes qemu to issue a warning 'externally
   # provided fw_cfg item names should be prefixed with "opt/"', but there
   # doesn't seem to be a better option.
-  qemuopts="${qemuopts} -fw_cfg name=etc/sercon-port,string=0"
+  #
+  # Don't do this on pseries, it breaks recent qemus.
+  if [ $pseries == n ]; then
+    qemuopts="${qemuopts} -fw_cfg name=etc/sercon-port,string=0"

I've confirmed that this is an issue on Qemu 5.2 from Debian 11 and that this patch works as advertised. The better solution would only be added on targets using SeaBIOS. This would only be i386, right? There is an HPPA port, but I don't think we support that architecture.

tests/util/grub-shell.in if test -z "$debug"; then · line 386

Quoted 4 lines
tests/util/grub-shell.in +1 -0
+  fi
 fi
 
 if [ x$boot != xnet ] && [ x$boot != xemu ]; then

Glenn

#6 lore raw

Re: [PATCH 02/19] grub-shell: pseries: don't pass fw_opt to qemu

Quoted 21 lines · Glenn Washburn
Quoted 18 lines · On Tue, 12 Oct 2021 18:29:51 +1100 Daniel Axtens <[email protected]> wrote:

At some point this started to break the test - qemu just rejects the command line. Just don't pass it in, it's seabios specific.

---
 tests/util/grub-shell.in | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
tests/util/grub-shell.in +1 -1
index 33590baeb13c..92c0b6f320c3 100644
@@ -376,7 +376,11 @@ if test -z "$debug"; then
   # workaround unfortunately causes qemu to issue a warning 'externally
   # provided fw_cfg item names should be prefixed with "opt/"', but there
   # doesn't seem to be a better option.
-  qemuopts="${qemuopts} -fw_cfg name=etc/sercon-port,string=0"
+  #
Quoted 8 lines

tests/util/grub-shell.in if test -z "$debug"; then · line 382

Quoted 3 lines
tests/util/grub-shell.in +3 -0
+  # Don't do this on pseries, it breaks recent qemus.
+  if [ $pseries == n ]; then
+    qemuopts="${qemuopts} -fw_cfg name=etc/sercon-port,string=0"

I've confirmed that this is an issue on Qemu 5.2 from Debian 11 and that this patch works as advertised. The better solution would only be added on targets using SeaBIOS. This would only be i386, right? There is an HPPA port, but I don't think we support that architecture.

Yeah, I agree, the sercon-port looks like the SeaBIOS specific thing. Daniel A., could you rework the patch?

Daniel

#7 lore raw

PATCH 3/19 mm: document grub internal memory management structures

In reply to Daniel Axtens

I spent more than a trivial quantity of time figuring out pre_size and whether a memory region's size contains the header cell or not.

Document the meanings of all the properties. Hopefully now no-one else has to figure it out!

---
 include/grub/mm_private.h | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
include/grub/mm_private.h +21 -0
index c2c4cb1511c6..e80a059dd4e4 100644
@@ -25,11 +25,18 @@
 #define GRUB_MM_FREE_MAGIC	0x2d3c2808
 #define GRUB_MM_ALLOC_MAGIC	0x6db08fa4
 
+/* A header describing a block of memory - either allocated or free */
 typedef struct grub_mm_header
 {
+  /* The 'next' free block in this region. A circular list.
+     Only meaningful if the block is free.
+   */
   struct grub_mm_header *next;
+  /* The region size in cells (not bytes). Includes the header cell. */
   grub_size_t size;
+  /* either free or alloc magic, depending on the block type. */
   grub_size_t magic;
+  /* pad to cell size: see the top of mm.c. */
 #if GRUB_CPU_SIZEOF_VOID_P == 4
   char padding[4];
 #elif GRUB_CPU_SIZEOF_VOID_P == 8
@@ -48,11 +55,25 @@ typedef struct grub_mm_header
 
 #define GRUB_MM_ALIGN	(1 << GRUB_MM_ALIGN_LOG2)
 
+/* A region from which we can make allocations. */
 typedef struct grub_mm_region
 {
+  /* The first free block in this region. */
   struct grub_mm_header *first;
+
+  /* The next region in the linked list of regions. Regions are initially
+     sorted in order of increasing size, but can grow, in which case the
+     ordering may not be preserved.
+   */
   struct grub_mm_region *next;
+
+  /* A grub_mm_region will always be aligned to cell size. The pre-size is
+     the number of bytes we were given but had to skip in order to get that
+     alignment.
+   */
   grub_size_t pre_size;
+
+  /* How many bytes are in this region? (free and allocated) */
   grub_size_t size;
 }
 *grub_mm_region_t;
Signature · 1 line
2.30.2
#8 lore raw

Re: [PATCH 03/19] mm: document grub internal memory management structures

Quoted 16 lines · On Tue, 12 Oct 2021 18:29:52 +1100 Daniel Axtens <[email protected]> wrote:

I spent more than a trivial quantity of time figuring out pre_size and whether a memory region's size contains the header cell or not.

Document the meanings of all the properties. Hopefully now no-one else has to figure it out!

---
 include/grub/mm_private.h | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
include/grub/mm_private.h +0 -0
index c2c4cb1511c6..e80a059dd4e4 100644

include/grub/mm_private.h line 34

Quoted 10 lines
include/grub/mm_private.h +4 -0
@@ -25,11 +25,18 @@
 #define GRUB_MM_FREE_MAGIC	0x2d3c2808
 #define GRUB_MM_ALLOC_MAGIC	0x6db08fa4
 
+/* A header describing a block of memory - either allocated or free */
 typedef struct grub_mm_header
 {
+  /* The 'next' free block in this region. A circular list.
+     Only meaningful if the block is free.
+   */
   struct grub_mm_header *next;

This and the subsequent comments do not follow the multiline comment style[1].

One nit, "region. A circular" -> "region's circular".

This comment leads me to wonder if the block is not free, what is the value of next? Seems like it should be NULL, if its not meaningful.

https://www.gnu.org/software/grub/manual/grub-dev/grub-dev.html#Multi_002dLine-Comments

include/grub/mm_private.h line 35

Quoted 1 line
include/grub/mm_private.h +1 -0
+  /* The region size in cells (not bytes). Includes the header cell. */

What exactly does "cell" mean in this context? I'm gathering cell is not defined as in this link[2], which is equivalent to "bit". Perhaps "size" should be renamed to "ncells" or something more descriptive.

[2] https://en.wikipedia.org/wiki/Memory_cell_(computing)

Quoted 23 lines
include/grub/mm_private.h +10 -0
   grub_size_t size;
+  /* either free or alloc magic, depending on the block type. */
   grub_size_t magic;
+  /* pad to cell size: see the top of mm.c. */
 #if GRUB_CPU_SIZEOF_VOID_P == 4
   char padding[4];
 #elif GRUB_CPU_SIZEOF_VOID_P == 8
@@ -48,11 +55,25 @@ typedef struct grub_mm_header
 
 #define GRUB_MM_ALIGN	(1 << GRUB_MM_ALIGN_LOG2)
 
+/* A region from which we can make allocations. */
 typedef struct grub_mm_region
 {
+  /* The first free block in this region. */
   struct grub_mm_header *first;
+
+  /* The next region in the linked list of regions. Regions are initially
+     sorted in order of increasing size, but can grow, in which case the
+     ordering may not be preserved.
+   */
   struct grub_mm_region *next;
+

include/grub/mm_private.h typedef struct grub_mm_header · line 79

Quoted 10 lines
include/grub/mm_private.h +6 -0
+  /* A grub_mm_region will always be aligned to cell size. The pre-size is
+     the number of bytes we were given but had to skip in order to get that
+     alignment.
+   */
   grub_size_t pre_size;
+
+  /* How many bytes are in this region? (free and allocated) */
   grub_size_t size;
 }
 *grub_mm_region_t;

Glenn

#9 lore raw

Re: [PATCH 03/19] mm: document grub internal memory management structures

Quoted 22 lines · Glenn Washburn
Quoted 19 lines · On Tue, 12 Oct 2021 18:29:52 +1100 Daniel Axtens <[email protected]> wrote:

I spent more than a trivial quantity of time figuring out pre_size and whether a memory region's size contains the header cell or not.

Document the meanings of all the properties. Hopefully now no-one else has to figure it out!

---
 include/grub/mm_private.h | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
include/grub/mm_private.h +0 -0
index c2c4cb1511c6..e80a059dd4e4 100644
@@ -25,11 +25,18 @@
 #define GRUB_MM_FREE_MAGIC	0x2d3c2808
 #define GRUB_MM_ALLOC_MAGIC	0x6db08fa4
 
Quoted 10 lines

include/grub/mm_private.h line 34

Quoted 7 lines
include/grub/mm_private.h +4 -0
+/* A header describing a block of memory - either allocated or free */
 typedef struct grub_mm_header
 {
+  /* The 'next' free block in this region. A circular list.
+     Only meaningful if the block is free.
+   */
   struct grub_mm_header *next;

This and the subsequent comments do not follow the multiline comment style[1].

Will fix, thanks.

Quoted 1 line

One nit, "region. A circular" -> "region's circular".

Sure, will do.

Quoted 2 lines

This comment leads me to wonder if the block is not free, what is the value of next? Seems like it should be NULL, if its not meaningful.

In practice the value will be:

- if the block was converted from an existing free block: whatever the 'next' pointer was when the block was free

- if the block was carved out of the middle or end of an existing free
block: whatever happened to be at that offset in memory.

We certainly _could_ null it out. I don't think there's any real value in doing so but I'm open to being convinced. I suppose you could make an argument that it reduces the chance that we'll follow a pointer to somewhere random in memory but we check the magic before we check next so we're fairly safe.

Quoted 9 lines

https://www.gnu.org/software/grub/manual/grub-dev/grub-dev.html#Multi_002dLine-Comments

include/grub/mm_private.h line 35

Quoted 1 line
include/grub/mm_private.h +1 -0
+  /* The region size in cells (not bytes). Includes the header cell. */

What exactly does "cell" mean in this context? I'm gathering cell is not defined as in this link[2], which is equivalent to "bit". Perhaps "size" should be renamed to "ncells" or something more descriptive.

[2] https://en.wikipedia.org/wiki/Memory_cell_(computing)

The definition of cell is from mm.c:

The memory space is used as cells instead of bytes for simplicity. This is important for some CPUs which may not access multiple bytes at a time when the first byte is not aligned at a certain boundary (typically, 4-byte or 8-byte). The size of each cell is equal to the size of struct grub_mm_header, so the header of each allocated/free block fits into one cell precisely. One cell is 16 bytes on 32-bit platforms and 32 bytes on 64-bit platforms.

I don't want to change variable names in this patch but I will update the comment.

Kind regards,
Daniel

#10 lore raw

PATCH 4/19 mm: assert that we preserve header vs region alignment

In reply to Daniel Axtens

grub_mm_region_init() does:

  h = (grub_mm_header_t) (r + 1);

where h is a grub_mm_header_t and r is a grub_mm_region_t.

Cells are supposed to be GRUB_MM_ALIGN aligned, but while grub_mm_dump ensures this vs the region header, grub_mm_region_init() does not.

It's better to be explicit than implicit here: rather than changing grub_mm_region_init() to ALIGN_UP(), require that the struct is explictly a multiple of the header size.

---
 include/grub/mm_private.h | 8 ++++++++
 1 file changed, 8 insertions(+)
include/grub/mm_private.h +8 -0
index e80a059dd4e4..533b47173e18 100644
@@ -20,6 +20,7 @@
 #define GRUB_MM_PRIVATE_H	1
 
 #include <grub/mm.h>
+#include <grub/misc.h>
 
 /* Magic words.  */
 #define GRUB_MM_FREE_MAGIC	0x2d3c2808
@@ -82,4 +83,11 @@ typedef struct grub_mm_region
 extern grub_mm_region_t EXPORT_VAR (grub_mm_base);
 #endif
 
+static inline void grub_mm_size_sanity_check(void) {
+  /* Ensure we preserve alignment when doing h = (grub_mm_header_t) (r + 1) */
+  COMPILE_TIME_ASSERT((sizeof(struct grub_mm_region) %
+		       sizeof(struct grub_mm_header)) == 0);
+
+}
+
 #endif
Signature · 1 line
2.30.2
#11 lore raw

Re: [PATCH 04/19] mm: assert that we preserve header vs region alignment

Quoted 31 lines · Daniel Axtens

grub_mm_region_init() does:

  h = (grub_mm_header_t) (r + 1);

where h is a grub_mm_header_t and r is a grub_mm_region_t.

Cells are supposed to be GRUB_MM_ALIGN aligned, but while grub_mm_dump ensures this vs the region header, grub_mm_region_init() does not.

It's better to be explicit than implicit here: rather than changing grub_mm_region_init() to ALIGN_UP(), require that the struct is explictly a multiple of the header size.

---
 include/grub/mm_private.h | 8 ++++++++
 1 file changed, 8 insertions(+)
include/grub/mm_private.h +1 -0
index e80a059dd4e4..533b47173e18 100644
@@ -20,6 +20,7 @@
 #define GRUB_MM_PRIVATE_H	1
 
 #include <grub/mm.h>
+#include <grub/misc.h>
 
 /* Magic words.  */
 #define GRUB_MM_FREE_MAGIC	0x2d3c2808

include/grub/mm_private.h typedef struct grub_mm_region · line 86

Quoted 4 lines
include/grub/mm_private.h +1 -0
@@ -82,4 +83,11 @@ typedef struct grub_mm_region
 extern grub_mm_region_t EXPORT_VAR (grub_mm_base);
 #endif
 
+static inline void grub_mm_size_sanity_check(void) {

static inline void
grub_mm_size_sanity_check (void)
{

include/grub/mm_private.h typedef struct grub_mm_region · line 88

Quoted 2 lines
include/grub/mm_private.h +2 -0
+  /* Ensure we preserve alignment when doing h = (grub_mm_header_t) (r + 1) */
+  COMPILE_TIME_ASSERT((sizeof(struct grub_mm_region) %

Missing spaces before "(" above and below...

s/struct grub_mm_region/grub_mm_region_t/
s/struct grub_mm_header/grub_mm_header_t/

include/grub/mm_private.h typedef struct grub_mm_region · line 89

Quoted 1 line
include/grub/mm_private.h +1 -0
+		       sizeof(struct grub_mm_header)) == 0);

I think this check is insufficient. The GRUB_MM_ALIGN should be checked somehow too. E.g. if sizeof(struct grub_mm_region) == 16, sizeof(struct grub_mm_header) == 8 and GRUB_MM_ALIGN == 32 then assert above will pass but the GRUB will blow up later. Of course numbers are not real but I chose them to show the problem. Hmmm... If we could choose grub_mm_region_t and grub_mm_header_t sizes properly then probably we could drop GRUB_MM_ALIGN...

Daniel

#12 lore raw

Re: [PATCH 04/19] mm: assert that we preserve header vs region alignment

Quoted 3 lines

s/struct grub_mm_region/grub_mm_region_t/
s/struct grub_mm_header/grub_mm_header_t/

The problem is that grub_mm_{region,header}_t is a pointer type, not a struct type. So sizeof (grub_mm_region_t) == sizeof(void *). You also can't do sizeof (*grub_mm_region_t), because you can't dereference a type. If there's a better way to express this I am open to it, but for now I will stick with the struct type...

Quoted 11 lines

include/grub/mm_private.h typedef struct grub_mm_region · line 89

Quoted 2 lines
include/grub/mm_private.h +1 -0
+		       sizeof(struct grub_mm_header)) == 0);

I think this check is insufficient. The GRUB_MM_ALIGN should be checked somehow too. E.g. if sizeof(struct grub_mm_region) == 16, sizeof(struct grub_mm_header) == 8 and GRUB_MM_ALIGN == 32 then assert above will pass but the GRUB will blow up later. Of course numbers are not real but I chose them to show the problem. Hmmm... If we could choose grub_mm_region_t and grub_mm_header_t sizes properly then probably we could drop GRUB_MM_ALIGN...

I have added that sizeof(struct header) == GRUB_MM_ALIGN. GRUB_MM_ALIGN is supposed to align you to a cell, and a header is supposed to be 1 cell.

We probably _could_ do away with the constant but I think that requires a bit more close thought.

Kind regards,
Daniel

#13 lore raw

Re: [PATCH 04/19] mm: assert that we preserve header vs region alignment

Quoted 8 lines · Daniel Axtens
Quoted 2 lines

s/struct grub_mm_region/grub_mm_region_t/
s/struct grub_mm_header/grub_mm_header_t/

The problem is that grub_mm_{region,header}_t is a pointer type, not a struct type. So sizeof (grub_mm_region_t) == sizeof(void *). You also can't do sizeof (*grub_mm_region_t), because you can't dereference a type. If there's a better way to express this I am open to it, but for now I will stick with the struct type...

OK.

Quoted 11 lines
Quoted 10 lines

include/grub/mm_private.h typedef struct grub_mm_region · line 89

Quoted 1 line
include/grub/mm_private.h +1 -0
+		       sizeof(struct grub_mm_header)) == 0);

I think this check is insufficient. The GRUB_MM_ALIGN should be checked somehow too. E.g. if sizeof(struct grub_mm_region) == 16, sizeof(struct grub_mm_header) == 8 and GRUB_MM_ALIGN == 32 then assert above will pass but the GRUB will blow up later. Of course numbers are not real but I chose them to show the problem. Hmmm... If we could choose grub_mm_region_t and grub_mm_header_t sizes properly then probably we could drop GRUB_MM_ALIGN...

Quoted 3 lines

I have added that sizeof(struct header) == GRUB_MM_ALIGN. GRUB_MM_ALIGN is supposed to align you to a cell, and a header is supposed to be 1 cell.

Cool!

Quoted 2 lines

We probably _could_ do away with the constant but I think that requires a bit more close thought.

Yeah, I think additional GRUB_MM_ALIGN check is enough right now. It does not make sense to spent more time on this and block the other series any longer.

Thanks,

Daniel

#14 lore raw

PATCH 5/19 mm: when adding a region, merge with region after as well as before

In reply to Daniel Axtens

On x86_64-efi (at least) regions seem to be added from top down. The mm code will merge a new region with an existing region that comes immediately before the new region. This allows larger allocations to be satisfied that would otherwise be the case.

On powerpc-ieee1275, however, regions are added from bottom up. So if we add 3x 32MB regions, we can still only satisfy a 32MB allocation, rather than the 96MB allocation we might otherwise be able to satisfy.

* Define 'post_size' as being bytes lost to the end of an allocation due to being given weird sizes from firmware that are not multiples of GRUB_MM_ALIGN.

* Allow merging of regions immediately _after_ existing regions, not just before. As with the other approach, we create an allocated block to represent the new space and the pass it to grub_free() to get the metadata right.

---
 grub-core/kern/mm.c       | 55 +++++++++++++++++++++++++--------------
 include/grub/mm_private.h | 15 +++++++++++
 2 files changed, 51 insertions(+), 19 deletions(-)
grub-core/kern/mm.c +36 -19
index c070afc621f8..835ed8a8f6f9 100644
@@ -129,25 +129,41 @@ grub_mm_init_region (void *addr, grub_size_t size)
     size = ((grub_addr_t) -0x1000) - (grub_addr_t) addr;
 
   for (p = &grub_mm_base, q = *p; q; p = &(q->next), q = *p)
-    if ((grub_uint8_t *) addr + size + q->pre_size == (grub_uint8_t *) q)
-      {
-	r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN);
-	*r = *q;
-	r->pre_size += size;
-	
-	if (r->pre_size >> GRUB_MM_ALIGN_LOG2)
-	  {
-	    h = (grub_mm_header_t) (r + 1);
-	    h->size = (r->pre_size >> GRUB_MM_ALIGN_LOG2);
-	    h->magic = GRUB_MM_ALLOC_MAGIC;
-	    r->size += h->size << GRUB_MM_ALIGN_LOG2;
-	    r->pre_size &= (GRUB_MM_ALIGN - 1);
-	    *p = r;
-	    grub_free (h + 1);
-	  }
-	*p = r;
-	return;
-      }
+    {
+      /* Does this region come _before_ an existing region? */
+      if ((grub_uint8_t *) addr + size + q->pre_size == (grub_uint8_t *) q)
+	{
+	  r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN);
+	  *r = *q;
+	  r->pre_size += size;
+
+	  if (r->pre_size >> GRUB_MM_ALIGN_LOG2)
+	    {
+	      h = (grub_mm_header_t) (r + 1);
+	      h->size = (r->pre_size >> GRUB_MM_ALIGN_LOG2);
+	      h->magic = GRUB_MM_ALLOC_MAGIC;
+	      r->size += h->size << GRUB_MM_ALIGN_LOG2;
+	      r->pre_size &= (GRUB_MM_ALIGN - 1);
+	      *p = r;
+	      grub_free (h + 1);
+	    }
+	  *p = r;
+	  return;
+	}
+
+      /* Does this region come _after_ an existing region? */
+      if ((grub_uint8_t *)q + sizeof(*q) + q->size + q->post_size ==
+	  (grub_uint8_t *) addr)
+	{
+	  h = (grub_mm_header_t) ((grub_uint8_t *)addr - q->post_size);
+	  h->size = (size + q->post_size) >> GRUB_MM_ALIGN_LOG2;
+	  h->magic = GRUB_MM_ALLOC_MAGIC;
+	  q->size += h->size << GRUB_MM_ALIGN_LOG2;
+	  q->post_size = (q->post_size + size) & (GRUB_MM_ALIGN - 1);
+	  grub_free (h + 1);
+	  return;
+	}
+    }
 
   /* Allocate a region from the head.  */
   r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN);
@@ -166,6 +182,7 @@ grub_mm_init_region (void *addr, grub_size_t size)
   r->first = h;
   r->pre_size = (grub_addr_t) r - (grub_addr_t) addr;
   r->size = (h->size << GRUB_MM_ALIGN_LOG2);
+  r->post_size = size - r->size;
 
   /* Find where to insert this region. Put a smaller one before bigger ones,
      to prevent fragmentation.  */
include/grub/mm_private.h +15 -0
index 533b47173e18..0effbc45a668 100644
@@ -74,8 +74,23 @@ typedef struct grub_mm_region
    */
   grub_size_t pre_size;
 
+  /* Likewise, the post-size is the number of bytes we wasted at the end
+     of the allocation because it wasn't a multiple of GRUB_MM_ALIGN
+   */
+  grub_size_t post_size;
+
   /* How many bytes are in this region? (free and allocated) */
   grub_size_t size;
+
+  /* pad to a multiple of cell size */
+#if GRUB_CPU_SIZEOF_VOID_P == 4
+  char padding[4+4+4];
+#elif GRUB_CPU_SIZEOF_VOID_P == 8
+  char padding[8+8+8];
+#else
+# error "unknown word size"
+#endif
+
 }
 *grub_mm_region_t;
 
Signature · 1 line
2.30.2
#15 lore raw

Re: [PATCH 05/19] mm: when adding a region, merge with region after as well as before

Quoted 74 lines · Daniel Axtens

On x86_64-efi (at least) regions seem to be added from top down. The mm code will merge a new region with an existing region that comes immediately before the new region. This allows larger allocations to be satisfied that would otherwise be the case.

On powerpc-ieee1275, however, regions are added from bottom up. So if we add 3x 32MB regions, we can still only satisfy a 32MB allocation, rather than the 96MB allocation we might otherwise be able to satisfy.

* Define 'post_size' as being bytes lost to the end of an allocation due to being given weird sizes from firmware that are not multiples of GRUB_MM_ALIGN.

* Allow merging of regions immediately _after_ existing regions, not just before. As with the other approach, we create an allocated block to represent the new space and the pass it to grub_free() to get the metadata right.

---
 grub-core/kern/mm.c       | 55 +++++++++++++++++++++++++--------------
 include/grub/mm_private.h | 15 +++++++++++
 2 files changed, 51 insertions(+), 19 deletions(-)
grub-core/kern/mm.c +23 -19
index c070afc621f8..835ed8a8f6f9 100644
@@ -129,25 +129,41 @@ grub_mm_init_region (void *addr, grub_size_t size)
     size = ((grub_addr_t) -0x1000) - (grub_addr_t) addr;
 
   for (p = &grub_mm_base, q = *p; q; p = &(q->next), q = *p)
-    if ((grub_uint8_t *) addr + size + q->pre_size == (grub_uint8_t *) q)
-      {
-	r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN);
-	*r = *q;
-	r->pre_size += size;
-
-	if (r->pre_size >> GRUB_MM_ALIGN_LOG2)
-	  {
-	    h = (grub_mm_header_t) (r + 1);
-	    h->size = (r->pre_size >> GRUB_MM_ALIGN_LOG2);
-	    h->magic = GRUB_MM_ALLOC_MAGIC;
-	    r->size += h->size << GRUB_MM_ALIGN_LOG2;
-	    r->pre_size &= (GRUB_MM_ALIGN - 1);
-	    *p = r;
-	    grub_free (h + 1);
-	  }
-	*p = r;
-	return;
-      }
+    {
+      /* Does this region come _before_ an existing region? */
+      if ((grub_uint8_t *) addr + size + q->pre_size == (grub_uint8_t *) q)
+	{
+	  r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN);
+	  *r = *q;
+	  r->pre_size += size;
+
+	  if (r->pre_size >> GRUB_MM_ALIGN_LOG2)
+	    {
+	      h = (grub_mm_header_t) (r + 1);
+	      h->size = (r->pre_size >> GRUB_MM_ALIGN_LOG2);
+	      h->magic = GRUB_MM_ALLOC_MAGIC;
+	      r->size += h->size << GRUB_MM_ALIGN_LOG2;
+	      r->pre_size &= (GRUB_MM_ALIGN - 1);
+	      *p = r;
+	      grub_free (h + 1);
+	    }
+	  *p = r;
+	  return;
+	}
+
+      /* Does this region come _after_ an existing region? */

grub-core/kern/mm.c grub_mm_init_region (void *addr, grub_size_t size) · line 164

Quoted 10 lines
grub-core/kern/mm.c +10 -0
+      if ((grub_uint8_t *)q + sizeof(*q) + q->size + q->post_size ==
+	  (grub_uint8_t *) addr)
+	{
+	  h = (grub_mm_header_t) ((grub_uint8_t *)addr - q->post_size);
+	  h->size = (size + q->post_size) >> GRUB_MM_ALIGN_LOG2;
+	  h->magic = GRUB_MM_ALLOC_MAGIC;
+	  q->size += h->size << GRUB_MM_ALIGN_LOG2;
+	  q->post_size = (q->post_size + size) & (GRUB_MM_ALIGN - 1);
+	  grub_free (h + 1);
+	  return;

The code itself LGTM. However, I would be more than happy if you add bunch of comments and maybe some ASCII drawings explaining what is happening here. This should save a lot time of our successors... :-)

Quoted 18 lines
grub-core/kern/mm.c +3 -0
+	}
+    }
 
   /* Allocate a region from the head.  */
   r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN);
@@ -166,6 +182,7 @@ grub_mm_init_region (void *addr, grub_size_t size)
   r->first = h;
   r->pre_size = (grub_addr_t) r - (grub_addr_t) addr;
   r->size = (h->size << GRUB_MM_ALIGN_LOG2);
+  r->post_size = size - r->size;
 
   /* Find where to insert this region. Put a smaller one before bigger ones,
      to prevent fragmentation.  */
include/grub/mm_private.h +0 -0
index 533b47173e18..0effbc45a668 100644

include/grub/mm_private.h typedef struct grub_mm_region · line 79

Quoted 6 lines
include/grub/mm_private.h +3 -0
@@ -74,8 +74,23 @@ typedef struct grub_mm_region
    */
   grub_size_t pre_size;
 
+  /* Likewise, the post-size is the number of bytes we wasted at the end
+     of the allocation because it wasn't a multiple of GRUB_MM_ALIGN
+   */

Please fix the formatting of this comment.

include/grub/mm_private.h typedef struct grub_mm_region · line 92

Quoted 13 lines
include/grub/mm_private.h +11 -0
+  grub_size_t post_size;
+
   /* How many bytes are in this region? (free and allocated) */
   grub_size_t size;
+
+  /* pad to a multiple of cell size */
+#if GRUB_CPU_SIZEOF_VOID_P == 4
+  char padding[4+4+4];
+#elif GRUB_CPU_SIZEOF_VOID_P == 8
+  char padding[8+8+8];
+#else
+# error "unknown word size"
+#endif

grub_uint8_t padding[3 * GRUB_CPU_SIZEOF_VOID_P];?

Or do not we have a construct in the C which allows us to add paddings like that one in smarter way?

Daniel

#16 lore raw

PATCH 6/19 configure: properly pass through MM_DEBUG

In reply to Daniel Axtens

I noticed that compiling with --enable-mm-debug didn't cause the functions in #ifdef MM_DEBUG to be compiled in. Somehow the variable wasn't actually being substituted into anything that was built.

Change configure.ac to do AC_SUBST(), and put a substitution into config.h.in. This makes MM_DEBUG available to any file which includes config.h.

---
 config.h.in  | 3 +++
 configure.ac | 6 ++++--
 2 files changed, 7 insertions(+), 2 deletions(-)
config.h.in +3 -0
index 9e8f9911b183..8cf62616cad7 100644
@@ -13,6 +13,9 @@
 #define DISK_CACHE_STATS @DISK_CACHE_STATS@
 #define BOOT_TIME_STATS @BOOT_TIME_STATS@
 
+/* Define to 1 to enable mm debugging */
+#define MM_DEBUG @MM_DEBUG@
+
 /* We don't need those.  */
 #define MINILZO_CFG_SKIP_LZO_PTR 1
 #define MINILZO_CFG_SKIP_LZO_UTIL 1
configure.ac +4 -2
index 8d1c81a7316e..889d07b3c1d0 100644
@@ -1511,9 +1511,11 @@ AC_ARG_ENABLE([mm-debug],
 	      AS_HELP_STRING([--enable-mm-debug],
                              [include memory manager debugging]))
 if test x$enable_mm_debug = xyes; then
-    AC_DEFINE([MM_DEBUG], [1],
-            [Define to 1 if you enable memory manager debugging.])
+    MM_DEBUG=1
+else
+    MM_DEBUG=0
 fi
+AC_SUBST([MM_DEBUG])
 
 AC_ARG_ENABLE([cache-stats],
 	      AS_HELP_STRING([--enable-cache-stats],
Signature · 1 line
2.30.2
#17 lore raw

Re: [PATCH 06/19] configure: properly pass through MM_DEBUG

Quoted 33 lines · Daniel Axtens

I noticed that compiling with --enable-mm-debug didn't cause the functions in #ifdef MM_DEBUG to be compiled in. Somehow the variable wasn't actually being substituted into anything that was built.

Change configure.ac to do AC_SUBST(), and put a substitution into config.h.in. This makes MM_DEBUG available to any file which includes config.h.

---
 config.h.in  | 3 +++
 configure.ac | 6 ++++--
 2 files changed, 7 insertions(+), 2 deletions(-)
config.h.in +3 -0
index 9e8f9911b183..8cf62616cad7 100644
@@ -13,6 +13,9 @@
 #define DISK_CACHE_STATS @DISK_CACHE_STATS@
 #define BOOT_TIME_STATS @BOOT_TIME_STATS@
 
+/* Define to 1 to enable mm debugging */
+#define MM_DEBUG @MM_DEBUG@
+
 /* We don't need those.  */
 #define MINILZO_CFG_SKIP_LZO_PTR 1
 #define MINILZO_CFG_SKIP_LZO_UTIL 1
configure.ac +0 -0
index 8d1c81a7316e..889d07b3c1d0 100644

configure.ac AC_ARG_ENABLE([mm-debug], · line 1518

Quoted 10 lines
configure.ac +4 -2
@@ -1511,9 +1511,11 @@ AC_ARG_ENABLE([mm-debug],
 	      AS_HELP_STRING([--enable-mm-debug],
                              [include memory manager debugging]))
 if test x$enable_mm_debug = xyes; then
-    AC_DEFINE([MM_DEBUG], [1],
-            [Define to 1 if you enable memory manager debugging.])
+    MM_DEBUG=1
+else
+    MM_DEBUG=0
 fi
+AC_SUBST([MM_DEBUG])

It seems to me this patch is incorrect. The MM_DEBUG constant is properly defined in config-util.h (config-util.h.in contains "#undef MM_DEBUG") which is included conditionally from config.h. The key word here is "conditionally". It means MM_DEBUG is defined when utils are build but it is not defined when the GRUB itself is build. Of course it does not make a lot of sense but it is what it is. I think this is due to how config-util.h and config.h are generated. The former is generated using AC_CONFIG_HEADER() and the latter is generated using AC_CONFIG_FILES(). I tried to move "#undef MM_DEBUG" to the config.h.in but it does not help for some reason. If you could investigate this further that would be perfect...

Daniel

#18 lore raw

PATCH 7/19 Add memtool module with memory allocation stress-test

In reply to Daniel Axtens

When working on memory, it's nice to be able to test your work.

Add a memtest module. When compiled with --enable-mm-debug, it exposes 3 commands:

* lsmem - print all allocations and free space in all regions
* lsfreemem - print free space in all regions

* stress_big_allocs - stress test large allocations:
- how much memory can we allocate in one chunk?
- how many 1MB chunks can we allocate?
- check that gap-filling works with a 1MB aligned 900kB alloc + a

     100kB alloc.

---

I've put this as copyright IBM for now - hopefully we can conclude on whether we're still doing FSF copyright assignments?

---
 grub-core/Makefile.core.def   |   5 ++
 grub-core/commands/memtools.c | 157 ++++++++++++++++++++++++++++++++++
 grub-core/kern/mm.c           |   4 +
 include/grub/mm.h             |   4 +-
 4 files changed, 168 insertions(+), 2 deletions(-)
 create mode 100644 grub-core/commands/memtools.c
grub-core/Makefile.core.def +5 -0
index 8022e1c0a794..0cc3a4a500ec 100644
@@ -2527,3 +2527,8 @@ module = {
   common = commands/i386/wrmsr.c;
   enable = x86;
 };
+
+module = {
+  name = memtools;
+  common = commands/memtools.c;
+};
grub-core/commands/memtools.c new file +157 -0

157 lines not on this page · raw message

grub-core/kern/mm.c +4 -0
index 835ed8a8f6f9..032c8f71aed2 100644
@@ -556,6 +556,8 @@ grub_mm_dump_free (void)
     {
       grub_mm_header_t p;
 
+      grub_printf ("Region %p (size %" PRIuGRUB_SIZE ")\n\n", r, r->size);
+
       /* Follow the free list.  */
       p = r->first;
       do
@@ -583,6 +585,8 @@ grub_mm_dump (unsigned lineno)
     {
       grub_mm_header_t p;
 
+      grub_printf ("Region %p (size %" PRIuGRUB_SIZE ")\n\n", r, r->size);
+
       for (p = (grub_mm_header_t) ALIGN_UP ((grub_addr_t) (r + 1),
 					    GRUB_MM_ALIGN);
 	   (grub_addr_t) p < (grub_addr_t) (r+1) + r->size;
include/grub/mm.h +2 -2
index 9c38dd3ca5d2..44fde7cb9033 100644
@@ -46,8 +46,8 @@ void grub_mm_check_real (const char *file, int line);
 /* Set this variable to 1 when you want to trace all memory function calls.  */
 extern int EXPORT_VAR(grub_mm_debug);
 
-void grub_mm_dump_free (void);
-void grub_mm_dump (unsigned lineno);
+void EXPORT_FUNC(grub_mm_dump_free) (void);
+void EXPORT_FUNC(grub_mm_dump) (unsigned lineno);
 
 #define grub_calloc(nmemb, size)	\
   grub_debug_calloc (GRUB_FILE, __LINE__, nmemb, size)
Signature · 1 line
2.30.2
#19 lore raw

Re: [PATCH 07/19] Add memtool module with memory allocation stress-test

Quoted 5 lines · On Tue, 12 Oct 2021 18:29:56 +1100 Daniel Axtens <[email protected]> wrote:

When working on memory, it's nice to be able to test your work.

Add a memtest module. When compiled with --enable-mm-debug, it exposes 3 commands:

Quoted 8 lines

* lsmem - print all allocations and free space in all regions
* lsfreemem - print free space in all regions

* stress_big_allocs - stress test large allocations:
- how much memory can we allocate in one chunk?
- how many 1MB chunks can we allocate?
- check that gap-filling works with a 1MB aligned 900kB alloc + a

     100kB alloc.

Perhaps note here that the questions are "up to 4GB".

Quoted 83 lines

---

I've put this as copyright IBM for now - hopefully we can conclude on whether we're still doing FSF copyright assignments?

---
 grub-core/Makefile.core.def   |   5 ++
 grub-core/commands/memtools.c | 157 ++++++++++++++++++++++++++++++++++
 grub-core/kern/mm.c           |   4 +
 include/grub/mm.h             |   4 +-
 4 files changed, 168 insertions(+), 2 deletions(-)
 create mode 100644 grub-core/commands/memtools.c
grub-core/Makefile.core.def +5 -0
index 8022e1c0a794..0cc3a4a500ec 100644
@@ -2527,3 +2527,8 @@ module = {
   common = commands/i386/wrmsr.c;
   enable = x86;
 };
+
+module = {
+  name = memtools;
+  common = commands/memtools.c;
+};
grub-core/commands/memtools.c new file +49 -0
new file mode 100644
index 000000000000..6d5778f4a1b0
@@ -0,0 +1,157 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2021  IBM Corporation
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <grub/dl.h>
+#include <grub/misc.h>
+#include <grub/command.h>
+#include <grub/i18n.h>
+#include <grub/memory.h>
+#include <grub/mm.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+#ifdef MM_DEBUG
+
+static grub_err_t
+grub_cmd_lsmem (grub_command_t cmd __attribute__ ((unused)),
+		 int argc __attribute__ ((unused)),
+		 char **args __attribute__ ((unused)))
+
+{
+#ifndef GRUB_MACHINE_EMU
+  grub_mm_dump(0);
+#endif
+
+  return 0;
+}
+
+static grub_err_t
+grub_cmd_lsfreemem (grub_command_t cmd __attribute__ ((unused)),
+		    int argc __attribute__ ((unused)),
+		    char **args __attribute__ ((unused)))
+
+{

grub-core/commands/memtools.c line 59

Quoted 10 lines
grub-core/commands/memtools.c new file +10 -0
+#ifndef GRUB_MACHINE_EMU
+  grub_mm_dump_free();
+#endif
+
+  return 0;
+}
+
+
+#define BIG_ALLOC (64 * 1024 * 1024)
+#define SMALL_ALLOC 32

Where are these used? A search is coming up with nothing.

grub-core/commands/memtools.c line 64

Quoted 5 lines
grub-core/commands/memtools.c new file +5 -0
+
+static grub_err_t
+grub_cmd_stress_big_allocs (grub_command_t cmd __attribute__ ((unused)),
+			    int argc __attribute__ ((unused)),
+			    char **args __attribute__ ((unused)))

I'm thinking this would be valuable to be included in the functional tests. What do you think?

grub-core/commands/memtools.c line 70

Quoted 6 lines
grub-core/commands/memtools.c new file +6 -0
+{
+  int i, max_mb, blocks_alloced;
+  void *mem;
+  void **blocklist;
+
+  grub_printf ("Test 1: increasingly sized allocs to 1GB block\n");

Perhaps, "Test 1: Allocate and free up from 1MB to 1GB in 1MB increments."

grub-core/commands/memtools.c line 76

Quoted 6 lines
grub-core/commands/memtools.c new file +6 -0
+  for (i = 1; i < 1024; i++) {
+    grub_printf ("%d MB . ", i);
+    mem = grub_malloc (i * 1024 * 1024);
+    if (mem == NULL)
+      {
+	grub_printf ("failed\n");

"Failed to allocate a %dMB continuous block of memory.\n"

grub-core/commands/memtools.c line 83

Quoted 7 lines
grub-core/commands/memtools.c new file +7 -0
+	break;
+      }
+    else
+      grub_free (mem);
+
+    if (i % 10 == 0)
+      grub_printf ("\n");

So every 10Mb a newline is output. That's seems small, if you've got generally at least 80 char with terminal. Why not make it 64 and thus evenly divide 1024?

grub-core/commands/memtools.c line 88

Quoted 5 lines
grub-core/commands/memtools.c new file +5 -0
+  }
+
+  max_mb = i - 1;
+  grub_printf ("Max sized allocation we did was %d MB\n", max_mb);
+  

Line with just a space, should be empty.

grub-core/commands/memtools.c line 89

Quoted 1 line
grub-core/commands/memtools.c new file +1 -0
+  grub_printf ("Test 2: 1MB at a time, max 4GB\n");

"Test 2: Allocate total of 4GB of memory in 1MB chunks"

grub-core/commands/memtools.c line 96

Quoted 7 lines
grub-core/commands/memtools.c new file +7 -0
+  blocklist = grub_calloc (4096, sizeof (void *));
+  for (i = 0; i < 4096; i++)
+    {
+      blocklist[i] = grub_malloc (1024 * 1024);
+      if (!blocklist[i])
+	{
+	  grub_printf ("Ran out of memory at iteration %d\n", i);

"Failed to allocate 1MB memory chunk on block %d of 4096"

grub-core/commands/memtools.c line 104

Quoted 8 lines
grub-core/commands/memtools.c new file +8 -0
+	  break;
+	}
+    }
+  blocks_alloced = i;
+  for (i = 0; i < blocks_alloced; i++)
+    grub_free (blocklist[i]);
+
+  grub_printf ("\nTest 3: 1MB aligned 900kB + 100kB\n");

"Test 3: Allocate total of 4Gb of memory in 1MB aligned 900kB + 100kB chunks"

grub-core/commands/memtools.c line 105

Quoted 1 line
grub-core/commands/memtools.c new file +1 -0
+  //grub_mm_debug=1;

Was this comment intended to be included? If so, why here?

Quoted 31 lines
grub-core/commands/memtools.c new file +31 -0
+  for (i = 0; i < 4096; i += 2)
+    {
+      blocklist[i] = grub_memalign (1024 * 1024, 900 * 1024);
+      if (!blocklist[i])
+	{
+	  grub_printf ("Failed big allocation, iteration %d\n", i);
+	  blocks_alloced = i;
+	  break;
+	}
+
+      blocklist[i + 1] = grub_malloc (100 * 1024);
+      if (!blocklist[i + 1])
+	{
+	  grub_printf ("Failed small allocation, iteration %d\n", i);
+	  blocks_alloced = i + 1;
+	  break;
+	}
+      grub_printf (".");
+    }
+  for (i = 0; i < blocks_alloced; i++)
+    grub_free (blocklist[i]);
+
+  grub_free (blocklist);
+
+  grub_errno = GRUB_ERR_NONE;
+  return GRUB_ERR_NONE;
+}
+
+static grub_command_t cmd_lsmem, cmd_lsfreemem, cmd_sba;
+
+#endif /* MM_DEBUG */

grub-core/commands/memtools.c line 146

Quoted 10 lines
grub-core/commands/memtools.c new file +10 -0
+
+GRUB_MOD_INIT(memtools)
+{
+#ifdef MM_DEBUG
+  cmd_lsmem = grub_register_command ("lsmem", grub_cmd_lsmem,
+				     0, N_("List free and allocated memory blocks."));
+  cmd_lsfreemem = grub_register_command ("lsfreemem", grub_cmd_lsfreemem,
+					 0, N_("List free memory blocks."));
+  cmd_sba = grub_register_command ("stress_big_allocs", grub_cmd_stress_big_allocs,
+  				   0, N_("Stress test large allocations."));

Leading space characters.

Quoted 38 lines
grub-core/commands/memtools.c new file +11 -0
+#endif
+}
+
+GRUB_MOD_FINI(memtools)
+{
+#ifdef MM_DEBUG
+  grub_unregister_command (cmd_lsmem);
+  grub_unregister_command (cmd_lsfreemem);
+  grub_unregister_command (cmd_sba);
+#endif
+}
grub-core/kern/mm.c +4 -0
index 835ed8a8f6f9..032c8f71aed2 100644
@@ -556,6 +556,8 @@ grub_mm_dump_free (void)
     {
       grub_mm_header_t p;
 
+      grub_printf ("Region %p (size %" PRIuGRUB_SIZE ")\n\n", r, r->size);
+
       /* Follow the free list.  */
       p = r->first;
       do
@@ -583,6 +585,8 @@ grub_mm_dump (unsigned lineno)
     {
       grub_mm_header_t p;
 
+      grub_printf ("Region %p (size %" PRIuGRUB_SIZE ")\n\n", r, r->size);
+
       for (p = (grub_mm_header_t) ALIGN_UP ((grub_addr_t) (r + 1),
 					    GRUB_MM_ALIGN);
 	   (grub_addr_t) p < (grub_addr_t) (r+1) + r->size;
include/grub/mm.h +0 -0
index 9c38dd3ca5d2..44fde7cb9033 100644

include/grub/mm.h void grub_mm_check_real (const char *file, int line); · line 50

Quoted 7 lines
include/grub/mm.h +2 -2
@@ -46,8 +46,8 @@ void grub_mm_check_real (const char *file, int line);
 /* Set this variable to 1 when you want to trace all memory function calls.  */
 extern int EXPORT_VAR(grub_mm_debug);
 
-void grub_mm_dump_free (void);
-void grub_mm_dump (unsigned lineno);
+void EXPORT_FUNC(grub_mm_dump_free) (void);
+void EXPORT_FUNC(grub_mm_dump) (unsigned lineno);

This makes me wonder if we could easily move grub_mm_dump and grub_mm_dump_free to memtools.c, and always have them built and lsmem and lsmemfree as well. IOW, lsmem would work without need to compile with --enable-mm-debug. If so, since its compiled as a module there's not an issue of it increasing the core size.

include/grub/mm.h void grub_mm_check_real (const char *file, int line); · line 53

Quoted 3 lines
include/grub/mm.h +0 -0
 #define grub_calloc(nmemb, size)	\
   grub_debug_calloc (GRUB_FILE, __LINE__, nmemb, size)

Glenn

#20 lore raw

Re: [PATCH 07/19] Add memtool module with memory allocation stress-test

In reply to Daniel Axtens
Quoted 12 lines · Daniel Axtens

When working on memory, it's nice to be able to test your work.

Add a memtest module. When compiled with --enable-mm-debug, it exposes 3 commands:

* lsmem - print all allocations and free space in all regions
* lsfreemem - print free space in all regions

* stress_big_allocs - stress test large allocations:
- how much memory can we allocate in one chunk?
- how many 1MB chunks can we allocate?
- check that gap-filling works with a 1MB aligned 900kB alloc + a

Quoted 8 lines
     100kB alloc.

---

I've put this as copyright IBM for now - hopefully we can conclude on whether we're still doing FSF copyright assignments?

It seems to me we should do it. In the worst case, I think, we can put IBM and FSF copyright into the file.

Quoted 46 lines
---
 grub-core/Makefile.core.def   |   5 ++
 grub-core/commands/memtools.c | 157 ++++++++++++++++++++++++++++++++++
 grub-core/kern/mm.c           |   4 +
 include/grub/mm.h             |   4 +-
 4 files changed, 168 insertions(+), 2 deletions(-)
 create mode 100644 grub-core/commands/memtools.c
grub-core/Makefile.core.def +5 -0
index 8022e1c0a794..0cc3a4a500ec 100644
@@ -2527,3 +2527,8 @@ module = {
   common = commands/i386/wrmsr.c;
   enable = x86;
 };
+
+module = {
+  name = memtools;
+  common = commands/memtools.c;
+};
grub-core/commands/memtools.c new file +19 -0
new file mode 100644
index 000000000000..6d5778f4a1b0
@@ -0,0 +1,157 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2021  IBM Corporation
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>

grub-core/commands/memtools.c line 29

Quoted 10 lines
grub-core/commands/memtools.c new file +10 -0
+#include <grub/dl.h>
+#include <grub/misc.h>
+#include <grub/command.h>
+#include <grub/i18n.h>
+#include <grub/memory.h>
+#include <grub/mm.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+#ifdef MM_DEBUG

Could we avoid this ifdefery and do everything in Makefile? Then non-debug builds will no contain even memtools stub module.

s/memtools/memdebug/?

grub-core/commands/memtools.c line 38

Quoted 8 lines
grub-core/commands/memtools.c new file +8 -0
+static grub_err_t
+grub_cmd_lsmem (grub_command_t cmd __attribute__ ((unused)),
+		 int argc __attribute__ ((unused)),
+		 char **args __attribute__ ((unused)))
+
+{
+#ifndef GRUB_MACHINE_EMU
+  grub_mm_dump(0);

Missing space between function name and "(". Please fix the same mistakes below.

grub-core/commands/memtools.c line 51

Quoted 13 lines
grub-core/commands/memtools.c new file +13 -0
+#endif
+
+  return 0;
+}
+
+static grub_err_t
+grub_cmd_lsfreemem (grub_command_t cmd __attribute__ ((unused)),
+		    int argc __attribute__ ((unused)),
+		    char **args __attribute__ ((unused)))
+
+{
+#ifndef GRUB_MACHINE_EMU
+  grub_mm_dump_free();

Ditto...

grub-core/commands/memtools.c line 41

Quoted 3 lines
grub-core/commands/memtools.c new file +3 -0
+#endif
+
+  return 0;

return GRUB_ERR_NONE

grub-core/commands/memtools.c line 58

Quoted 4 lines
grub-core/commands/memtools.c new file +4 -0
+}
+
+
+#define BIG_ALLOC (64 * 1024 * 1024)

s/BIG_ALLOC/STRESS_BIG_ALLOC/?

grub-core/commands/memtools.c line 59

Quoted 1 line
grub-core/commands/memtools.c new file +1 -0
+#define SMALL_ALLOC 32

s/SMALL_ALLOC/STRESS_SMALL_ALLOC/?

And could you define these constants immediately behind GRUB_MOD_LICENSE()?

Quoted 16 lines
grub-core/commands/memtools.c new file +16 -0
+static grub_err_t
+grub_cmd_stress_big_allocs (grub_command_t cmd __attribute__ ((unused)),
+			    int argc __attribute__ ((unused)),
+			    char **args __attribute__ ((unused)))
+{
+  int i, max_mb, blocks_alloced;
+  void *mem;
+  void **blocklist;
+
+  grub_printf ("Test 1: increasingly sized allocs to 1GB block\n");
+  for (i = 1; i < 1024; i++) {
+    grub_printf ("%d MB . ", i);
+    mem = grub_malloc (i * 1024 * 1024);
+    if (mem == NULL)
+      {
+	grub_printf ("failed\n");

grub-core/commands/memtools.c line 86

Quoted 10 lines
grub-core/commands/memtools.c new file +10 -0
+	break;
+      }
+    else
+      grub_free (mem);
+
+    if (i % 10 == 0)
+      grub_printf ("\n");
+  }
+
+  max_mb = i - 1;

I think i is not incremented when break is executed. So, max_mb value will be wrong.

grub-core/commands/memtools.c line 90

Quoted 4 lines
grub-core/commands/memtools.c new file +4 -0
+  grub_printf ("Max sized allocation we did was %d MB\n", max_mb);
+
+  grub_printf ("Test 2: 1MB at a time, max 4GB\n");
+  blocklist = grub_calloc (4096, sizeof (void *));

The blocklist can be NULL here.

grub-core/commands/memtools.c line 94

Quoted 4 lines
grub-core/commands/memtools.c new file +4 -0
+  for (i = 0; i < 4096; i++)
+    {
+      blocklist[i] = grub_malloc (1024 * 1024);
+      if (!blocklist[i])

if (blocklist[i] == NULL)

And please fix similar things below...

grub-core/commands/memtools.c line 97

Quoted 3 lines
grub-core/commands/memtools.c new file +3 -0
+	{
+	  grub_printf ("Ran out of memory at iteration %d\n", i);
+	  break;

Should not we print a dot after every 32 or 64 iterations?

grub-core/commands/memtools.c line 100

Quoted 3 lines
grub-core/commands/memtools.c new file +3 -0
+	}
+    }
+  blocks_alloced = i;

Again, i value can be wrong after break.

grub-core/commands/memtools.c line 100

Quoted 2 lines
grub-core/commands/memtools.c new file +2 -0
+  for (i = 0; i < blocks_alloced; i++)
+    grub_free (blocklist[i]);

I think each of these tests should have separate command assigned.

grub-core/commands/memtools.c line 105

Quoted 2 lines
grub-core/commands/memtools.c new file +2 -0
+  grub_printf ("\nTest 3: 1MB aligned 900kB + 100kB\n");
+  //grub_mm_debug=1;

Please drop this.

Quoted 8 lines
grub-core/commands/memtools.c new file +8 -0
+  for (i = 0; i < 4096; i += 2)
+    {
+      blocklist[i] = grub_memalign (1024 * 1024, 900 * 1024);
+      if (!blocklist[i])
+	{
+	  grub_printf ("Failed big allocation, iteration %d\n", i);
+	  blocks_alloced = i;
+	  break;

grub-core/commands/memtools.c line 123

Quoted 10 lines
grub-core/commands/memtools.c new file +10 -0
+	}
+
+      blocklist[i + 1] = grub_malloc (100 * 1024);
+      if (!blocklist[i + 1])
+	{
+	  grub_printf ("Failed small allocation, iteration %d\n", i);
+	  blocks_alloced = i + 1;
+	  break;
+	}
+      grub_printf (".");

Should not we do this every 32 or 64 iterations instead?

grub-core/commands/memtools.c line 132

Quoted 9 lines
grub-core/commands/memtools.c new file +9 -0
+    }
+  for (i = 0; i < blocks_alloced; i++)
+    grub_free (blocklist[i]);
+
+  grub_free (blocklist);
+
+  grub_errno = GRUB_ERR_NONE;
+  return GRUB_ERR_NONE;
+}

Daniel

#21 lore raw

Re: [PATCH 07/19] Add memtool module with memory allocation stress-test

Quoted 191 lines · Daniel Kiper
Quoted 12 lines · Daniel Axtens

When working on memory, it's nice to be able to test your work.

Add a memtest module. When compiled with --enable-mm-debug, it exposes 3 commands:

* lsmem - print all allocations and free space in all regions
* lsfreemem - print free space in all regions

* stress_big_allocs - stress test large allocations:
- how much memory can we allocate in one chunk?
- how many 1MB chunks can we allocate?
- check that gap-filling works with a 1MB aligned 900kB alloc + a

Quoted 8 lines
     100kB alloc.

---

I've put this as copyright IBM for now - hopefully we can conclude on whether we're still doing FSF copyright assignments?

It seems to me we should do it. In the worst case, I think, we can put IBM and FSF copyright into the file.

Quoted 46 lines
---
 grub-core/Makefile.core.def   |   5 ++
 grub-core/commands/memtools.c | 157 ++++++++++++++++++++++++++++++++++
 grub-core/kern/mm.c           |   4 +
 include/grub/mm.h             |   4 +-
 4 files changed, 168 insertions(+), 2 deletions(-)
 create mode 100644 grub-core/commands/memtools.c
grub-core/Makefile.core.def +5 -0
index 8022e1c0a794..0cc3a4a500ec 100644
@@ -2527,3 +2527,8 @@ module = {
   common = commands/i386/wrmsr.c;
   enable = x86;
 };
+
+module = {
+  name = memtools;
+  common = commands/memtools.c;
+};
grub-core/commands/memtools.c new file +19 -0
new file mode 100644
index 000000000000..6d5778f4a1b0
@@ -0,0 +1,157 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2021  IBM Corporation
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>

grub-core/commands/memtools.c line 29

Quoted 10 lines
grub-core/commands/memtools.c new file +10 -0
+#include <grub/dl.h>
+#include <grub/misc.h>
+#include <grub/command.h>
+#include <grub/i18n.h>
+#include <grub/memory.h>
+#include <grub/mm.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+#ifdef MM_DEBUG

Could we avoid this ifdefery and do everything in Makefile? Then non-debug builds will no contain even memtools stub module.

s/memtools/memdebug/?

grub-core/commands/memtools.c line 38

Quoted 8 lines
grub-core/commands/memtools.c new file +8 -0
+static grub_err_t
+grub_cmd_lsmem (grub_command_t cmd __attribute__ ((unused)),
+		 int argc __attribute__ ((unused)),
+		 char **args __attribute__ ((unused)))
+
+{
+#ifndef GRUB_MACHINE_EMU
+  grub_mm_dump(0);

Missing space between function name and "(". Please fix the same mistakes below.

grub-core/commands/memtools.c line 51

Quoted 13 lines
grub-core/commands/memtools.c new file +13 -0
+#endif
+
+  return 0;
+}
+
+static grub_err_t
+grub_cmd_lsfreemem (grub_command_t cmd __attribute__ ((unused)),
+		    int argc __attribute__ ((unused)),
+		    char **args __attribute__ ((unused)))
+
+{
+#ifndef GRUB_MACHINE_EMU
+  grub_mm_dump_free();

Ditto...

grub-core/commands/memtools.c line 41

Quoted 3 lines
grub-core/commands/memtools.c new file +3 -0
+#endif
+
+  return 0;

return GRUB_ERR_NONE

grub-core/commands/memtools.c line 58

Quoted 4 lines
grub-core/commands/memtools.c new file +4 -0
+}
+
+
+#define BIG_ALLOC (64 * 1024 * 1024)

s/BIG_ALLOC/STRESS_BIG_ALLOC/?

grub-core/commands/memtools.c line 59

Quoted 1 line
grub-core/commands/memtools.c new file +1 -0
+#define SMALL_ALLOC 32

s/SMALL_ALLOC/STRESS_SMALL_ALLOC/?

And could you define these constants immediately behind GRUB_MOD_LICENSE()?

Quoted 16 lines
grub-core/commands/memtools.c new file +16 -0
+static grub_err_t
+grub_cmd_stress_big_allocs (grub_command_t cmd __attribute__ ((unused)),
+			    int argc __attribute__ ((unused)),
+			    char **args __attribute__ ((unused)))
+{
+  int i, max_mb, blocks_alloced;
+  void *mem;
+  void **blocklist;
+
+  grub_printf ("Test 1: increasingly sized allocs to 1GB block\n");
+  for (i = 1; i < 1024; i++) {
+    grub_printf ("%d MB . ", i);
+    mem = grub_malloc (i * 1024 * 1024);
+    if (mem == NULL)
+      {
+	grub_printf ("failed\n");

grub-core/commands/memtools.c line 86

Quoted 10 lines
grub-core/commands/memtools.c new file +10 -0
+	break;
+      }
+    else
+      grub_free (mem);
+
+    if (i % 10 == 0)
+      grub_printf ("\n");
+  }
+
+  max_mb = i - 1;

I think i is not incremented when break is executed. So, max_mb value will be wrong.

grub-core/commands/memtools.c line 90

Quoted 4 lines
grub-core/commands/memtools.c new file +4 -0
+  grub_printf ("Max sized allocation we did was %d MB\n", max_mb);
+
+  grub_printf ("Test 2: 1MB at a time, max 4GB\n");
+  blocklist = grub_calloc (4096, sizeof (void *));

The blocklist can be NULL here.

grub-core/commands/memtools.c line 94

Quoted 4 lines
grub-core/commands/memtools.c new file +4 -0
+  for (i = 0; i < 4096; i++)
+    {
+      blocklist[i] = grub_malloc (1024 * 1024);
+      if (!blocklist[i])

if (blocklist[i] == NULL)

And please fix similar things below...

grub-core/commands/memtools.c line 97

Quoted 3 lines
grub-core/commands/memtools.c new file +3 -0
+	{
+	  grub_printf ("Ran out of memory at iteration %d\n", i);
+	  break;

Should not we print a dot after every 32 or 64 iterations?

grub-core/commands/memtools.c line 100

Quoted 3 lines
grub-core/commands/memtools.c new file +3 -0
+	}
+    }
+  blocks_alloced = i;

Again, i value can be wrong after break.

Quoted 4 lines

grub-core/commands/memtools.c line 100

Quoted 2 lines
grub-core/commands/memtools.c new file +2 -0
+  for (i = 0; i < blocks_alloced; i++)
+    grub_free (blocklist[i]);

I think each of these tests should have separate command assigned.

...and then everyone should get some parameters instead of being them hard coded. This should ease testing with various memory allocation sizes and number of iterations.

Daniel

#22 lore raw

PATCH 8/19 mm: Drop unused unloading of modules on OOM

In reply to Daniel Axtens

From: Patrick Steinhardt <[email protected]>

In grub_memalign (), there's a commented section which would allow for unloading of unneeded modules in case where there is not enough free memory available to satisfy a request. Given that this code is never compiled in, let's remove it together with grub_dl_unload_unneeded()

---
 grub-core/kern/dl.c | 20 --------------------
 grub-core/kern/mm.c |  8 --------
 include/grub/dl.h   |  1 -
 3 files changed, 29 deletions(-)
grub-core/kern/dl.c +0 -20
index 48f8a79073dd..a62dbeebbfe9 100644
@@ -803,23 +803,3 @@ grub_dl_unload (grub_dl_t mod)
   grub_free (mod);
   return 1;
 }
-
-/* Unload unneeded modules.  */
-void
-grub_dl_unload_unneeded (void)
-{
-  /* Because grub_dl_remove modifies the list of modules, this
-     implementation is tricky.  */
-  grub_dl_t p = grub_dl_head;
-
-  while (p)
-    {
-      if (grub_dl_unload (p))
-	{
-	  p = grub_dl_head;
-	  continue;
-	}
-
-      p = p->next;
-    }
-}
grub-core/kern/mm.c +0 -8
index 032c8f71aed2..6038c0d0cbb2 100644
@@ -377,14 +377,6 @@ grub_memalign (grub_size_t align, grub_size_t size)
       count++;
       goto again;
 
-#if 0
-    case 1:
-      /* Unload unneeded modules.  */
-      grub_dl_unload_unneeded ();
-      count++;
-      goto again;
-#endif
-
     default:
       break;
     }
include/grub/dl.h +0 -1
index b3753c9ca262..536717776367 100644
@@ -203,7 +203,6 @@ grub_dl_t EXPORT_FUNC(grub_dl_load) (const char *name);
 grub_dl_t grub_dl_load_core (void *addr, grub_size_t size);
 grub_dl_t EXPORT_FUNC(grub_dl_load_core_noinit) (void *addr, grub_size_t size);
 int EXPORT_FUNC(grub_dl_unload) (grub_dl_t mod);
-extern void grub_dl_unload_unneeded (void);
 extern int EXPORT_FUNC(grub_dl_ref) (grub_dl_t mod);
 extern int EXPORT_FUNC(grub_dl_unref) (grub_dl_t mod);
 extern int EXPORT_FUNC(grub_dl_ref_count) (grub_dl_t mod);
Signature · 1 line
2.30.2
#23 lore raw

Re: [PATCH 08/19] mm: Drop unused unloading of modules on OOM

Quoted 8 lines · Daniel Axtens

From: Patrick Steinhardt <[email protected]>

In grub_memalign (), there's a commented section which would allow for unloading of unneeded modules in case where there is not enough free memory available to satisfy a request. Given that this code is never compiled in, let's remove it together with grub_dl_unload_unneeded()

You should add your SOB here.

Otherwise Reviewed-by: Daniel Kiper <[email protected]>

Daniel

#24 lore raw

PATCH 9/19 mm: Allow dynamically requesting additional memory regions

In reply to Daniel Axtens

From: Patrick Steinhardt <[email protected]>

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.

[dja: add this to the documentation at the top of mm.c]
---
 grub-core/kern/mm.c | 13 +++++++++++++
 include/grub/mm.h   | 16 ++++++++++++++++
 2 files changed, 29 insertions(+)
grub-core/kern/mm.c +13 -0
index 6038c0d0cbb2..58d5b89e8860 100644
@@ -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;
     }
include/grub/mm.h +16 -0
index 44fde7cb9033..5d916809666c 100644
@@ -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);
Signature · 1 line
2.30.2
#25 lore raw

Re: [PATCH 09/19] mm: Allow dynamically requesting additional memory regions

Quoted 52 lines · Daniel Axtens

From: Patrick Steinhardt <[email protected]>

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.

[dja: add this to the documentation at the top of mm.c]
---
 grub-core/kern/mm.c | 13 +++++++++++++
 include/grub/mm.h   | 16 ++++++++++++++++
 2 files changed, 29 insertions(+)
grub-core/kern/mm.c +4 -0
index 6038c0d0cbb2..58d5b89e8860 100644
@@ -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

grub-core/kern/mm.c grub_memalign (grub_size_t align, grub_size_t size) · line 388

Quoted 8 lines
grub-core/kern/mm.c +5 -0
@@ -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)

if (grub_mm_add_region_fn != NULL && ...

If you fix this you can add Reviewed-by: Daniel Kiper <[email protected]>

grub-core/kern/mm.c grub_memalign (grub_size_t align, grub_size_t size) · line 395

Quoted 7 lines
grub-core/kern/mm.c +4 -0
+	goto again;
+
+      /* fallthrough */
+
     default:
       break;
     }

Daniel

#26 lore raw

PATCH 10/19 efi: mm: Always request a fixed number of pages on init

In reply to Daniel Axtens

From: Patrick Steinhardt <[email protected]>

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.

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.

---
 grub-core/kern/efi/mm.c | 35 +++--------------------------------
 1 file changed, 3 insertions(+), 32 deletions(-)
grub-core/kern/efi/mm.c +3 -32
index 9838fb2f50db..4d276bc87a4c 100644
@@ -38,9 +38,8 @@
    a multiplier of 4KB.  */
 #define MEMORY_MAP_SIZE	0x3000
 
-/* The minimum and maximum heap size for GRUB itself.  */
-#define MIN_HEAP_SIZE	0x100000
-#define MAX_HEAP_SIZE	(1600 * 0x100000)
+/* The default heap size for GRUB itself in bytes.  */
+#define DEFAULT_HEAP_SIZE	0x100000
 
 static void *finish_mmap_buf = 0;
 static grub_efi_uintn_t finish_mmap_size = 0;
@@ -478,23 +477,6 @@ filter_memory_map (grub_efi_memory_descriptor_t *memory_map,
   return filtered_desc;
 }
 
-/* Return the total number of pages.  */
-static grub_efi_uint64_t
-get_total_pages (grub_efi_memory_descriptor_t *memory_map,
-		 grub_efi_uintn_t desc_size,
-		 grub_efi_memory_descriptor_t *memory_map_end)
-{
-  grub_efi_memory_descriptor_t *desc;
-  grub_efi_uint64_t total = 0;
-
-  for (desc = memory_map;
-       desc < memory_map_end;
-       desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))
-    total += desc->num_pages;
-
-  return total;
-}
-
 /* Add memory regions.  */
 static void
 add_memory_regions (grub_efi_memory_descriptor_t *memory_map,
@@ -583,8 +565,6 @@ grub_efi_mm_init (void)
   grub_efi_memory_descriptor_t *filtered_memory_map_end;
   grub_efi_uintn_t map_size;
   grub_efi_uintn_t desc_size;
-  grub_efi_uint64_t total_pages;
-  grub_efi_uint64_t required_pages;
   int mm_status;
 
   /* Prepare a memory region to store two memory maps.  */
@@ -624,22 +604,13 @@ grub_efi_mm_init (void)
   filtered_memory_map_end = filter_memory_map (memory_map, filtered_memory_map,
 					       desc_size, memory_map_end);
 
-  /* By default, request a quarter of the available memory.  */
-  total_pages = get_total_pages (filtered_memory_map, desc_size,
-				 filtered_memory_map_end);
-  required_pages = (total_pages >> 2);
-  if (required_pages < BYTES_TO_PAGES (MIN_HEAP_SIZE))
-    required_pages = BYTES_TO_PAGES (MIN_HEAP_SIZE);
-  else if (required_pages > BYTES_TO_PAGES (MAX_HEAP_SIZE))
-    required_pages = BYTES_TO_PAGES (MAX_HEAP_SIZE);
-
   /* Sort the filtered descriptors, so that GRUB can allocate pages
      from smaller regions.  */
   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, required_pages);
+		      filtered_memory_map_end, BYTES_TO_PAGES (DEFAULT_HEAP_SIZE));
 
 #if 0
   /* For debug.  */
Signature · 1 line
2.30.2
#27 lore raw

Re: [PATCH 10/19] efi: mm: Always request a fixed number of pages on init

Quoted 18 lines · Daniel Axtens

From: Patrick Steinhardt <[email protected]>

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.

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.

Quoted 1 line

Please add your SOB here.

Otherwise Reviewed-by: Daniel Kiper <[email protected]>

Daniel

#28 lore raw

PATCH 11/19 efi: mm: Extract function to add memory regions

In reply to Daniel Axtens

From: Patrick Steinhardt <[email protected]>

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.

---
 grub-core/kern/efi/mm.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)
grub-core/kern/efi/mm.c +16 -7
index 4d276bc87a4c..cfc6a67fc0cc 100644
@@ -504,7 +504,7 @@ 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),
@@ -556,8 +556,8 @@ 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;
@@ -570,7 +570,7 @@ grub_efi_mm_init (void)
   /* 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;
@@ -588,14 +588,14 @@ 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);
 
@@ -610,7 +610,7 @@ 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, BYTES_TO_PAGES (DEFAULT_HEAP_SIZE));
+		      filtered_memory_map_end, BYTES_TO_PAGES (required_bytes));
 
 #if 0
   /* For debug.  */
@@ -628,6 +628,15 @@ 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 ("%s", grub_errmsg);
 }
 
 #if defined (__aarch64__) || defined (__arm__) || defined (__riscv)
Signature · 1 line
2.30.2
#29 lore raw

Re: [PATCH 11/19] efi: mm: Extract function to add memory regions

Quoted 36 lines · On Tue, 12 Oct 2021 18:30:00 +1100 Daniel Axtens <[email protected]> wrote:

From: Patrick Steinhardt <[email protected]>

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.

---
 grub-core/kern/efi/mm.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)
grub-core/kern/efi/mm.c +3 -3
index 4d276bc87a4c..cfc6a67fc0cc 100644
@@ -504,7 +504,7 @@ 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),
@@ -556,8 +556,8 @@ 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) · line 573

Quoted 5 lines
grub-core/kern/efi/mm.c +1 -1
@@ -570,7 +570,7 @@ grub_efi_mm_init (void)
   /* 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");

Something more descriptive would be nice, may be even "cannot allocate memory for memory map"

grub-core/kern/efi/mm.c grub_efi_mm_init (void) · line 591

Quoted 9 lines
grub-core/kern/efi/mm.c +1 -1
   /* Obtain descriptors for available memory.  */
   map_size = MEMORY_MAP_SIZE;
@@ -588,14 +588,14 @@ 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");

Ditto. And maybe nice to have it slightly different, perhaps "cannot re-allocate memory map"

grub-core/kern/efi/mm.c grub_efi_mm_init (void) · line 598

Quoted 8 lines
grub-core/kern/efi/mm.c +1 -1
       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");

What are the range of values that mm_status could be? Would it be useful to include the status code in the error message? IOW, could a failure here be affected by a configuration that the user could change to make this work? Also I like the message "failed to retrieve memory map (status=%d)".

grub-core/kern/efi/mm.c grub_efi_mm_init (void) · line 616

Quoted 12 lines
grub-core/kern/efi/mm.c +1 -1
   memory_map_end = NEXT_MEMORY_DESCRIPTOR (memory_map, map_size);
 
@@ -610,7 +610,7 @@ 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, BYTES_TO_PAGES (DEFAULT_HEAP_SIZE));
+		      filtered_memory_map_end, BYTES_TO_PAGES (required_bytes));
 
 #if 0
   /* For debug.  */

What about turning this on when MM_DEBUG is on? Seems like it could be a useful (would been to get rid of/change the grub_fatal calls).

Quoted 6 lines
grub-core/kern/efi/mm.c +2 -0
@@ -628,6 +628,15 @@ 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;

grub-core/kern/efi/mm.c grub_efi_mm_init (void) · line 642

Quoted 10 lines
grub-core/kern/efi/mm.c +7 -0
+}
+
+void
+grub_efi_mm_init (void)
+{
+  if (grub_efi_mm_add_regions (DEFAULT_HEAP_SIZE) != GRUB_ERR_NONE)
+    grub_fatal ("%s", grub_errmsg);
 }
 
 #if defined (__aarch64__) || defined (__arm__) || defined (__riscv)

Glenn

#30 lore raw

Re: [PATCH 11/19] efi: mm: Extract function to add memory regions

Quoted 8 lines

Review

Quoted 5 lines
   /* 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)
(quoted diff) +1 -1
-    grub_fatal ("cannot allocate memory");
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate memory");

Something more descriptive would be nice, may be even "cannot allocate memory for memory map"

Yes.

Quoted 5 lines
Quoted 5 lines
 
   /* Obtain descriptors for available memory.  */
   map_size = MEMORY_MAP_SIZE;
(quoted diff) +0 -0
@@ -588,14 +588,14 @@ grub_efi_mm_init (void)
 
Quoted 7 lines

grub-core/kern/efi/mm.c grub_efi_mm_init (void) · line 591

Quoted 4 lines
grub-core/kern/efi/mm.c +1 -1
       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");

Ditto. And maybe nice to have it slightly different, perhaps "cannot re-allocate memory map"

Yes.

Quoted 9 lines

grub-core/kern/efi/mm.c grub_efi_mm_init (void) · line 598

Quoted 8 lines
grub-core/kern/efi/mm.c +1 -1
       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");
Quoted 6 lines

What are the range of values that mm_status could be? Would it be useful to include the status code in the error message? IOW, could a failure here be affected by a configuration that the user could change to make this work? Also I like the message "failed to retrieve memory map (status=%d)".

The function says:
/* Get the memory map as defined in the EFI spec. Return 1 if successful,
return 0 if partial, or return -1 if an error occurs. */

So no, I don't think we could print anything more interesting without reworking the grub_efi_get_memory_map function.

Quoted 10 lines
Quoted 10 lines
grub-core/kern/efi/mm.c +1 -1
   memory_map_end = NEXT_MEMORY_DESCRIPTOR (memory_map, map_size);
 
@@ -610,7 +610,7 @@ 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, BYTES_TO_PAGES (DEFAULT_HEAP_SIZE));
+		      filtered_memory_map_end, BYTES_TO_PAGES (required_bytes));
 
Quoted 6 lines

grub-core/kern/efi/mm.c grub_efi_mm_init (void) · line 616

Quoted 2 lines
grub-core/kern/efi/mm.c +0 -0
 #if 0
   /* For debug.  */

What about turning this on when MM_DEBUG is on? Seems like it could be a useful (would been to get rid of/change the grub_fatal calls).

Perhaps, but I'm not sufficiently across EFI to be confident making that sort of change, and I'm not well set up to test EFI systems or evaluate the correctness or usefulness of any output. I think this would be better deferred to another patch from someone with more experience in the field.

Kind regards,
Daniel

#31 lore raw

Re: [PATCH 11/19] efi: mm: Extract function to add memory regions

In reply to Daniel Axtens
Quoted 36 lines · On Tue, 12 Oct 2021 18:30:00 +1100 Daniel Axtens <[email protected]> wrote:

From: Patrick Steinhardt <[email protected]>

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.

---
 grub-core/kern/efi/mm.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)
grub-core/kern/efi/mm.c +3 -3
index 4d276bc87a4c..cfc6a67fc0cc 100644
@@ -504,7 +504,7 @@ 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),
@@ -556,8 +556,8 @@ 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) · line 573

Quoted 5 lines
grub-core/kern/efi/mm.c +1 -1
@@ -570,7 +570,7 @@ grub_efi_mm_init (void)
   /* 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");

Something more descriptive would be nice, may be even "cannot allocate memory for memory map"

grub-core/kern/efi/mm.c grub_efi_mm_init (void) · line 591

Quoted 9 lines
grub-core/kern/efi/mm.c +1 -1
   /* Obtain descriptors for available memory.  */
   map_size = MEMORY_MAP_SIZE;
@@ -588,14 +588,14 @@ 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");

Ditto. And maybe nice to have it slightly different, perhaps "cannot re-allocate memory map"

grub-core/kern/efi/mm.c grub_efi_mm_init (void) · line 598

Quoted 8 lines
grub-core/kern/efi/mm.c +1 -1
       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");

What are the range of values that mm_status could be? Would it be useful to include the status code in the error message? IOW, could a failure here be affected by a configuration that the user could change to make this work? Also I like the message "failed to retrieve memory map (status=%d)".

grub-core/kern/efi/mm.c grub_efi_mm_init (void) · line 616

Quoted 12 lines
grub-core/kern/efi/mm.c +1 -1
   memory_map_end = NEXT_MEMORY_DESCRIPTOR (memory_map, map_size);
 
@@ -610,7 +610,7 @@ 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, BYTES_TO_PAGES (DEFAULT_HEAP_SIZE));
+		      filtered_memory_map_end, BYTES_TO_PAGES (required_bytes));
 
 #if 0
   /* For debug.  */

What about turning this on when MM_DEBUG is on? Seems like it could be a useful (would been to get rid of/change the grub_fatal calls).

Quoted 6 lines
grub-core/kern/efi/mm.c +2 -0
@@ -628,6 +628,15 @@ 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;

grub-core/kern/efi/mm.c grub_efi_mm_init (void) · line 642

Quoted 10 lines
grub-core/kern/efi/mm.c +7 -0
+}
+
+void
+grub_efi_mm_init (void)
+{
+  if (grub_efi_mm_add_regions (DEFAULT_HEAP_SIZE) != GRUB_ERR_NONE)
+    grub_fatal ("%s", grub_errmsg);
 }
 
 #if defined (__aarch64__) || defined (__arm__) || defined (__riscv)

Glenn

#32 lore raw

Re: [PATCH 11/19] efi: mm: Extract function to add memory regions

In reply to Daniel Axtens
Quoted 16 lines · Daniel Axtens

From: Patrick Steinhardt <[email protected]>

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.

---
 grub-core/kern/efi/mm.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)
grub-core/kern/efi/mm.c +0 -0
index 4d276bc87a4c..cfc6a67fc0cc 100644

grub-core/kern/efi/mm.c add_memory_regions (grub_efi_memory_descriptor_t *memory_map, · line 507

Quoted 5 lines
grub-core/kern/efi/mm.c +1 -1
@@ -504,7 +504,7 @@ 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);

Please drop this change.

...and if possible please take into account Glenn's comments...

Daniel

#33 lore raw

PATCH 12/19 efi: mm: Pass up errors from `add_memory_regions ()`

In reply to Daniel Axtens

From: Patrick Steinhardt <[email protected]>

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.

---
 grub-core/kern/efi/mm.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
grub-core/kern/efi/mm.c +13 -7
index cfc6a67fc0cc..ced3ee5e7537 100644
@@ -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,
+			    "cannot allocate conventional memory %p with %u pages",
+			    (void *) ((grub_addr_t) start), (unsigned) pages);
 
       grub_mm_init_region (addr, PAGES_TO_BYTES (pages));
 
@@ -518,7 +518,9 @@ 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, "too little memory");
+
+  return GRUB_ERR_NONE;
 }
 
 void
@@ -565,6 +567,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 +612,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.  */
Signature · 1 line
2.30.2
#34 lore raw

Re: [PATCH 12/19] efi: mm: Pass up errors from `add_memory_regions ()`

Quoted 33 lines · On Tue, 12 Oct 2021 18:30:01 +1100 Daniel Axtens <[email protected]> wrote:

From: Patrick Steinhardt <[email protected]>

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.

---
 grub-core/kern/efi/mm.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
grub-core/kern/efi/mm.c +1 -1
index cfc6a67fc0cc..ced3ee5e7537 100644
@@ -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,

grub-core/kern/efi/mm.c add_memory_regions (grub_efi_memory_descriptor_t *memory_map, · line 511

Quoted 9 lines
grub-core/kern/efi/mm.c +3 -3
@@ -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,
+			    "cannot allocate conventional memory %p with %u pages",
+			    (void *) ((grub_addr_t) start), (unsigned) pages);

I wonder if we shouldn't print a debug message here and try the next descriptor. Is it possible that grub_efi_allocate_pages_real fails for the specified range, but a range further on will succeed? The fact that it hasn't mattered so far seems to indicate that either this case is very rarely encountered or we should not continue on a failure for some reason.

grub-core/kern/efi/mm.c add_memory_regions (grub_efi_memory_descriptor_t *memory_map, · line 521

Quoted 9 lines
grub-core/kern/efi/mm.c +1 -1
       grub_mm_init_region (addr, PAGES_TO_BYTES (pages));
 
@@ -518,7 +518,9 @@ 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, "too little memory");

A more descriptive error might be nice here too, perhaps "insufficient memory, require %d more bytes"

Quoted 17 lines
grub-core/kern/efi/mm.c +3 -0
+
+  return GRUB_ERR_NONE;
 }
 
 void
@@ -565,6 +567,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 +612,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.  */

grub-core/kern/efi/mm.c grub_efi_mm_add_regions (grub_efi_uint64_t required_bytes) · line 622

Quoted 10 lines
grub-core/kern/efi/mm.c +5 -2
-  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.  */

Glenn

#35 lore raw

Re: [PATCH 12/19] efi: mm: Pass up errors from `add_memory_regions ()`

Quoted 44 lines · Glenn Washburn
Quoted 41 lines · On Tue, 12 Oct 2021 18:30:01 +1100 Daniel Axtens <[email protected]> wrote:

From: Patrick Steinhardt <[email protected]>

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.

---
 grub-core/kern/efi/mm.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
grub-core/kern/efi/mm.c +3 -4
index cfc6a67fc0cc..ced3ee5e7537 100644
@@ -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,
+			    "cannot allocate conventional memory %p with %u pages",
Quoted 8 lines

grub-core/kern/efi/mm.c add_memory_regions (grub_efi_memory_descriptor_t *memory_map, · line 511

Quoted 1 line
grub-core/kern/efi/mm.c +1 -0
+			    (void *) ((grub_addr_t) start), (unsigned) pages);

I wonder if we shouldn't print a debug message here and try the next descriptor. Is it possible that grub_efi_allocate_pages_real fails for the specified range, but a range further on will succeed? The fact that it hasn't mattered so far seems to indicate that either this case is very rarely encountered or we should not continue on a failure for some reason.

You allocate number of pages of a given UEFI memory region or less. So, the grub_efi_allocate_pages_real() may fail only due to bug somewhere. Then I do not think we should try next UEFI memory region.

Daniel

#36 lore raw

Re: [PATCH 12/19] efi: mm: Pass up errors from `add_memory_regions ()`

In reply to Daniel Axtens
Quoted 46 lines · Daniel Axtens

From: Patrick Steinhardt <[email protected]>

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.

---
 grub-core/kern/efi/mm.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
grub-core/kern/efi/mm.c +4 -4
index cfc6a67fc0cc..ced3ee5e7537 100644
@@ -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,
+			    "cannot allocate conventional memory %p with %u pages",
+			    (void *) ((grub_addr_t) start), (unsigned) pages);
 
       grub_mm_init_region (addr, PAGES_TO_BYTES (pages));
 

grub-core/kern/efi/mm.c add_memory_regions (grub_efi_memory_descriptor_t *memory_map, · line 521

Quoted 5 lines
grub-core/kern/efi/mm.c +1 -1
@@ -518,7 +518,9 @@ 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, "too little memory");

I agree with Glenn. The message is a bit imprecise here. Maybe we should make more clear that the UEFI allocation somehow failed. I think I would improve message above too.

Daniel

#37 lore raw

PATCH 13/19 efi: mm: Implement runtime addition of pages

In reply to Daniel Axtens

From: Patrick Steinhardt <[email protected]>

Adjust the interface of grub_efi_mm_add_regions () to take a set of 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 | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
grub-core/kern/efi/mm.c +11 -4
index ced3ee5e7537..f3d2e2b99830 100644
@@ -482,7 +482,8 @@ 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,
-		    grub_efi_uint64_t required_pages)
+		    grub_efi_uint64_t required_pages,
+		    unsigned int flags)
 {
   grub_efi_memory_descriptor_t *desc;
 
@@ -496,6 +497,10 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map,
 
       start = desc->physical_start;
       pages = desc->num_pages;
+
+      if (pages < required_pages && (flags & GRUB_MM_ADD_REGION_CONSECUTIVE))
+	continue;
+
       if (pages > required_pages)
 	{
 	  start += PAGES_TO_BYTES (pages - required_pages);
@@ -559,7 +564,7 @@ print_memory_map (grub_efi_memory_descriptor_t *memory_map,
 #endif
 
 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 int flags)
 {
   grub_efi_memory_descriptor_t *memory_map;
   grub_efi_memory_descriptor_t *memory_map_end;
@@ -614,7 +619,8 @@ grub_efi_mm_add_regions (grub_efi_uint64_t required_bytes)
   /* Allocate memory regions for GRUB's memory management.  */
   err = add_memory_regions (filtered_memory_map, desc_size,
 			    filtered_memory_map_end,
-			    BYTES_TO_PAGES (required_bytes));
+			    BYTES_TO_PAGES (required_bytes),
+			    flags);
   if (err != GRUB_ERR_NONE)
     return err;
 
@@ -641,8 +647,9 @@ grub_efi_mm_add_regions (grub_efi_uint64_t required_bytes)
 void
 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, 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)
Signature · 1 line
2.30.2
#38 lore raw

Re: [PATCH 13/19] efi: mm: Implement runtime addition of pages

Quoted 9 lines · Daniel Axtens

From: Patrick Steinhardt <[email protected]>

Adjust the interface of grub_efi_mm_add_regions () to take a set of 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.

Please add your SOB here.

Otherwise Reviewed-by: Daniel Kiper <[email protected]>

Daniel

#39 lore raw

PATCH 14/19 ieee1275: request memory with ibm, client-architecture-support

In reply to Daniel Axtens

On PowerVM, the first time we boot a Linux partition, we may only get 256MB of real memory area, even if the partition has more memory.

This isn't really enough. Fortunately, the Power Architecture Platform Reference (PAPR) defines a method we can call to ask for more memory. This is part of the broad and powerful ibm,client-architecture-support (CAS) method.

CAS can do an enormous amount of things on a PAPR platform: as well as asking for memory, you can set the supported processor level, the interrupt controller, hash vs radix mmu, and so on. We want to touch as little of this as possible because we don't want to step on the toes of the future OS.

If:

- we are running under what we think is PowerVM (compatible property of / begins with "IBM"), and

- the full amount of RMA is less than 512MB (as determined by the reg property of /memory)

then call CAS as follows: (refer to the Linux on Power Architecture Reference, LoPAR, which is public, at B.5.2.3):

- Use the "any" PVR value and supply 2 option vectors.

- Set option vector 1 (PowerPC Server Processor Architecture Level) to "ignore".

- Set option vector 2 with default or Linux-like options, including a min-rma-size of 512MB.

This will cause a CAS reboot and the partition will restart with 512MB of RMA. Grub will notice the 512MB and not call CAS again.

(A partition can be configured with only 256MB of memory, which would mean this request couldn't be satisfied, but PFW refuses to load with only 256MB of memory, so it's a bit moot. SLOF will run fine with 256MB, but we will never call CAS under qemu/SLOF because /compatible won't begin with "IBM".)

One of the first things Linux does while still running under OpenFirmware is to call CAS with a much fuller set of options (including asking for 512MB of memory). This includes a much more restrictive set of PVR values and processor support levels, and this will induce another reboot. On this reboot grub will again notice the higher RMA, and not call CAS. We will get to Linux, Linux will call CAS but because the values are now set for Linux this will not induce another CAS reboot and we will finally boot.

On all subsequent boots, everything will be configured with 512MB of RMA and all the settings Linux likes, so there will be no further CAS reboots.

(phyp is super sticky with the RMA size - it persists even on cold boots. So if you've ever booted Linux in a partition, you'll probably never have grub call CAS. It'll only ever fire the first time a partition loads grub, or if you deliberately lower the amount of memory your partition has below 512MB.)

---
 grub-core/kern/ieee1275/cmain.c  |   3 +
 grub-core/kern/ieee1275/init.c   | 140 +++++++++++++++++++++++++++++++
 include/grub/ieee1275/ieee1275.h |   6 ++
 3 files changed, 149 insertions(+)
grub-core/kern/ieee1275/cmain.c +3 -0
index 4442b6a83193..b707798ec3fb 100644
@@ -123,6 +123,9 @@ grub_ieee1275_find_options (void)
 	      break;
 	    }
 	}
+
+      if (grub_strncmp (tmp, "IBM,", 4) == 0)
+	grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_CAN_TRY_CAS_FOR_MORE_MEMORY);
     }
 
   if (is_smartfirmware)
grub-core/kern/ieee1275/init.c +140 -0

151 lines not on this page · raw message

include/grub/ieee1275/ieee1275.h +6 -0
index 6ce8605d6daf..1752c58e8ef6 100644
@@ -128,6 +128,12 @@ enum grub_ieee1275_flag
   GRUB_IEEE1275_FLAG_CURSORONOFF_ANSI_BROKEN,
 
   GRUB_IEEE1275_FLAG_RAW_DEVNAMES,
+
+  /* On PFW, the first time we boot a Linux partition, we may only get 256MB
+     of real memory area, even if the partition has more memory. Set this flag
+     if we think we're running under PFW. Then, if this flag is set, and the
+     RMA is only 256MB in size, try asking for more with CAS. */
+  GRUB_IEEE1275_FLAG_CAN_TRY_CAS_FOR_MORE_MEMORY,
 };
 
 extern int EXPORT_FUNC(grub_ieee1275_test_flag) (enum grub_ieee1275_flag flag);
Signature · 1 line
2.30.2
#40 lore raw

PATCH 15/19 ieee1275: drop len -= 1 quirk in heap_init

In reply to Daniel Axtens

This was apparently 'required by some firmware': commit dc9468500919 ("2007-02-12 Hollis Blanchard <[email protected]>").

It's not clear what firmware that was, and what platform from 14 years ago which exhibited the bug then is still both in use and buggy now.

It doesn't cause issues on qemu (mac99 or pseries) or under PFW for Power8.

I don't have access to old Mac hardware, but if anyone feels especially strongly we can put it under some feature flag. I really want to disable it under pseries because it will mess with region merging.

---
 grub-core/kern/ieee1275/init.c | 1 -
 1 file changed, 1 deletion(-)
grub-core/kern/ieee1275/init.c +0 -1
index 46b1aee732d3..bf2cd200893d 100644
@@ -166,7 +166,6 @@ heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
 	  addr = 0x180000;
 	}
     }
-  len -= 1; /* Required for some firmware.  */
 
   /* Never exceed HEAP_MAX_SIZE  */
   if (*total + len > HEAP_MAX_SIZE)
Signature · 1 line
2.30.2
#41 lore raw

PATCH 16/19 ieee1275: support runtime memory claiming

In reply to Daniel Axtens

On powerpc-ieee1275, we are running out of memory trying to verify anything. This is because:

- we have to load an entire file into memory to verify it. This is extremely difficult to change with appended signatures.
- We only have 32MB of heap.
- Distro kernels are now often around 30MB.

So we want to be able to claim more memory from OpenFirmware for our heap at runtime.

There are some complications:

- The grub mm code isn't the only thing that will make claims on memory from OpenFirmware:

* PFW/SLOF will have claimed some for their own use.

* The ieee1275 loader will try to find other bits of memory that we haven't claimed to place the kernel and initrd when we go to boot.

* Once we load Linux, it will also try to claim memory. It claims memory without any reference to /memory/available, it just starts at min(top of RMO, 768MB) and works down. So we need to avoid this area. See arch/powerpc/kernel/prom_init.c as of v5.11.

- The smallest amount of memory a ppc64 KVM guest can have is 256MB. It doesn't work with distro kernels but can work with custom kernels. We should maintain support for that. (ppc32 can boot with even less, and we shouldn't break that either.)

- Even if a VM has more memory, the memory OpenFirmware makes available as Real Memory Area can be restricted. Even with our CAS work, an LPAR on a PowerVM box is likely to have only 512MB available to OpenFirmware even if it has many gigabytes of memory allocated.

The current EFI approach is in flux. Previously, EFI systems would attempt to allocate 1/4th of the available memory, clamped to between 1M and 1600M. Now EFI seems to be moving towards a base allocation plus runtime allocations.

What should we do?

We don't know in advance how big the kernel and initrd are going to be, which makes figuring out how much memory we can take a bit tricky.

To figure out how much memory we should leave unused, I looked at:

- an Ubuntu 20.04.1 ppc64le pseries KVM guest:

    vmlinux: ~30MB
    initrd:  ~50MB

- a RHEL8.2 ppc64le pseries KVM guest:

    vmlinux: ~30MB
    initrd:  ~30MB

So to give us a little wriggle room, I think we want to leave at least 128MB for the loader to put vmlinux and initrd in memory and leave Linux with space to satisfy its early allocations.

Other space can now be allocated at runtime.

---
 docs/grub-dev.texi             |   7 +-
 grub-core/kern/ieee1275/init.c | 179 +++++++++++++++++++++++++++++----
 2 files changed, 165 insertions(+), 21 deletions(-)
docs/grub-dev.texi +5 -2
index fb2cc965ed80..d576d17e3be2 100644
@@ -1047,7 +1047,10 @@ space is limited to 4GiB. GRUB allocates pages from EFI for its heap, at most
 1.6 GiB.
 
 On i386-ieee1275 and powerpc-ieee1275 GRUB uses same stack as IEEE1275.
-It allocates at most 32MiB for its heap.
+
+On i386-ieee1275 and powerpc-ieee1275, GRUB will allocate 32MiB for its heap on
+startup. It may allocate more at runtime, as long as at least 128MiB remain free
+in OpenFirmware.
 
 On sparc64-ieee1275 stack is 256KiB and heap is 2MiB.
 
@@ -1075,7 +1078,7 @@ In short:
 @item i386-qemu               @tab 60 KiB  @tab < 4 GiB
 @item *-efi                   @tab ?       @tab < 1.6 GiB
 @item i386-ieee1275           @tab ?       @tab < 32 MiB
-@item powerpc-ieee1275        @tab ?       @tab < 32 MiB
+@item powerpc-ieee1275        @tab ?       @tab available memory - 128MiB
 @item sparc64-ieee1275        @tab 256KiB  @tab 2 MiB
 @item arm-uboot               @tab 256KiB  @tab 2 MiB
 @item mips(el)-qemu_mips      @tab 2MiB    @tab 253 MiB
grub-core/kern/ieee1275/init.c +160 -19

240 lines not on this page · raw message

Signature · 1 line
2.30.2
#42 lore raw

PATCH 17/19 print more debug info in mm

In reply to Daniel Axtens

This is handy for debugging - I'm including it in case anyone else hacking on this area finds it helpful.

---
 grub-core/kern/mm.c | 8 ++++++++
 1 file changed, 8 insertions(+)
grub-core/kern/mm.c +8 -0
index 58d5b89e8860..811df1ab5ebb 100644
@@ -135,6 +135,9 @@ grub_mm_init_region (void *addr, grub_size_t size)
   for (p = &grub_mm_base, q = *p; q; p = &(q->next), q = *p)
     {
       /* Does this region come _before_ an existing region? */
+      grub_printf ("Extending w/ before %p + %" PRIxGRUB_SIZE " + %" PRIxGRUB_SIZE " = %p ? %s\n",
+		 (grub_uint8_t *)addr, size, q->pre_size, (grub_uint8_t *)q,
+		 (grub_uint8_t *)addr + size + q->pre_size == (grub_uint8_t *) q ? "yes" : "no");
       if ((grub_uint8_t *) addr + size + q->pre_size == (grub_uint8_t *) q)
 	{
 	  r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN);
@@ -143,6 +146,7 @@ grub_mm_init_region (void *addr, grub_size_t size)
 
 	  if (r->pre_size >> GRUB_MM_ALIGN_LOG2)
 	    {
+	      grub_printf ("extending a region\n");
 	      h = (grub_mm_header_t) (r + 1);
 	      h->size = (r->pre_size >> GRUB_MM_ALIGN_LOG2);
 	      h->magic = GRUB_MM_ALLOC_MAGIC;
@@ -156,9 +160,13 @@ grub_mm_init_region (void *addr, grub_size_t size)
 	}
 
       /* Does this region come _after_ an existing region? */
+      grub_printf ("Extending w/ after %p + %" PRIxGRUB_SIZE " + %" PRIxGRUB_SIZE " + %" PRIxGRUB_SIZE " = %p ? %s\n",
+		 (grub_uint8_t *)q, sizeof(*q), q->size, q->post_size, (grub_uint8_t *)addr,
+		 (grub_uint8_t *)q + sizeof(*q) + q->size + q->post_size == (grub_uint8_t *) addr ? "yes" : "no");
       if ((grub_uint8_t *)q + sizeof(*q) + q->size + q->post_size ==
 	  (grub_uint8_t *) addr)
 	{
+	  grub_printf ("extending a region\n");
 	  h = (grub_mm_header_t) ((grub_uint8_t *)addr - q->post_size);
 	  h->size = (size + q->post_size) >> GRUB_MM_ALIGN_LOG2;
 	  h->magic = GRUB_MM_ALLOC_MAGIC;
Signature · 1 line
2.30.2
#43 lore raw

Re: [PATCH 17/19] [not for merge] print more debug info in mm

CC-ing Glenn...

Quoted 13 lines · Daniel Axtens

This is handy for debugging - I'm including it in case anyone else hacking on this area finds it helpful.

---
 grub-core/kern/mm.c | 8 ++++++++
 1 file changed, 8 insertions(+)
grub-core/kern/mm.c +0 -0
index 58d5b89e8860..811df1ab5ebb 100644

grub-core/kern/mm.c grub_mm_init_region (void *addr, grub_size_t size) · line 140

Quoted 6 lines
grub-core/kern/mm.c +3 -0
@@ -135,6 +135,9 @@ grub_mm_init_region (void *addr, grub_size_t size)
   for (p = &grub_mm_base, q = *p; q; p = &(q->next), q = *p)
     {
       /* Does this region come _before_ an existing region? */
+      grub_printf ("Extending w/ before %p + %" PRIxGRUB_SIZE " + %" PRIxGRUB_SIZE " = %p ? %s\n",
+		 (grub_uint8_t *)addr, size, q->pre_size, (grub_uint8_t *)q,
+		 (grub_uint8_t *)addr + size + q->pre_size == (grub_uint8_t *) q ? "yes" : "no");

I think this kind of messages can be useful. Same applies to patch #18. Though I would use grub_dprintf() instead which should be wrapped with #ifdef MM_DEBUG. However, we have to be very careful with printing any messages from mm and do not exeecec 255 chars message length. If we go above that limit then we will trigger dynamic allocation in grub_dprintf() from mm which may lead to a recursion...

Additionally, I think Glenn's patch allowing us to disable logging from certain subsystem would be useful here. Glenn, could you take a look at it once again?

Daniel

#44 lore raw

Re: [PATCH 17/19] [not for merge] print more debug info in mm

Quoted 30 lines · On Wed, 10 Nov 2021 14:47:07 +0100 Daniel Kiper <[email protected]> wrote:

CC-ing Glenn...

Quoted 13 lines · Daniel Axtens

This is handy for debugging - I'm including it in case anyone else hacking on this area finds it helpful.

---
 grub-core/kern/mm.c | 8 ++++++++
 1 file changed, 8 insertions(+)
grub-core/kern/mm.c +0 -0
index 58d5b89e8860..811df1ab5ebb 100644

grub-core/kern/mm.c grub_mm_init_region (void *addr, grub_size_t size) · line 140

Quoted 6 lines
grub-core/kern/mm.c +3 -0
@@ -135,6 +135,9 @@ grub_mm_init_region (void *addr, grub_size_t size)
   for (p = &grub_mm_base, q = *p; q; p = &(q->next), q = *p)
     {
       /* Does this region come _before_ an existing region? */
+      grub_printf ("Extending w/ before %p + %" PRIxGRUB_SIZE " + %" PRIxGRUB_SIZE " = %p ? %s\n",
+		 (grub_uint8_t *)addr, size, q->pre_size, (grub_uint8_t *)q,
+		 (grub_uint8_t *)addr + size + q->pre_size == (grub_uint8_t *) q ? "yes" : "no");

I think this kind of messages can be useful. Same applies to patch #18. Though I would use grub_dprintf() instead which should be wrapped with #ifdef MM_DEBUG. However, we have to be very careful with printing any messages from mm and do not exeecec 255 chars message length. If we go above that limit then we will trigger dynamic allocation in grub_dprintf() from mm which may lead to a recursion...

Quoted 3 lines

Additionally, I think Glenn's patch allowing us to disable logging from certain subsystem would be useful here. Glenn, could you take a look at it once again?

Yes, I'm planning on it. Its been low priority for me and this month is very busy fo me IRL. I don't think my patches should hold this up though (or would this cause way to much logs if one didn't want them?)

Glenn

#45 lore raw

Re: [PATCH 17/19] [not for merge] print more debug info in mm

Quoted 33 lines · Glenn Washburn
Quoted 30 lines · On Wed, 10 Nov 2021 14:47:07 +0100 Daniel Kiper <[email protected]> wrote:

CC-ing Glenn...

Quoted 13 lines · Daniel Axtens

This is handy for debugging - I'm including it in case anyone else hacking on this area finds it helpful.

---
 grub-core/kern/mm.c | 8 ++++++++
 1 file changed, 8 insertions(+)
grub-core/kern/mm.c +0 -0
index 58d5b89e8860..811df1ab5ebb 100644

grub-core/kern/mm.c grub_mm_init_region (void *addr, grub_size_t size) · line 140

Quoted 6 lines
grub-core/kern/mm.c +3 -0
@@ -135,6 +135,9 @@ grub_mm_init_region (void *addr, grub_size_t size)
   for (p = &grub_mm_base, q = *p; q; p = &(q->next), q = *p)
     {
       /* Does this region come _before_ an existing region? */
+      grub_printf ("Extending w/ before %p + %" PRIxGRUB_SIZE " + %" PRIxGRUB_SIZE " = %p ? %s\n",
+		 (grub_uint8_t *)addr, size, q->pre_size, (grub_uint8_t *)q,
+		 (grub_uint8_t *)addr + size + q->pre_size == (grub_uint8_t *) q ? "yes" : "no");

I think this kind of messages can be useful. Same applies to patch #18. Though I would use grub_dprintf() instead which should be wrapped with #ifdef MM_DEBUG. However, we have to be very careful with printing any messages from mm and do not exeecec 255 chars message length. If we go above that limit then we will trigger dynamic allocation in grub_dprintf() from mm which may lead to a recursion...

Quoted 5 lines
Quoted 3 lines

Additionally, I think Glenn's patch allowing us to disable logging from certain subsystem would be useful here. Glenn, could you take a look at it once again?

Yes, I'm planning on it. Its been low priority for me and this month is

Cool! Thanks!

Quoted 2 lines

very busy fo me IRL. I don't think my patches should hold this up though (or would this cause way to much logs if one didn't want them?)

No rush...

Daniel

#46 lore raw

PATCH 18/19 ieee1275 debugging info

In reply to Daniel Axtens

This is also handy for debugging.

---
 grub-core/kern/ieee1275/init.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
grub-core/kern/ieee1275/init.c +10 -1
index 0fb7bae280df..f70cb134178b 100644
@@ -243,6 +243,7 @@ regions_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
    */
   if (rmo_top > RUNTIME_MIN_SPACE)
     {
+      grub_printf ("was %llx - %llx, ", addr, addr + len);
       linux_rmo_save = grub_min (0x30000000, rmo_top) - RUNTIME_MIN_SPACE;
       if ((addr < linux_rmo_save) && ((addr + len) > linux_rmo_save))
 	len = linux_rmo_save - addr;
@@ -253,6 +254,7 @@ regions_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
 	  addr = 0x30000000;
 	  len -= RUNTIME_MIN_SPACE;
         }
+      grub_printf ("now %llx - %llx (vs %x)\n", addr, addr + len, linux_rmo_save);
     }
 
   if (flags & GRUB_MM_ADD_REGION_CONSECUTIVE)
@@ -315,7 +317,12 @@ static grub_err_t grub_ieee1275_mm_add_region (grub_size_t size, unsigned int fl
       /* Ensure we leave enough space to boot
 	 (be careful of underflow here) */
       if (free_memory <= RUNTIME_MIN_SPACE)
-	return GRUB_ERR_OUT_OF_MEMORY;
+	{
+	  grub_printf ("Giving up, already at/over limit\n");
+	  return GRUB_ERR_OUT_OF_MEMORY;
+	}
+
+      grub_printf ("ieee1275: free = 0x%x, allocated = 0x%x\n", free_memory, allocated_memory);
 
       if (size > free_memory - RUNTIME_MIN_SPACE)
        return GRUB_ERR_OUT_OF_MEMORY;
@@ -324,8 +331,10 @@ static grub_err_t grub_ieee1275_mm_add_region (grub_size_t size, unsigned int fl
 	  total = grub_max(ALIGN_UP(size, 1024 * 1024) + 1024 * 1024, 32 * 1024 * 1024);
 	  total = grub_min(free_memory - RUNTIME_MIN_SPACE, total);
 	}
+      grub_printf ("ieee1275: looking for %x bytes of memory (%x requested)\n", total, size);
 
       grub_machine_mmap_iterate (region_claim, &total);
+      grub_printf ("ieee1275: get memory from fw %s\n", total == 0 ? "succeeded" : "failed");
     }
   else
     grub_machine_mmap_iterate (heap_init, &total);
Signature · 1 line
2.30.2
#47 lore raw

PATCH 19/19 RFC: Ignore REGION_CONSECUTIVE

In reply to Daniel Axtens

Looking into the region merging stuff, I started to wonder if we actually want to just try adding as much memory as we can (up to the limit requested), even if we cannot satisfy the full allocation (i.e. we are not CONSECUTIVE).

On powerpc-ieee1275 this actually allows me to satisfy bigger allocations:
while the FW cannot necessarily satisfy e.g. a 128MB allocation, it can satisfy e.g. a 32MB allocation, and that might merge with a region that's 100MB, and then we can satisfy our 128MB allocation.

The way the call out to firmware operates in mm.c is that it preceeds a 'goto again;' - it kicks off a whole new round of attempting to satisfy allocation. mm.c doesn't fill the allocation only from the new memory from firmware.

Perhaps an even better approach would be for the mm.c code to try going to firmware twice - once for the precise size of the allocation (rounded up etc) and then if that doesn't succeed go back and ask for 'anything you can give me'?

---
 grub-core/kern/ieee1275/init.c | 37 +++++++++-------------------------
 1 file changed, 10 insertions(+), 27 deletions(-)
grub-core/kern/ieee1275/init.c +10 -27
index f70cb134178b..87c1ab997138 100644
@@ -186,7 +186,7 @@ count_free (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
 
 static int
 regions_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
-	      unsigned int flags, void *data)
+	       void *data)
 {
   grub_uint32_t total = *(grub_uint32_t *)data;
   grub_uint32_t linux_rmo_save;
@@ -257,13 +257,6 @@ regions_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
       grub_printf ("now %llx - %llx (vs %x)\n", addr, addr + len, linux_rmo_save);
     }
 
-  if (flags & GRUB_MM_ADD_REGION_CONSECUTIVE)
-    {
-      // only continue if we can satisfy the entire allocation
-      if (len < total)
-	return 0;
-    }
-
   if (len > total)
     len = total;
 
@@ -287,21 +280,6 @@ regions_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
   return 0;
 }
 
-static int
-heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
-	   void *data)
-{
-  return regions_claim(addr, len, type, GRUB_MM_ADD_REGION_NONE, data);
-}
-
-static int
-region_claim (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
-	   void *data)
-{
-  return regions_claim(addr, len, type, GRUB_MM_ADD_REGION_CONSECUTIVE, data);
-}
-
-
 static grub_err_t grub_ieee1275_mm_add_region (grub_size_t size, unsigned int flags)
 {
   grub_uint32_t total = size;
@@ -325,7 +303,10 @@ static grub_err_t grub_ieee1275_mm_add_region (grub_size_t size, unsigned int fl
       grub_printf ("ieee1275: free = 0x%x, allocated = 0x%x\n", free_memory, allocated_memory);
 
       if (size > free_memory - RUNTIME_MIN_SPACE)
-       return GRUB_ERR_OUT_OF_MEMORY;
+        {
+	  // hope region extension saves us
+	  total = free_memory - RUNTIME_MIN_SPACE;
+	}
       else
 	{
 	  total = grub_max(ALIGN_UP(size, 1024 * 1024) + 1024 * 1024, 32 * 1024 * 1024);
@@ -333,13 +314,15 @@ static grub_err_t grub_ieee1275_mm_add_region (grub_size_t size, unsigned int fl
 	}
       grub_printf ("ieee1275: looking for %x bytes of memory (%x requested)\n", total, size);
 
-      grub_machine_mmap_iterate (region_claim, &total);
+      grub_machine_mmap_iterate (regions_claim, &total);
       grub_printf ("ieee1275: get memory from fw %s\n", total == 0 ? "succeeded" : "failed");
     }
   else
-    grub_machine_mmap_iterate (heap_init, &total);
+    grub_machine_mmap_iterate (regions_claim, &total);
 
-  if (total == 0)
+  // lie a little: if we allocated _anything_ give it another go
+  // because region merging might have saved the day
+  if (total != size)
     return GRUB_ERR_NONE;
   else
     return GRUB_ERR_OUT_OF_MEMORY;
Signature · 1 line
2.30.2
#48 lore raw

Re: [PATCH 19/19] RFC: Ignore REGION_CONSECUTIVE

Quoted 9 lines · Daniel Axtens

Looking into the region merging stuff, I started to wonder if we actually want to just try adding as much memory as we can (up to the limit requested), even if we cannot satisfy the full allocation (i.e. we are not CONSECUTIVE).

On powerpc-ieee1275 this actually allows me to satisfy bigger allocations:
while the FW cannot necessarily satisfy e.g. a 128MB allocation, it can satisfy e.g. a 32MB allocation, and that might merge with a region that's 100MB, and then we can satisfy our 128MB allocation.

Quoted 8 lines

The way the call out to firmware operates in mm.c is that it preceeds a 'goto again;' - it kicks off a whole new round of attempting to satisfy allocation. mm.c doesn't fill the allocation only from the new memory from firmware.

Perhaps an even better approach would be for the mm.c code to try going to firmware twice - once for the precise size of the allocation (rounded up etc) and then if that doesn't succeed go back and ask for 'anything you can give me'?

I think this is good idea. Though maybe we should allow platforms to choose mode of operation, i.e. try to allocate one memory region with precise size first and if this does not work then fulfill my request returning memory to the GRUB mm in chunks or vice versa or maybe some platforms would want to use only one allocation strategy...

Daniel