mm: When adding a region, merge with region after as well as before

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.

Signed-off-by: Daniel Axtens <dja@axtens.net>
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Tested-by: Patrick Steinhardt <ps@pks.im>
This commit is contained in:
Daniel Axtens authored and Daniel Kiper committed 2022-07-04 14:13:56 +02:00
1 parent 1df8fe66c5
commit 052e6068be
2 files changed
+45

No files matched your search

+36
View File
@@ -130,6 +130,7 @@ 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
@@ -178,6 +179,40 @@ grub_mm_init_region (void *addr, grub_size_t size)
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.
*
* Cast to (void *) first to avoid the following build error:
* kern/mm.c: In function ‘grub_mm_init_region’:
* kern/mm.c:211:15: error: cast increases required alignment of target type [-Werror=cast-align]
* 211 | h = (grub_mm_header_t) ((grub_uint8_t *) addr - q->post_size);
* | ^
* It is safe to do that because proper alignment is enforced in grub_mm_size_sanity_check().
*/
h = (grub_mm_header_t)(void *) ((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 +230,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. */
+9
View File
@@ -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;