Dynamic allocation of memory regions and IBM vTPM v2
v2 cover Dynamic allocation of memory regions and IBM vTPM v2
Hi all,
This is, at long last, an updated version of my series extending Patrick's dynamic memory regions to ieee1275.
Noteworthy changes:
- reworked debug prints as grub_dprintfs. Folded the ieee1275 ones into the ieee1275 patches.
- reworked the ieee1275 runtime memory claiming to be more resilient and better documented.
- fixed comment style and hopefully addressed all other change requests.
- grub will now try asking for contiguous memory and then, if that fails, for discontiguous memory - in case region merging with the discontiguous memory is sufficient to allow the eventual allocation to succeed.
- The ieee1275 code agressively rounds up the size of the region for a dynamic allocation - it will now retry with a more precise size if the larger allocation fails.
The memtool module is included as an RFC only and will require more work, as discussed in the patch.
I've also included Stefan's vTPM patch - Stefan kindly tested that his patch worked with my memory rework series. I have added his tested-by to relevant Power-specific patches only.
Kind regards,
Daniel
Daniel Axtens (8):
grub-shell: pseries: don't pass fw_opt to qemu
mm: assert that we preserve header vs region alignment
mm: when adding a region, merge with region after as well as before
mm: debug support for region operations
ieee1275: request memory with ibm,client-architecture-support
ieee1275: drop len -= 1 quirk in heap_init
ieee1275: support runtime memory claiming
[RFC] Add memtool module with memory allocation stress-test
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
Stefan Berger (1):
ibmvtpm: Add support for trusted boot using a vTPM 2.0
PATCH v2 1/15 grub-shell: only pass SeaBIOS fw_opt in for x86 BIOS platforms
This breaks the tests on pseries - just restrict it to x86 platforms that don't specify a BIOS.
---
v2: Thanks Daniel K and Glenn for feedback.
--- tests/util/grub-shell.in | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
tests/util/grub-shell.in +5 -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" + # + # SeaBIOS is used for i386, except on EFI. + if [ ${grub_modinfo_target_cpu} == 'i386' ] && [ ${grub_modinfo_platform} != 'efi' ]; 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.32.0
Re: [PATCH v2 01/15] grub-shell: only pass SeaBIOS fw_opt in for x86 BIOS platforms
Quoted 4 lines · Daniel Axtens
This breaks the tests on pseries - just restrict it to x86 platforms that don't specify a BIOS.
Signed-off-by Daniel Axtens <[email protected]>
Daniel
PATCH v2 2/15 mm: assert that we preserve header vs region alignment
In reply to Daniel Axtensgrub_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.
---
v2: Thanks Daniel K for feedback.
--- include/grub/mm_private.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
include/grub/mm_private.h +14 -0
@@ -20,6 +20,7 @@#define GRUB_MM_PRIVATE_H 1#include <grub/mm.h> +#include <grub/misc.h> /* For context, see kern/mm.c */
@@ -89,4 +90,17 @@ typedef struct grub_mm_regionextern 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 ); + +/* +* GRUB_MM_ALIGN is supposed to represent cell size, and a mm_header is +* supposed to be 1 cell. +*/ +COMPILE_TIME_ASSERT (sizeof (struct grub_mm_header ) ==GRUB_MM_ALIGN ); +} +#endif
Signature · 1 line
2.32.0
Re: [PATCH v2 02/15] mm: assert that we preserve header vs region alignment
Quoted 6 lines · Daniel Axtens
grub_mm_region_init()does:h = (grub_mm_header_t) (r + 1);where h is a
grub_mm_header_tand r is agrub_mm_region_t.
Quoted 8 lines
Cells are supposed to be
GRUB_MM_ALIGNaligned, but whilegrub_mm_dumpensures 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()toALIGN_UP(), require that the struct is explictly a multiple of the header size.Signed-off-by Daniel Axtens <[email protected]>
Daniel
PATCH v2 3/15 mm: when adding a region, merge with region after as well as before
In reply to Daniel AxtensOn 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.
---
v2: Thanks Daniel K for feedback.
--- grub-core/kern/mm.c | 123 +++++++++++++++++++++++--------------- include/grub/mm_private.h | 9 +++ 2 files changed, 85 insertions(+), 47 deletions(-)
grub-core/kern/mm.c +76 -47
@@ -130,53 +130,81 @@ grub_mm_init_region (void *addr, grub_size_t size)/* Attempt to merge this region with every existing region */ for (p = &grub_mm_base, q = *p; q; p = &(q->next), q = *p) -/* -* Is the new region immediately below an existing region? That -* is, is the address of the memory we're adding now (addr) + size -* of the memory we're adding (size) + the bytes we couldn't use -* at the start of the region we're considering (q->pre_size) -* equal to the address of q? In other words, does the memory -* looks like this? -* -* addr q -* |----size-----|-q->pre_size-|<q region>| -*/ -if ((grub_uint8_t *) addr + size + q->pre_size == (grub_uint8_t *) q) - { -/* -* Yes, we can merge the memory starting at addr into the -* existing region from below. Align up addr to GRUB_MM_ALIGN -* so that our new region has proper alignment. -*/ - r = (grub_mm_region_t )ALIGN_UP ((grub_addr_t ) addr,GRUB_MM_ALIGN ); -/* Copy the region data across */ - *r = *q; -/* Consider all the new size as pre-size */ - r->pre_size += size; - -/* -* If we have enough pre-size to create a block, create a -* block with it. Mark it as allocated and pass it to -* grub_free (), which will sort out getting it into the free -* list. -*/ -if (r->pre_size >>GRUB_MM_ALIGN_LOG2 ) - { - h = (grub_mm_header_t) (r +1 ); -/* block size is pre-size converted to cells */ - h->size = (r->pre_size >>GRUB_MM_ALIGN_LOG2 ); - h->magic =GRUB_MM_ALLOC_MAGIC ; -/* region size grows by block size converted back to bytes */ - r->size += h->size <<GRUB_MM_ALIGN_LOG2 ; -/* adjust pre_size to be accurate */ - r->pre_size &= (GRUB_MM_ALIGN -1 ); - *p = r; -grub_free (h +1 ); - } -/* Replace the old region with the new region */ - *p = r; -return ; - } + { +/* +* Is the new region immediately below an existing region? That +* is, is the address of the memory we're adding now (addr) + size +* of the memory we're adding (size) + the bytes we couldn't use +* at the start of the region we're considering (q->pre_size) +* equal to the address of q? In other words, does the memory +* looks like this? +* +* addr q +* |----size-----|-q->pre_size-|<q region>| +*/ +if ((grub_uint8_t *) addr + size + q->pre_size == (grub_uint8_t *) q) + { +/* +* Yes, we can merge the memory starting at addr into the +* existing region from below. Align up addr to GRUB_MM_ALIGN +* so that our new region has proper alignment. +*/ + r = (grub_mm_region_t )ALIGN_UP ((grub_addr_t ) addr,GRUB_MM_ALIGN ); +/* Copy the region data across */ + *r = *q; +/* Consider all the new size as pre-size */ + r->pre_size += size; + +/* +* If we have enough pre-size to create a block, create a +* block with it. Mark it as allocated and pass it to +* grub_free (), which will sort out getting it into the free +* list. +*/ +if (r->pre_size >>GRUB_MM_ALIGN_LOG2 ) + { + h = (grub_mm_header_t) (r +1 ); +/* block size is pre-size converted to cells */ + h->size = (r->pre_size >>GRUB_MM_ALIGN_LOG2 ); + h->magic =GRUB_MM_ALLOC_MAGIC ; +/* region size grows by block size converted back to bytes */ + r->size += h->size <<GRUB_MM_ALIGN_LOG2 ; +/* adjust pre_size to be accurate */ + r->pre_size &= (GRUB_MM_ALIGN -1 ); + *p = r; +grub_free (h +1 ); + } +/* Replace the old region with the new region */ + *p = r; +return ; + } + +/* +* Is the new region immediately above an existing region? That +* is: +* q addr +* |<q region>|-q->post_size-|----size-----| +*/ +if ((grub_uint8_t *)q +sizeof (*q) + q->size + q->post_size == + (grub_uint8_t *) addr) + { +/* +* Yes! Follow a similar pattern to above, but simpler. +* Our header starts at address - post_size, which should align us +* to a cell boundary. +*/ + h = (grub_mm_header_t) ((grub_uint8_t *)addr - q->post_size); +/* our size is the allocated size plus post_size, in cells */ + h->size = (size + q->post_size) >>GRUB_MM_ALIGN_LOG2 ; + h->magic =GRUB_MM_ALLOC_MAGIC ; +/* region size grows by block size converted back to bytes */ + q->size += h->size <<GRUB_MM_ALIGN_LOG2 ; +/* adjust new post_size to be accurate */ + 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 );
@@ -195,6 +223,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 +9 -0
@@ -81,8 +81,17 @@ 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 */ +char padding[3 *GRUB_CPU_SIZEOF_VOID_P ]; } *grub_mm_region_t;
Signature · 1 line
2.32.0
Re: [PATCH v2 03/15] mm: when adding a region, merge with region after as well as before
Quoted 13 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 ofGRUB_MM_ALIGN.
Quoted 6 lines
* 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.Tested-by Stefan Berger <[email protected]>
This tag should be added by Stefan in a reply to the email(s) send to the grub-devel. Though I will accept it this time. And it should be behind SOB.
Quoted 1 line
Signed-off-by Daniel Axtens <[email protected]>
But there some nits below...
Quoted 122 lines
---
v2: Thanks Daniel K for feedback.--- grub-core/kern/mm.c | 123 +++++++++++++++++++++++--------------- include/grub/mm_private.h | 9 +++ 2 files changed, 85 insertions(+), 47 deletions(-)grub-core/kern/mm.c +60
-47@@ -130,53 +130,81 @@ grub_mm_init_region (void *addr, grub_size_t size)/* Attempt to merge this region with every existing region */ for (p = &grub_mm_base, q = *p; q; p = &(q->next), q = *p) -/* -* Is the new region immediately below an existing region? That -* is, is the address of the memory we're adding now (addr) + size -* of the memory we're adding (size) + the bytes we couldn't use -* at the start of the region we're considering (q->pre_size) -* equal to the address of q? In other words, does the memory -* looks like this? -* -* addr q -* |----size-----|-q->pre_size-|<q region>| -*/ -if ((grub_uint8_t *) addr + size + q->pre_size == (grub_uint8_t *) q) - { -/* -* Yes, we can merge the memory starting at addr into the -* existing region from below. Align up addr to GRUB_MM_ALIGN -* so that our new region has proper alignment. -*/ - r = (grub_mm_region_t )ALIGN_UP ((grub_addr_t ) addr,GRUB_MM_ALIGN ); -/* Copy the region data across */ - *r = *q; -/* Consider all the new size as pre-size */ - r->pre_size += size; - -/* -* If we have enough pre-size to create a block, create a -* block with it. Mark it as allocated and pass it to -* grub_free (), which will sort out getting it into the free -* list. -*/ -if (r->pre_size >>GRUB_MM_ALIGN_LOG2 ) - { - h = (grub_mm_header_t) (r +1 ); -/* block size is pre-size converted to cells */ - h->size = (r->pre_size >>GRUB_MM_ALIGN_LOG2 ); - h->magic =GRUB_MM_ALLOC_MAGIC ; -/* region size grows by block size converted back to bytes */ - r->size += h->size <<GRUB_MM_ALIGN_LOG2 ; -/* adjust pre_size to be accurate */ - r->pre_size &= (GRUB_MM_ALIGN -1 ); - *p = r; -grub_free (h +1 ); - } -/* Replace the old region with the new region */ - *p = r; -return ; - } + { +/* +* Is the new region immediately below an existing region? That +* is, is the address of the memory we're adding now (addr) + size +* of the memory we're adding (size) + the bytes we couldn't use +* at the start of the region we're considering (q->pre_size) +* equal to the address of q? In other words, does the memory +* looks like this? +* +* addr q +* |----size-----|-q->pre_size-|<q region>| +*/ +if ((grub_uint8_t *) addr + size + q->pre_size == (grub_uint8_t *) q) + { +/* +* Yes, we can merge the memory starting at addr into the +* existing region from below. Align up addr to GRUB_MM_ALIGN +* so that our new region has proper alignment. +*/ + r = (grub_mm_region_t )ALIGN_UP ((grub_addr_t ) addr,GRUB_MM_ALIGN ); +/* Copy the region data across */ + *r = *q; +/* Consider all the new size as pre-size */ + r->pre_size += size; + +/* +* If we have enough pre-size to create a block, create a +* block with it. Mark it as allocated and pass it to +* grub_free (), which will sort out getting it into the free +* list. +*/ +if (r->pre_size >>GRUB_MM_ALIGN_LOG2 ) + { + h = (grub_mm_header_t) (r +1 ); +/* block size is pre-size converted to cells */ + h->size = (r->pre_size >>GRUB_MM_ALIGN_LOG2 ); + h->magic =GRUB_MM_ALLOC_MAGIC ; +/* region size grows by block size converted back to bytes */ + r->size += h->size <<GRUB_MM_ALIGN_LOG2 ; +/* adjust pre_size to be accurate */ + r->pre_size &= (GRUB_MM_ALIGN -1 ); + *p = r; +grub_free (h +1 ); + } +/* Replace the old region with the new region */ + *p = r; +return ; + } + +/* +* Is the new region immediately above an existing region? That +* is: +* q addr +* |<q region>|-q->post_size-|----size-----| +*/ +if ((grub_uint8_t *)q +sizeof (*q) + q->size + q->post_size == + (grub_uint8_t *) addr) + { +/* +* Yes! Follow a similar pattern to above, but simpler.
grub-core/kern/mm.c grub_mm_init_region (void *addr, grub_size_t size) · line 202
Quoted 10 lines
grub-core/kern/mm.c +10
-0+* Our header starts at address - post_size, which should align us +* to a cell boundary. +*/ + h = (grub_mm_header_t) ((grub_uint8_t *)addr - q->post_size); +/* our size is the allocated size plus post_size, in cells */ + h->size = (size + q->post_size) >>GRUB_MM_ALIGN_LOG2 ; + h->magic =GRUB_MM_ALLOC_MAGIC ; +/* region size grows by block size converted back to bytes */ + q->size += h->size <<GRUB_MM_ALIGN_LOG2 ; +/* adjust new post_size to be accurate */
Comments and code are incorrectly aligned...
Anyway, I can fix this before committing.
Daniel
PATCH v2 4/15 mm: debug support for region operations
In reply to Daniel AxtensThis is handy for debugging. Enable with set debug=regions.
--- grub-core/kern/mm.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)
grub-core/kern/mm.c +16 -3
@@ -115,9 +115,8 @@ grub_mm_init_region (void *addr, grub_size_t size)grub_mm_header_t h;grub_mm_region_t r, *p, q; -#if 0 -grub_printf ("Using memory for heap: start=%p, end=%p\n" , addr, addr + (unsigned int ) size); -#endif +grub_dprintf ("regions" ,"Using memory for heap: start=%p, end=%p\n" , + addr, (char *) addr + (unsigned int ) size);/* Exclude last 4K to avoid overflows. */ /* If addr + 0x1000 overflows then whole region is in excluded zone. */
@@ -142,8 +141,14 @@ grub_mm_init_region (void *addr, grub_size_t size)* addr q * |----size-----|-q->pre_size-|<q region>| */ +grub_dprintf ("regions" ,"Can we extend into region above?" +" %p + %" PRIxGRUB_SIZE" + %" PRIxGRUB_SIZE" ?=? %p\n" , + (grub_uint8_t *) addr, size, q->pre_size, (grub_uint8_t *) q);if ((grub_uint8_t *) addr + size + q->pre_size == (grub_uint8_t *) q) { +grub_dprintf ("regions" ,"Yes: extending a region: (%p -> %p) -> (%p -> %p)\n" , + q, (grub_uint8_t *) q +sizeof (*q) + q->size, + addr, (grub_uint8_t *) q +sizeof (*q) + q->size);/* * Yes, we can merge the memory starting at addr into the * existing region from below. Align up addr to GRUB_MM_ALIGN
@@ -185,9 +190,15 @@ grub_mm_init_region (void *addr, grub_size_t size)* q addr * |<q region>|-q->post_size-|----size-----| */ +grub_dprintf ("regions" ,"Can we extend into region below?" +" %p + %" PRIxGRUB_SIZE" + %" PRIxGRUB_SIZE" + %" PRIxGRUB_SIZE" ?=? %p\n" , + (grub_uint8_t *) q,sizeof (*q), q->size, q->post_size, (grub_uint8_t *) addr);if ((grub_uint8_t *)q +sizeof (*q) + q->size + q->post_size == (grub_uint8_t *) addr) { +grub_dprintf ("regions" ,"Yes: extending a region: (%p -> %p) -> (%p -> %p)\n" , + q, (grub_uint8_t *) q +sizeof (*q) + q->size, + q, (grub_uint8_t *) addr + size);/* * Yes! Follow a similar pattern to above, but simpler. * Our header starts at address - post_size, which should align us
@@ -206,6 +217,8 @@ grub_mm_init_region (void *addr, grub_size_t size) } } +grub_dprintf ("regions" ,"No: considering a new region at %p of size %" PRIxGRUB_SIZE"\n" , + addr, size);/* Allocate a region from the head. */ r = (grub_mm_region_t )ALIGN_UP ((grub_addr_t ) addr,GRUB_MM_ALIGN );
Signature · 1 line
2.32.0
Re: [PATCH v2 04/15] mm: debug support for region operations
Quoted 3 lines · Daniel Axtens
This is handy for debugging. Enable with
set debug=regions.Signed-off-by Daniel Axtens <[email protected]>
Daniel
PATCH v2 5/15 mm: Drop unused unloading of modules on OOM
In reply to Daniel AxtensFrom: 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
@@ -811,23 +811,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
@@ -437,14 +437,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
@@ -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.32.0
PATCH v2 6/15 mm: Allow dynamically requesting additional memory regions
In reply to Daniel AxtensFrom: 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.
If this fails, we try asking for n bytes, possibly spread across multiple regions, in hopes that region merging means that we end up with enough memory for things to work out.
[dja: add this to the documentation at the top of mm.c
v2: fallback to non-contiguous]
---
Daniel K: you had a Reviewed-by based on v1, but as the v2 changes are non-trivial I have not included it.
I currently default to a fallback to non-contiguous on all platforms: if anyone objects I'm happy to tweak it.
--- grub-core/kern/mm.c | 30 ++++++++++++++++++++++++++++++ include/grub/mm.h | 16 ++++++++++++++++ 2 files changed, 46 insertions(+)
grub-core/kern/mm.c +30 -0
@@ -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
@@ -437,6 +441,32 @@ grub_memalign (grub_size_t align, grub_size_t size) count++; goto again; +case 1 : +/* Request additional pages, contiguous */ + count++; + +if (grub_mm_add_region_fn !=NULL && +grub_mm_add_region_fn (size,GRUB_MM_ADD_REGION_CONSECUTIVE ) ==GRUB_ERR_NONE ) + goto again; + +/* fallthrough */ + +case 2 : +/* Request additional pages, anything at all */ + count++; + +if (grub_mm_add_region_fn !=NULL ) + { +/* +* Try again even if this fails, in case it was able to partially +* satisfy the request +*/ +grub_mm_add_region_fn (size,GRUB_MM_ADD_REGION_NONE ); + goto again; + } + +/* fallthrough */ +default :break ; }
include/grub/mm.h +16 -0
@@ -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.32.0
Re: [PATCH v2 06/15] mm: Allow dynamically requesting additional memory regions
Quoted 25 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
nbytes. If this succeeds, we retry searching for a valid memory region, which should now succeed.If this fails, we try asking for
nbytes, possibly spread across multiple regions, in hopes that region merging means that we end up with enough memory for things to work out.
Quoted 5 lines
Tested-by Stefan Berger <[email protected]>Signed-off-by Patrick Steinhardt <[email protected]>[dja: add this to the documentation at the top of
mm.c
v2: fallback to non-contiguous]Signed-off-by Daniel Axtens <[email protected]>
Daniel
PATCH v2 7/15 efi: mm: Always request a fixed number of pages on init
In reply to Daniel AxtensFrom: 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
@@ -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 0x100000static 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.32.0
PATCH v2 8/15 efi: mm: Extract function to add memory regions
In reply to Daniel AxtensFrom: 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 | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-)
grub-core/kern/efi/mm.c +15 -6
@@ -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 for memory map" );/* 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 for new memory map" ); 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 ,"error fetching memory map from EFI" ); 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.32.0
Re: [PATCH v2 08/15] efi: mm: Extract function to add memory regions
Quoted 8 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.
Signed-off-by Patrick Steinhardt <[email protected]>Signed-off-by Daniel Axtens <[email protected]>
Daniel
PATCH v2 9/15 efi: mm: Pass up errors from `add_memory_regions ()`
In reply to Daniel AxtensFrom: 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.
---
v2: Thank you Glenn and Daniel K for the feedback.
This and all EFI changes were not tested on real hardware.
--- grub-core/kern/efi/mm.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-)
grub-core/kern/efi/mm.c +15 -7
@@ -478,7 +478,7 @@ filter_memory_map (grub_efi_memory_descriptor_t *memory_map, }/* Add memory regions. */ -static void +static grub_err_t add_memory_regions (grub_efi_memory_descriptor_t *memory_map, grub_efi_uintn_t desc_size, grub_efi_memory_descriptor_t *memory_map_end,
@@ -506,9 +506,9 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map,GRUB_EFI_ALLOCATE_ADDRESS , GRUB_EFI_LOADER_CODE);if (! addr) -grub_fatal ("cannot allocate conventional memory %p with %u pages" , - (void *) ((grub_addr_t ) start), - (unsigned ) pages); +return grub_error (GRUB_ERR_OUT_OF_MEMORY , +"Memory starting at %p (%u pages) marked as free, but EFI would not allocate" , + (void *) ((grub_addr_t ) start), (unsigned ) pages);grub_mm_init_region (addr,PAGES_TO_BYTES (pages));
@@ -518,7 +518,11 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map, }if (required_pages >0 ) -grub_fatal ("too little memory" ); +return grub_error (GRUB_ERR_OUT_OF_MEMORY , +"could not allocate all requested memory: %" PRIuGRUB_UINT64_T" pages still required after iterating EFI memory map" , + required_pages); + +return GRUB_ERR_NONE ; }void
@@ -565,6 +569,7 @@ grub_efi_mm_add_regions (grub_efi_uint64_t required_bytes)grub_efi_memory_descriptor_t *filtered_memory_map_end;grub_efi_uintn_t map_size;grub_efi_uintn_t desc_size; +grub_err_t err;int mm_status;/* Prepare a memory region to store two memory maps. */
@@ -609,8 +614,11 @@ grub_efi_mm_add_regions (grub_efi_uint64_t required_bytes)sort_memory_map (filtered_memory_map, desc_size, filtered_memory_map_end);/* Allocate memory regions for GRUB's memory management. */ -add_memory_regions (filtered_memory_map, desc_size, - filtered_memory_map_end,BYTES_TO_PAGES (required_bytes)); + err =add_memory_regions (filtered_memory_map, desc_size, + filtered_memory_map_end, +BYTES_TO_PAGES (required_bytes)); +if (err !=GRUB_ERR_NONE ) +return err;#if 0 /* For debug. */
Signature · 1 line
2.32.0
Re: [PATCH v2 09/15] efi: mm: Pass up errors from `add_memory_regions ()`
Quoted 9 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.
Quoted 8 lines
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 callgrub_fatal ()in casegrub_efi_mm_add_regions ()returns an error.Signed-off-by Patrick Steinhardt <[email protected]>[dja: clarify error messages]Signed-off-by Daniel Axtens <[email protected]>
Daniel
PATCH v2 10/15 efi: mm: Implement runtime addition of pages
In reply to Daniel AxtensFrom: 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
@@ -482,7 +482,8 @@ static grub_err_tadd_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);
@@ -561,7 +566,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;
@@ -616,7 +621,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;
@@ -643,8 +649,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.32.0
PATCH v2 11/15 ieee1275: request memory with ibm, client-architecture-support
In reply to Daniel AxtensOn 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.)
---
v2: reformat
--- grub-core/kern/ieee1275/cmain.c | 3 + grub-core/kern/ieee1275/init.c | 144 +++++++++++++++++++++++++++++++ include/grub/ieee1275/ieee1275.h | 8 ++ 3 files changed, 155 insertions(+)
grub-core/kern/ieee1275/cmain.c +3 -0
@@ -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 +144 -0
155 lines not on this page · raw message
include/grub/ieee1275/ieee1275.h +8 -0
@@ -128,6 +128,14 @@ enum grub_ieee1275_flagGRUB_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.32.0
PATCH v2 12/15 ieee1275: drop len -= 1 quirk in heap_init
In reply to Daniel AxtensThis 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
@@ -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.32.0
PATCH v2 13/15 ieee1275: support runtime memory claiming
In reply to Daniel AxtensOn 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 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.
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: ~30MBSo 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.
Allow other space to be allocated at runtime.
---
v2: reformat, rework and explain better, add debug prints with
grub_dprintf
--- docs/grub-dev.texi | 7 +- grub-core/kern/ieee1275/init.c | 267 ++++++++++++++++++++++++++++++--- 2 files changed, 254 insertions(+), 20 deletions(-)
docs/grub-dev.texi +5 -2
@@ -1059,7 +1059,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.
@@ -1087,7 +1090,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 +249 -18
325 lines not on this page · raw message
Signature · 1 line
2.32.0
RFC PATCH v2 14/15 Add memtool module with memory allocation stress-test
In reply to Daniel AxtensWhen 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 haven't addressed most of the change requests yet. This will need lots of work to get to a mergable state, especially the ability to check MM_DEBUG in template files so that it can only be built if --enable-mm-debug is set, ('enable = mm_debug' or something) and that involves a lot of slow and painful build system hacking. Perhaps later on. But I have sorted out the copyright assignment so at least that is clear if anyone else wants to hack on it.
--- grub-core/Makefile.core.def | 5 ++ grub-core/commands/memtools.c | 155 ++++++++++++++++++++++++++++++++++ grub-core/kern/mm.c | 4 + 3 files changed, 164 insertions(+) create mode 100644 grub-core/commands/memtools.c
grub-core/Makefile.core.def +5 -0
@@ -2530,3 +2530,8 @@ module = { common = commands/i386/wrmsr.c; enable = x86; }; + +module = { + name = memtools; + common = commands/memtools.c; +};
grub-core/commands/memtools.c new file +155 -0
155 lines not on this page · raw message
grub-core/kern/mm.c +4 -0
@@ -657,6 +657,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
@@ -684,6 +686,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;
Signature · 1 line
2.32.0
PATCH v2 15/15 ibmvtpm: Add support for trusted boot using a vTPM 2.0
In reply to Daniel AxtensFrom: Stefan Berger <[email protected]>
Add support for trusted boot using a vTPM 2.0 on the IBM IEEE1275 PowerPC platform. With this patch grub now measures text and binary data into the TPM's PCRs 8 and 9 in the same way as the x86_64 platform does.
This patch requires Daniel Axtens's patches for claiming more memory.
For vTPM support to work on PowerVM, system driver levels 1010.30 or 1020.00 are required.
Note: Previous versions of firmware levels with the 2hash-ext-log API call have a bug that, once this API call is invoked, has the effect of disabling the vTPM driver under Linux causing an error message to be displayed in the Linux kernel log. Those users will have to update their machines to the firmware levels mentioned above.
--- docs/grub.texi | 3 +- grub-core/Makefile.core.def | 7 ++ grub-core/commands/ieee1275/ibmvtpm.c | 152 ++++++++++++++++++++++++++ include/grub/ieee1275/ieee1275.h | 3 + 4 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 grub-core/commands/ieee1275/ibmvtpm.c
docs/grub.texi +2 -1
@@ -6084,7 +6084,8 @@ tpm module is loaded. As such it is recommended that the tpm module be built into @file{core.img} in order to avoid a potential gap in measurement between @file{core.img} being loaded and the tpm module being loaded. -Measured boot is currently only supported on EFI platforms. +Measured boot is currently only supported on EFI and IBM IEEE1275 PowerPC +platforms. @node Lockdown @section Lockdown when booting on a secure setup
grub-core/Makefile.core.def +7 -0
@@ -1121,6 +1121,13 @@ module = { enable = powerpc_ieee1275; }; +module = { + name = tpm; + common = commands/tpm.c; + ieee1275 = commands/ieee1275/ibmvtpm.c; + enable = powerpc_ieee1275; +}; + module = { name = terminal; common = commands/terminal.c;
grub-core/commands/ieee1275/ibmvtpm.c new file +152 -0
152 lines not on this page · raw message
include/grub/ieee1275/ieee1275.h +3 -0
@@ -24,6 +24,9 @@#include <grub/types.h> #include <grub/machine/ieee1275.h> +#define GRUB_IEEE1275_CELL_FALSE ((grub_ieee1275_cell_t) 0) +#define GRUB_IEEE1275_CELL_TRUE ((grub_ieee1275_cell_t) -1) +struct grub_ieee1275_mem_region {unsigned int start;
Signature · 1 line
2.32.0
Quoted 10 lines · Daniel Axtens
Hi all,
This is, at long last, an updated version of my series extending Patrick's dynamic memory regions to ieee1275.
Noteworthy changes:
- reworked debug prints as
grub_dprintfs. Folded the ieee1275 ones into the ieee1275 patches.
Quoted 8 lines
- reworked the ieee1275 runtime memory claiming to be more resilient and better documented.
- fixed comment style and hopefully addressed all other change requests.
- grub will now try asking for contiguous memory and then, if that fails, for discontiguous memory - in case region merging with the discontiguous memory is sufficient to allow the eventual allocation to succeed.
Patrick, all mm and EFI code got my RB. Could you test it with your Argon changes? If these changes pass your tests I will merge them.
Daniel
Quoted 14 lines · Daniel Kiper
Quoted 13 lines · Daniel Axtens
Hi all,
This is, at long last, an updated version of my series extending Patrick's dynamic memory regions to ieee1275.
Noteworthy changes:
- reworked debug prints as
grub_dprintfs. Folded the ieee1275 ones into the ieee1275 patches.- reworked the ieee1275 runtime memory claiming to be more resilient and better documented.
Quoted 8 lines
Quoted 5 lines
- fixed comment style and hopefully addressed all other change requests.
- grub will now try asking for contiguous memory and then, if that fails, for discontiguous memory - in case region merging with the discontiguous memory is sufficient to allow the eventual allocation to succeed.
Patrick, all mm and EFI code got my RB. Could you test it with your Argon changes? If these changes pass your tests I will merge them.
Patrick, ping?
To be more precise, I am thinking about the patches up to #10.
Daniel
Quoted 21 lines · Daniel Kiper
Quoted 20 lines · Daniel Kiper
Quoted 18 lines · Daniel Axtens
Hi all,
This is, at long last, an updated version of my series extending Patrick's dynamic memory regions to ieee1275.
Noteworthy changes:
- reworked debug prints as
grub_dprintfs. Folded the ieee1275 ones into theieee1275 patches.- reworked the ieee1275 runtime memory claiming to be more resilient and better documented.
- fixed comment style and hopefully addressed all other change requests.
- grub will now try asking for contiguous memory and then, if that fails, for discontiguous memory - in case region merging with the discontiguous memory is sufficient to allow the eventual allocation to succeed.
Quoted 8 lines
Quoted 2 lines
Patrick, all mm and EFI code got my RB. Could you test it with your Argon changes? If these changes pass your tests I will merge them.
Patrick, ping?
To be more precise, I am thinking about the patches up to #10.
Daniel
Any way we can make progress with this series before it gets all forgotten about?
Stefan
Quoted 25 lines · Stefan Berger
Quoted 24 lines · Daniel Kiper
Quoted 22 lines · Daniel Kiper
Quoted 10 lines · Daniel Axtens
Hi all,
This is, at long last, an updated version of my series extending Patrick's dynamic memory regions to ieee1275.
Noteworthy changes:
- reworked debug prints as
grub_dprintfs. Folded the ieee1275 ones into theieee1275 patches.Quoted 8 lines
- reworked the ieee1275 runtime memory claiming to be more resilient and better documented.
- fixed comment style and hopefully addressed all other change requests.
- grub will now try asking for contiguous memory and then, if that fails, for discontiguous memory - in case region merging with the discontiguous memory is sufficient to allow the eventual allocation to succeed.
Patrick, all mm and EFI code got my RB. Could you test it with your Argon changes? If these changes pass your tests I will merge them.
Quoted 8 lines
Quoted 5 lines
Patrick, ping?
To be more precise, I am thinking about the patches up to #10.
Daniel
Any way we can make progress with this series before it gets all forgotten about?
It is not and it will not be forgotten. I am waiting for some allocator tests reports. When I get them I will merge allocator stuff and review the rest of the code from this patch series. Sadly folks who are going to test the code are busy with other stuff. Though I am pinging them...
Anyway, sorry for delay...
Daniel
Patrick Steinhardt
Tested-byQuoted 35 lines · Daniel Kiper
Quoted 25 lines · Stefan Berger
Quoted 24 lines · Daniel Kiper
Quoted 22 lines · Daniel Kiper
Quoted 10 lines · Daniel Axtens
Hi all,
This is, at long last, an updated version of my series extending Patrick's dynamic memory regions to ieee1275.
Noteworthy changes:
- reworked debug prints as
grub_dprintfs. Folded the ieee1275 ones into theieee1275 patches.Quoted 8 lines
- reworked the ieee1275 runtime memory claiming to be more resilient and better documented.
- fixed comment style and hopefully addressed all other change requests.
- grub will now try asking for contiguous memory and then, if that fails, for discontiguous memory - in case region merging with the discontiguous memory is sufficient to allow the eventual allocation to succeed.
Patrick, all mm and EFI code got my RB. Could you test it with your Argon changes? If these changes pass your tests I will merge them.
Quoted 8 lines
Quoted 5 lines
Patrick, ping?
To be more precise, I am thinking about the patches up to #10.
Daniel
Any way we can make progress with this series before it gets all forgotten about?
Quoted 8 lines
It is not and it will not be forgotten. I am waiting for some allocator tests reports. When I get them I will merge allocator stuff and review the rest of the code from this patch series. Sadly folks who are going to test the code are busy with other stuff. Though I am pinging them...
Anyway, sorry for delay...
Daniel
Sorry for the delay here, and thanks for the nudge. I've found a few quiet moments this morning to rebase my Argon2 patch series for LUKS2 on top of this series and found everything to work as expected. Decrypting a volume with a memory hardness of 2GB RAM worked just fine on an EFI x64 system. So this series is:
I'm going to revive my own patch series and send it in for review soonish. The nice thing of having waited so long is that Argon2 support has meanwhile landed in libgcrypt, so that should make it easier to integrate it into GRUB.
Thanks
Patrick
Huh! For some reason I missed this email... Sorry folks about that!
Quoted 45 lines · Patrick Steinhardt
Quoted 35 lines · Daniel Kiper
Quoted 25 lines · Stefan Berger
Quoted 24 lines · Daniel Kiper
Quoted 22 lines · Daniel Kiper
Quoted 10 lines · Daniel Axtens
Hi all,
This is, at long last, an updated version of my series extending Patrick's dynamic memory regions to ieee1275.
Noteworthy changes:
- reworked debug prints as
grub_dprintfs. Folded the ieee1275 ones into theieee1275 patches.Quoted 8 lines
- reworked the ieee1275 runtime memory claiming to be more resilient and better documented.
- fixed comment style and hopefully addressed all other change requests.
- grub will now try asking for contiguous memory and then, if that fails, for discontiguous memory - in case region merging with the discontiguous memory is sufficient to allow the eventual allocation to succeed.
Patrick, all mm and EFI code got my RB. Could you test it with your Argon changes? If these changes pass your tests I will merge them.
Quoted 8 lines
Quoted 5 lines
Patrick, ping?
To be more precise, I am thinking about the patches up to #10.
Daniel
Any way we can make progress with this series before it gets all forgotten about?
Quoted 8 lines
It is not and it will not be forgotten. I am waiting for some allocator tests reports. When I get them I will merge allocator stuff and review the rest of the code from this patch series. Sadly folks who are going to test the code are busy with other stuff. Though I am pinging them...
Anyway, sorry for delay...
Daniel
Quoted 7 lines
Sorry for the delay here, and thanks for the nudge. I've found a few quiet moments this morning to rebase my Argon2 patch series for LUKS2 on top of this series and found everything to work as expected. Decrypting a volume with a memory hardness of 2GB RAM worked just fine on an EFI x64 system. So this series is:
Tested-by Patrick Steinhardt <[email protected]>
Patrick, thanks a lot for doing tests!
Here I would like to thank Patrick, Stefan, Daniel, Glenn and other folks who were working on this feature! It is very important and long awaited improvement for the GRUB memory manager (everyone who was looking at it knows it is one of the most complicated parts of the GRUB). This patch set unblocks and makes possible work on Argon2, appended signature secure boot support, IBM vTPM, etc.
If I do not hear any objections by the end of this week I will be pushing into master v3 of this patch set [1] (Patrick, I hope you are OK with me adding your Tested-by there) together with the other patches which got my RB up until now.
I will be reviewing appended signature secure boot support, IBM vTPM, etc. patches in the following weeks.
Quoted 4 lines
I'm going to revive my own patch series and send it in for review soonish. The nice thing of having waited so long is that Argon2 support has meanwhile landed in libgcrypt, so that should make it easier to integrate it into GRUB.
Nice to hear that.
Daniel
[1] https://lists.gnu.org/archive/html/grub-devel/2022-04/msg00064.html
Quoted 65 lines · Daniel Kiper
Huh! For some reason I missed this email... Sorry folks about that!
Quoted 45 lines · Patrick Steinhardt
Quoted 35 lines · Daniel Kiper
Quoted 25 lines · Stefan Berger
Quoted 24 lines · Daniel Kiper
Quoted 22 lines · Daniel Kiper
Quoted 10 lines · Daniel Axtens
Hi all,
This is, at long last, an updated version of my series extending Patrick's dynamic memory regions to ieee1275.
Noteworthy changes:
- reworked debug prints as
grub_dprintfs. Folded the ieee1275 ones into theieee1275 patches.Quoted 8 lines
- reworked the ieee1275 runtime memory claiming to be more resilient and better documented.
- fixed comment style and hopefully addressed all other change requests.
- grub will now try asking for contiguous memory and then, if that fails, for discontiguous memory - in case region merging with the discontiguous memory is sufficient to allow the eventual allocation to succeed.
Patrick, all mm and EFI code got my RB. Could you test it with your Argon changes? If these changes pass your tests I will merge them.
Quoted 8 lines
Quoted 5 lines
Patrick, ping?
To be more precise, I am thinking about the patches up to #10.
Daniel
Any way we can make progress with this series before it gets all forgotten about?
Quoted 8 lines
It is not and it will not be forgotten. I am waiting for some allocator tests reports. When I get them I will merge allocator stuff and review the rest of the code from this patch series. Sadly folks who are going to test the code are busy with other stuff. Though I am pinging them...
Anyway, sorry for delay...
Daniel
Quoted 7 lines
Sorry for the delay here, and thanks for the nudge. I've found a few quiet moments this morning to rebase my Argon2 patch series for LUKS2 on top of this series and found everything to work as expected. Decrypting a volume with a memory hardness of 2GB RAM worked just fine on an EFI x64 system. So this series is:
Tested-by Patrick Steinhardt <[email protected]>Patrick, thanks a lot for doing tests!
Here I would like to thank Patrick, Stefan, Daniel, Glenn and other folks who were working on this feature! It is very important and long awaited improvement for the GRUB memory manager (everyone who was looking at it knows it is one of the most complicated parts of the GRUB). This patch set unblocks and makes possible work on Argon2, appended signature secure boot support, IBM vTPM, etc.
Quoted 4 lines
If I do not hear any objections by the end of this week I will be pushing into master v3 of this patch set [1] (Patrick, I hope you are OK with me adding your Tested-by there) together with the other patches which got my RB up until now.
Yes, I am. I noticed at a later point that I accidentally tested v2 and already re-did the tests on v3 with the same results.
Patrick
Quoted 18 lines
I will be reviewing appended signature secure boot support, IBM vTPM, etc. patches in the following weeks.
Quoted 4 lines
I'm going to revive my own patch series and send it in for review soonish. The nice thing of having waited so long is that Argon2 support has meanwhile landed in libgcrypt, so that should make it easier to integrate it into GRUB.
Nice to hear that.
Daniel
[1] https://lists.gnu.org/archive/html/grub-devel/2022-04/msg00064.html
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel