mm: Better handling of regions
v3 cover mm: Better handling of regions
Hi,
Change from V2:
1. Splited into two patches. One deal with the disk cache problem. The other is focused on improvement.
2. Add types to regions. So small and large allocations are effectively seperated into different heaps. This facility can also seperate module segments and plain data chunks.
3. Region management costs are now denoted by GRUB_MM_MAX_COST, seperated from GRUB_MM_GROW_SMALL ( =GRUB_MM_HEAP_GROW in previous patch).
Some notes for the size of heap growth:
In order to guarantee the success of allocation when heap exhausted, the new region size should be at least (size + align + GRUB_MM_MAX_COST). For small chunks, it's adjusted to (GRUB_MM_GROW_SMALL + GRUB_MM_MAX_COST) to improve performance.
Best Regards,
Zhang Boyang
RFC PATCH v3 1/2 mm: Try invalidate disk caches last when out of memory
Previously, every heap grow will cause all disk caches invalidated, which decreases performance severely. This patch moves disk cache invalidation code to the last of memory squeezing measures, so disk caches are released only when no other ways to get free memory.
--- grub-core/kern/mm.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
grub-core/kern/mm.c +7 -7
@@ -443,12 +443,6 @@ grub_memalign (grub_size_t align, grub_size_t size)switch (count) {case 0 : -/* Invalidate disk caches. */ -grub_disk_cache_invalidate_all (); - count++; - goto again; - -case 1 :/* Request additional pages, contiguous */ count++;
@@ -458,7 +452,7 @@ grub_memalign (grub_size_t align, grub_size_t size)/* fallthrough */ -case 2 : +case 1 :/* Request additional pages, anything at all */ count++;
@@ -474,6 +468,12 @@ grub_memalign (grub_size_t align, grub_size_t size)/* fallthrough */ +case 2 : +/* Invalidate disk caches. */ +grub_disk_cache_invalidate_all (); + count++; + goto again; +default :break ; }
Signature · 1 line
2.30.2
Re: [RFC PATCH v3 1/2] mm: Try invalidate disk caches last when out of memory
Quoted 6 lines · Zhang Boyang
Previously, every heap grow will cause all disk caches invalidated, which decreases performance severely. This patch moves disk cache invalidation code to the last of memory squeezing measures, so disk caches are released only when no other ways to get free memory.
Signed-off-by Zhang Boyang <[email protected]>
Daniel
Re: [RFC PATCH v3 1/2] mm: Try invalidate disk caches last when out of memory
In reply to Zhang BoyangQuoted 52 lines · Zhang Boyang
Previously, every heap grow will cause all disk caches invalidated, which decreases performance severely. This patch moves disk cache invalidation code to the last of memory squeezing measures, so disk caches are released only when no other ways to get free memory.
Signed-off-by Zhang Boyang <[email protected]>--- grub-core/kern/mm.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)grub-core/kern/mm.c +7
-7@@ -443,12 +443,6 @@ grub_memalign (grub_size_t align, grub_size_t size)switch (count) {case 0 : -/* Invalidate disk caches. */ -grub_disk_cache_invalidate_all (); - count++; - goto again; - -case 1 :/* Request additional pages, contiguous */ count++;@@ -458,7 +452,7 @@ grub_memalign (grub_size_t align, grub_size_t size)/* fallthrough */ -case 2 : +case 1 :/* Request additional pages, anything at all */ count++;@@ -474,6 +468,12 @@ grub_memalign (grub_size_t align, grub_size_t size)/* fallthrough */ +case 2 : +/* Invalidate disk caches. */ +grub_disk_cache_invalidate_all (); + count++; + goto again; +default :break ; }Signature · 1 line
2.30.2
RFC PATCH v3 2/2 mm: Separate different types of allocations into different regions
In reply to Zhang BoyangThis patch add type infomation to heap regions. Currently there are four
types: GRUB_MM_SIZE_SMALL, GRUB_MM_SIZE_LARGE, GRUB_MM_CLASS_DATA, GRUB_MM_CLASS_MODULE. Each heap region can have its own mask of acceptable memory allocation types.
The GRUB_MM_SIZE_* types denotes the relative size of an allocation. Allocations lesser than GRUB_MM_GROW_SMALL belong to GRUB_MM_SIZE_SMALL, otherwise they belong to GRUB_MM_SIZE_LARGE. Thus small and large chunks can be separated into different heaps. This might help reduce heap fragmentation.
The GRUB_MM_CLASS_* types denotes whether an allocation is a module segment. Module segments are allocated with GRUB_MM_CLASS_MODULE flag set. Plain data chunks (and module metadata) are allocated with GRUB_MM_CLASS_DATA flag set. Thus module segments and plain data chunks can be separated into different heaps.
On platforms with dynamic heap growth support, when heap space is exhausted, a new region will be allocated from firmware. The new region has same type flags as the allocation request itself. The size of new region will be (size + align + GRUB_MM_MAX_COST) for GRUB_MM_SIZE_LARGE requests, and (GRUB_MM_GROW_SMALL + GRUB_MM_MAX_COST) for GRUB_MM_SIZE_SMALL requests, with GRUB_MM_MAX_COST denotes the extra region management cost.
On platforms without dynamic heap growth support, default heap will be created with GRUB_MM_TYPE_ALL, so any type of allocation can be fulfilled by default heap.
--- docs/grub-dev.texi | 3 +- grub-core/kern/arm/coreboot/init.c | 4 +-- grub-core/kern/dl.c | 2 +- grub-core/kern/efi/mm.c | 12 ++++--- grub-core/kern/i386/coreboot/init.c | 2 +- grub-core/kern/i386/pc/init.c | 2 +- grub-core/kern/i386/qemu/init.c | 2 +- grub-core/kern/i386/xen/pvh.c | 2 +- grub-core/kern/ieee1275/init.c | 4 +-- grub-core/kern/main.c | 2 +- grub-core/kern/mips/arc/init.c | 2 +- grub-core/kern/mips/loongson/init.c | 2 +- grub-core/kern/mips/qemu_mips/init.c | 2 +- grub-core/kern/mm.c | 52 ++++++++++++++++++++++++---- grub-core/kern/uboot/hw.c | 2 +- grub-core/kern/xen/init.c | 2 +- include/grub/mm.h | 23 ++++++++++-- include/grub/mm_private.h | 5 ++- util/misc.c | 3 +- 19 files changed, 95 insertions(+), 33 deletions(-)
docs/grub-dev.texi +2 -1
@@ -818,7 +818,8 @@ If it works next stage is to have heap, console and timer. To have the heap working you need to determine which regions are suitable for heap usage, allocate them from firmware and map (if applicable). Then call -grub_mm_init_region (void *start, grub_size_t s) for every of this region. +grub_mm_init_region (void *addr, grub_size_t size, unsigned int types) for +every of this region. As a shortcut for early port you can allocate right after _end or have a big static array for heap. If you do you'll probably need to come back to this later. As for output console you should distinguish between an array of
grub-core/kern/arm/coreboot/init.c +2 -2
@@ -77,7 +77,7 @@ heap_init (grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type, {if (begin < (grub_addr_t )_start) { -grub_mm_init_region ((void *) (grub_addr_t ) begin, (grub_size_t) ((grub_addr_t )_start - begin)); +grub_mm_init_region ((void *) (grub_addr_t ) begin, (grub_size_t) ((grub_addr_t )_start - begin),GRUB_MM_TYPE_ALL ); have_memory =1 ; } begin = modend;
@@ -90,7 +90,7 @@ heap_init (grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type,if (end <= begin)return 0 ; -grub_mm_init_region ((void *) (grub_addr_t ) begin, (grub_size_t) (end - begin)); +grub_mm_init_region ((void *) (grub_addr_t ) begin, (grub_size_t) (end - begin),GRUB_MM_TYPE_ALL ); have_memory =1 ;
grub-core/kern/dl.c +1 -1
@@ -256,7 +256,7 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)#ifdef GRUB_MACHINE_EMU mod->base =grub_osdep_dl_memalign (talign, tsize);#else - mod->base =grub_memalign (talign, tsize); + mod->base =grub_module_memalign (talign, tsize);#endif if (!mod->base)return grub_errno;
grub-core/kern/efi/mm.c +7 -5
@@ -483,7 +483,7 @@ 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, - unsigned int flags) + unsigned int flags, unsigned int types) { grub_efi_memory_descriptor_t *desc;
@@ -515,7 +515,7 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map,"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)); +grub_mm_init_region (addr,PAGES_TO_BYTES (pages), types); required_pages -= pages;if (required_pages ==0 )
@@ -566,7 +566,7 @@ print_memory_map (grub_efi_memory_descriptor_t *memory_map,#endif static grub_err_t -grub_efi_mm_add_regions (grub_size_t required_bytes,unsigned int flags) +grub_efi_mm_add_regions (grub_size_t required_bytes,unsigned int flags,unsigned int types) {grub_efi_memory_descriptor_t *memory_map;grub_efi_memory_descriptor_t *memory_map_end;
@@ -622,7 +622,7 @@ grub_efi_mm_add_regions (grub_size_t required_bytes, unsigned int flags) err =add_memory_regions (filtered_memory_map, desc_size, filtered_memory_map_end,BYTES_TO_PAGES (required_bytes), - flags); + flags, types);if (err !=GRUB_ERR_NONE )return err;
@@ -649,7 +649,9 @@ grub_efi_mm_add_regions (grub_size_t required_bytes, unsigned int flags)void grub_efi_mm_init (void ) { -if (grub_efi_mm_add_regions (DEFAULT_HEAP_SIZE ,GRUB_MM_ADD_REGION_NONE ) !=GRUB_ERR_NONE ) +if (grub_efi_mm_add_regions (DEFAULT_HEAP_SIZE , +GRUB_MM_ADD_REGION_NONE , +GRUB_MM_SIZE_ALL |GRUB_MM_CLASS_DATA ) !=GRUB_ERR_NONE )grub_fatal ("%s" , grub_errmsg); grub_mm_add_region_fn = grub_efi_mm_add_regions; }
grub-core/kern/i386/coreboot/init.c +1 -1
@@ -81,7 +81,7 @@ heap_init (grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type,if (end <= begin)return 0 ; -grub_mm_init_region ((void *) (grub_addr_t ) begin, (grub_size_t) (end - begin)); +grub_mm_init_region ((void *) (grub_addr_t ) begin, (grub_size_t) (end - begin),GRUB_MM_TYPE_ALL ); have_memory =1 ;
grub-core/kern/i386/pc/init.c +1 -1
@@ -256,7 +256,7 @@ grub_machine_init (void) beg = modend;if (beg >= fin)continue ; -grub_mm_init_region ((void *) beg, fin - beg); +grub_mm_init_region ((void *) beg, fin - beg,GRUB_MM_TYPE_ALL ); }grub_tsc_init ();
grub-core/kern/i386/qemu/init.c +1 -1
@@ -77,7 +77,7 @@ heap_init (grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type,if (end <= begin)return 0 ; -grub_mm_init_region ((void *) (grub_addr_t ) begin, (grub_size_t) (end - begin)); +grub_mm_init_region ((void *) (grub_addr_t ) begin, (grub_size_t) (end - begin),GRUB_MM_TYPE_ALL );return 0 ; }
grub-core/kern/i386/xen/pvh.c +1 -1
@@ -261,7 +261,7 @@ grub_xen_mm_init_regions (void)continue ;if (to > (1ULL <<32 )) to =1ULL <<32 ; -grub_mm_init_region ((void *) (grub_addr_t ) from, to - from); +grub_mm_init_region ((void *) (grub_addr_t ) from, to - from,GRUB_MM_TYPE_ALL ); } }
grub-core/kern/ieee1275/init.c +2 -2
@@ -142,7 +142,7 @@ static voidgrub_claim_heap (void) {grub_mm_init_region ((void *) (grub_modules_get_end () - +GRUB_KERNEL_MACHINE_STACK_SIZE ),0x200000 ); + +GRUB_KERNEL_MACHINE_STACK_SIZE ),0x200000 ,GRUB_MM_TYPE_ALL ); }#else /* Helper for grub_claim_heap. */
@@ -190,7 +190,7 @@ heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, err =grub_claimmap (addr, len);if (err)return err; -grub_mm_init_region ((void *) (grub_addr_t ) addr, len); +grub_mm_init_region ((void *) (grub_addr_t ) addr, len,GRUB_MM_TYPE_ALL ); } *total += len;
grub-core/kern/main.c +1 -1
@@ -254,7 +254,7 @@ reclaim_module_space (void) grub_modbase =0 ;#if GRUB_KERNEL_PRELOAD_SPACE_REUSABLE -grub_mm_init_region ((void *) modstart, modend - modstart); +grub_mm_init_region ((void *) modstart, modend - modstart,GRUB_MM_TYPE_ALL );#else (void ) modstart; (void ) modend;
grub-core/kern/mips/arc/init.c +1 -1
@@ -250,7 +250,7 @@ grub_machine_init (void) end =0x20000000 ;if (end > start)grub_mm_init_region ((void *) (grub_addr_t) (start |0x80000000 ), - end - start); + end - start,GRUB_MM_TYPE_ALL ); }grub_console_init_lately ();
grub-core/kern/mips/loongson/init.c +1 -1
@@ -202,7 +202,7 @@ grub_machine_init (void) modend =grub_modules_get_end ();grub_mm_init_region ((void *) modend, (grub_arch_memsize <<20 ) - - (modend -GRUB_ARCH_LOWMEMVSTART )); + - (modend -GRUB_ARCH_LOWMEMVSTART ),GRUB_MM_TYPE_ALL );/* FIXME: use upper memory as well. */ /* Initialize output terminal (can't be done earlier, as gfxterm
grub-core/kern/mips/qemu_mips/init.c +1 -1
@@ -53,7 +53,7 @@ grub_machine_init (void) modend =grub_modules_get_end ();grub_mm_init_region ((void *) modend, grub_arch_memsize - - (modend -GRUB_ARCH_LOWMEMVSTART )); + - (modend -GRUB_ARCH_LOWMEMVSTART ),GRUB_MM_TYPE_ALL );grub_install_get_time_ms (grub_rtc_get_time_ms);
grub-core/kern/mm.c +45 -7
115 lines not on this page · raw message
grub-core/kern/uboot/hw.c +1 -1
@@ -41,7 +41,7 @@ grub_uboot_mm_init (void)struct sys_info *si =grub_uboot_get_sys_info ();grub_mm_init_region ((void *)grub_modules_get_end (), -GRUB_KERNEL_MACHINE_HEAP_SIZE ); +GRUB_KERNEL_MACHINE_HEAP_SIZE ,GRUB_MM_TYPE_ALL );if (si && (si->mr_no !=0 )) {
grub-core/kern/xen/init.c +1 -1
@@ -531,7 +531,7 @@ map_all_pages (void)grub_addr_t heap_start =grub_modules_get_end ();grub_addr_t heap_end = (grub_addr_t ) new_mfn_list; -grub_mm_init_region ((void *) heap_start, heap_end - heap_start); +grub_mm_init_region ((void *) heap_start, heap_end - heap_start,GRUB_MM_TYPE_ALL ); } grub_err_t
include/grub/mm.h +20 -3
@@ -29,14 +29,30 @@# define NULL ((void *) 0)#endif +/* An upper bound of management cost of each region, 4KB should be enough. */ +#define GRUB_MM_MAX_COST 0x1000 + +/* Region type flags. */ +#define GRUB_MM_SIZE_SMALL (1 << 0) +#define GRUB_MM_SIZE_LARGE (1 << 1) +#define GRUB_MM_SIZE_ALL (GRUB_MM_SIZE_SMALL | GRUB_MM_SIZE_LARGE) +#define GRUB_MM_CLASS_DATA (1 << 30) +#define GRUB_MM_CLASS_MODULE (1 << 31) +#define GRUB_MM_CLASS_ALL (GRUB_MM_CLASS_DATA | GRUB_MM_CLASS_MODULE) +#define GRUB_MM_TYPE_ALL (GRUB_MM_SIZE_ALL | GRUB_MM_CLASS_ALL) + +/* Heap grow granularity for region of small type. */ +#define GRUB_MM_GROW_SMALL 0x100000 +#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. +* parameter is a bitfield of `GRUB_MM_ADD_REGION` flags. The third parameter +* is a bitfield of `GRUB_MM_TYPE` flags. */ -typedef grub_err_t (*grub_mm_add_region_func_t ) (grub_size_t ,unsigned int ); +typedef grub_err_t (*grub_mm_add_region_func_t ) (grub_size_t ,unsigned int ,unsigned int );/* * Set this function pointer to enable adding memory-regions at runtime in case
@@ -46,7 +62,7 @@ typedef grub_err_t (*grub_mm_add_region_func_t) (grub_size_t, unsigned int);extern grub_mm_add_region_func_t EXPORT_VAR (grub_mm_add_region_fn );#endif -void grub_mm_init_region (void *addr,grub_size_t size); +void grub_mm_init_region (void *addr,grub_size_t size,unsigned int types);void *EXPORT_FUNC (grub_calloc ) (grub_size_t nmemb,grub_size_t size);void *EXPORT_FUNC (grub_malloc ) (grub_size_t size);void *EXPORT_FUNC (grub_zalloc ) (grub_size_t size);
@@ -54,6 +70,7 @@ void EXPORT_FUNC(grub_free) (void *ptr);void *EXPORT_FUNC (grub_realloc ) (void *ptr,grub_size_t size);#ifndef GRUB_MACHINE_EMU void *EXPORT_FUNC (grub_memalign ) (grub_size_t align,grub_size_t size); +void *EXPORT_FUNC (grub_module_memalign ) (grub_size_t align,grub_size_t size);#endif void grub_mm_check_real (const char *file,int line);
include/grub/mm_private.h +4 -1
@@ -90,8 +90,11 @@ typedef struct grub_mm_region/* How many bytes are in this region? (free and allocated) */ grub_size_t size; +/* Acceptable memory allocation types for this region. */ +unsigned int types; +/* pad to a multiple of cell size */ -char padding[3 *GRUB_CPU_SIZEOF_VOID_P ]; +void *padding[2 ]; } *grub_mm_region_t;
util/misc.c +2 -1
@@ -207,7 +207,8 @@ grub_dl_unref (grub_dl_t mod)/* Some functions that we don't use. */ void grub_mm_init_region (void *addr __attribute__ ((unused)), -grub_size_t size __attribute__ ((unused))) +grub_size_t size __attribute__ ((unused)), +unsigned int types __attribute__ ((unused))) { }
Signature · 1 line
2.30.2
Re: [RFC PATCH v3 2/2] mm: Separate different types of allocations into different regions
Quoted 25 lines · Zhang Boyang
This patch add type infomation to heap regions. Currently there are four
types:GRUB_MM_SIZE_SMALL,GRUB_MM_SIZE_LARGE,GRUB_MM_CLASS_DATA,GRUB_MM_CLASS_MODULE. Each heap region can have its own mask of acceptable memory allocation types.The GRUB_MM_SIZE_* types denotes the relative size of an allocation. Allocations lesser than
GRUB_MM_GROW_SMALLbelong toGRUB_MM_SIZE_SMALL, otherwise they belong toGRUB_MM_SIZE_LARGE. Thus small and large chunks can be separated into different heaps. This might help reduce heap fragmentation.The GRUB_MM_CLASS_* types denotes whether an allocation is a module segment. Module segments are allocated with
GRUB_MM_CLASS_MODULEflag set. Plain data chunks (and module metadata) are allocated withGRUB_MM_CLASS_DATAflag set. Thus module segments and plain data chunks can be separated into different heaps.On platforms with dynamic heap growth support, when heap space is exhausted, a new region will be allocated from firmware. The new region has same type flags as the allocation request itself. The size of new region will be (size + align +
GRUB_MM_MAX_COST) forGRUB_MM_SIZE_LARGErequests, and (GRUB_MM_GROW_SMALL+GRUB_MM_MAX_COST) forGRUB_MM_SIZE_SMALLrequests, withGRUB_MM_MAX_COSTdenotes the extra region management cost.
Quoted 3 lines
On platforms without dynamic heap growth support, default heap will be created with
GRUB_MM_TYPE_ALL, so any type of allocation can be fulfilled by default heap.
I wonder whether we can again split this patch up into at least two
parts: the first one that starts to round memory allocations, and then the second one to introduce memory types. The former would likely not be all that controversial. So by splitting this patch up we can already land the two patches to reorder disk cache invalidation and round requests, both of which should provide immediate benefit to users.
The introduction of memory request types can then be discussed separately.
Patrick
Quoted 491 lines
Signed-off-by Zhang Boyang <[email protected]>--- docs/grub-dev.texi | 3 +- grub-core/kern/arm/coreboot/init.c | 4 +-- grub-core/kern/dl.c | 2 +- grub-core/kern/efi/mm.c | 12 ++++--- grub-core/kern/i386/coreboot/init.c | 2 +- grub-core/kern/i386/pc/init.c | 2 +- grub-core/kern/i386/qemu/init.c | 2 +- grub-core/kern/i386/xen/pvh.c | 2 +- grub-core/kern/ieee1275/init.c | 4 +-- grub-core/kern/main.c | 2 +- grub-core/kern/mips/arc/init.c | 2 +- grub-core/kern/mips/loongson/init.c | 2 +- grub-core/kern/mips/qemu_mips/init.c | 2 +- grub-core/kern/mm.c | 52 ++++++++++++++++++++++++---- grub-core/kern/uboot/hw.c | 2 +- grub-core/kern/xen/init.c | 2 +- include/grub/mm.h | 23 ++++++++++-- include/grub/mm_private.h | 5 ++- util/misc.c | 3 +- 19 files changed, 95 insertions(+), 33 deletions(-)docs/grub-dev.texi +2
-1@@ -818,7 +818,8 @@ If it works next stage is to have heap, console and timer. To have the heap working you need to determine which regions are suitable for heap usage, allocate them from firmware and map (if applicable). Then call -grub_mm_init_region (void *start, grub_size_t s) for every of this region. +grub_mm_init_region (void *addr, grub_size_t size, unsigned int types) for +every of this region. As a shortcut for early port you can allocate right after _end or have a big static array for heap. If you do you'll probably need to come back to this later. As for output console you should distinguish between an array ofgrub-core/kern/arm/coreboot/init.c +2
-2@@ -77,7 +77,7 @@ heap_init (grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type, {if (begin < (grub_addr_t )_start) { -grub_mm_init_region ((void *) (grub_addr_t ) begin, (grub_size_t) ((grub_addr_t )_start - begin)); +grub_mm_init_region ((void *) (grub_addr_t ) begin, (grub_size_t) ((grub_addr_t )_start - begin),GRUB_MM_TYPE_ALL ); have_memory =1 ; } begin = modend;@@ -90,7 +90,7 @@ heap_init (grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type,if (end <= begin)return 0 ; -grub_mm_init_region ((void *) (grub_addr_t ) begin, (grub_size_t) (end - begin)); +grub_mm_init_region ((void *) (grub_addr_t ) begin, (grub_size_t) (end - begin),GRUB_MM_TYPE_ALL ); have_memory =1 ;grub-core/kern/dl.c +1
-1@@ -256,7 +256,7 @@ grub_dl_load_segments (grub_dl_t mod, const Elf_Ehdr *e)#ifdef GRUB_MACHINE_EMU mod->base =grub_osdep_dl_memalign (talign, tsize);#else - mod->base =grub_memalign (talign, tsize); + mod->base =grub_module_memalign (talign, tsize);#endif if (!mod->base)return grub_errno;grub-core/kern/efi/mm.c +7
-5@@ -483,7 +483,7 @@ 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, - unsigned int flags) + unsigned int flags, unsigned int types) { grub_efi_memory_descriptor_t *desc;@@ -515,7 +515,7 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map,"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)); +grub_mm_init_region (addr,PAGES_TO_BYTES (pages), types); required_pages -= pages;if (required_pages ==0 )@@ -566,7 +566,7 @@ print_memory_map (grub_efi_memory_descriptor_t *memory_map,#endif static grub_err_t -grub_efi_mm_add_regions (grub_size_t required_bytes,unsigned int flags) +grub_efi_mm_add_regions (grub_size_t required_bytes,unsigned int flags,unsigned int types) {grub_efi_memory_descriptor_t *memory_map;grub_efi_memory_descriptor_t *memory_map_end;@@ -622,7 +622,7 @@ grub_efi_mm_add_regions (grub_size_t required_bytes, unsigned int flags) err =add_memory_regions (filtered_memory_map, desc_size, filtered_memory_map_end,BYTES_TO_PAGES (required_bytes), - flags); + flags, types);if (err !=GRUB_ERR_NONE )return err;@@ -649,7 +649,9 @@ grub_efi_mm_add_regions (grub_size_t required_bytes, unsigned int flags)void grub_efi_mm_init (void ) { -if (grub_efi_mm_add_regions (DEFAULT_HEAP_SIZE ,GRUB_MM_ADD_REGION_NONE ) !=GRUB_ERR_NONE ) +if (grub_efi_mm_add_regions (DEFAULT_HEAP_SIZE , +GRUB_MM_ADD_REGION_NONE , +GRUB_MM_SIZE_ALL |GRUB_MM_CLASS_DATA ) !=GRUB_ERR_NONE )grub_fatal ("%s" , grub_errmsg); grub_mm_add_region_fn = grub_efi_mm_add_regions; }grub-core/kern/i386/coreboot/init.c +1
-1@@ -81,7 +81,7 @@ heap_init (grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type,if (end <= begin)return 0 ; -grub_mm_init_region ((void *) (grub_addr_t ) begin, (grub_size_t) (end - begin)); +grub_mm_init_region ((void *) (grub_addr_t ) begin, (grub_size_t) (end - begin),GRUB_MM_TYPE_ALL ); have_memory =1 ;grub-core/kern/i386/pc/init.c +1
-1@@ -256,7 +256,7 @@ grub_machine_init (void) beg = modend;if (beg >= fin)continue ; -grub_mm_init_region ((void *) beg, fin - beg); +grub_mm_init_region ((void *) beg, fin - beg,GRUB_MM_TYPE_ALL ); }grub_tsc_init ();grub-core/kern/i386/qemu/init.c +1
-1@@ -77,7 +77,7 @@ heap_init (grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type,if (end <= begin)return 0 ; -grub_mm_init_region ((void *) (grub_addr_t ) begin, (grub_size_t) (end - begin)); +grub_mm_init_region ((void *) (grub_addr_t ) begin, (grub_size_t) (end - begin),GRUB_MM_TYPE_ALL );return 0 ; }grub-core/kern/i386/xen/pvh.c +1
-1@@ -261,7 +261,7 @@ grub_xen_mm_init_regions (void)continue ;if (to > (1ULL <<32 )) to =1ULL <<32 ; -grub_mm_init_region ((void *) (grub_addr_t ) from, to - from); +grub_mm_init_region ((void *) (grub_addr_t ) from, to - from,GRUB_MM_TYPE_ALL ); } }grub-core/kern/ieee1275/init.c +2
-2@@ -142,7 +142,7 @@ static voidgrub_claim_heap (void) {grub_mm_init_region ((void *) (grub_modules_get_end () - +GRUB_KERNEL_MACHINE_STACK_SIZE ),0x200000 ); + +GRUB_KERNEL_MACHINE_STACK_SIZE ),0x200000 ,GRUB_MM_TYPE_ALL ); }#else /* Helper for grub_claim_heap. */ @@ -190,7 +190,7 @@ heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, err =grub_claimmap (addr, len);if (err)return err; -grub_mm_init_region ((void *) (grub_addr_t ) addr, len); +grub_mm_init_region ((void *) (grub_addr_t ) addr, len,GRUB_MM_TYPE_ALL ); } *total += len;grub-core/kern/main.c +1
-1@@ -254,7 +254,7 @@ reclaim_module_space (void) grub_modbase =0 ;#if GRUB_KERNEL_PRELOAD_SPACE_REUSABLE -grub_mm_init_region ((void *) modstart, modend - modstart); +grub_mm_init_region ((void *) modstart, modend - modstart,GRUB_MM_TYPE_ALL );#else (void ) modstart; (void ) modend;grub-core/kern/mips/arc/init.c +1
-1@@ -250,7 +250,7 @@ grub_machine_init (void) end =0x20000000 ;if (end > start)grub_mm_init_region ((void *) (grub_addr_t) (start |0x80000000 ), - end - start); + end - start,GRUB_MM_TYPE_ALL ); }grub_console_init_lately ();grub-core/kern/mips/loongson/init.c +1
-1@@ -202,7 +202,7 @@ grub_machine_init (void) modend =grub_modules_get_end ();grub_mm_init_region ((void *) modend, (grub_arch_memsize <<20 ) - - (modend -GRUB_ARCH_LOWMEMVSTART )); + - (modend -GRUB_ARCH_LOWMEMVSTART ),GRUB_MM_TYPE_ALL );/* FIXME: use upper memory as well. */ /* Initialize output terminal (can't be done earlier, as gfxterm grub-core/kern/mips/qemu_mips/init.c +1
-1@@ -53,7 +53,7 @@ grub_machine_init (void) modend =grub_modules_get_end ();grub_mm_init_region ((void *) modend, grub_arch_memsize - - (modend -GRUB_ARCH_LOWMEMVSTART )); + - (modend -GRUB_ARCH_LOWMEMVSTART ),GRUB_MM_TYPE_ALL );grub_install_get_time_ms (grub_rtc_get_time_ms);grub-core/kern/mm.c +45
-7@@ -114,13 +114,13 @@ get_header_from_pointer (void *ptr, grub_mm_header_t *p, grub_mm_region_t *r)/* Initialize a region starting from ADDR and whose size is SIZE, to use it as free space. */ void -grub_mm_init_region (void *addr,grub_size_t size) +grub_mm_init_region (void *addr,grub_size_t size,unsigned int types) {grub_mm_header_t h;grub_mm_region_t r, *p, q; -grub_dprintf ("regions" ,"Using memory for heap: start=%p, end=%p\n" , - addr, (char *) addr + (unsigned int ) size); +grub_dprintf ("regions" ,"Using memory for heap: start=%p, end=%p, types=0x%x\n" , + addr, (char *) addr + (unsigned int ) size, types);/* Exclude last 4K to avoid overflows. */ /* If addr + 0x1000 overflows then whole region is in excluded zone. */ @@ -134,6 +134,10 @@ 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) { +/* Don't merge regions with different allocation types */ +if (q->types != types) +continue ; +/* * Is the new region immediately below an existing region? That * is, is the address of the memory we're adding now (addr) + size @@ -248,6 +252,7 @@ grub_mm_init_region (void *addr, grub_size_t size) 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; + r->types = types;/* Find where to insert this region. Put a smaller one before bigger ones, to prevent fragmentation. */ @@ -405,12 +410,14 @@ grub_real_malloc (grub_mm_header_t *first, grub_size_t n, grub_size_t align) }/* Allocate SIZE bytes with the alignment ALIGN and return the pointer. */ -void * -grub_memalign (grub_size_t align,grub_size_t size) +static void * +grub_real_memalign (grub_size_t align,grub_size_t size,unsigned int types) {grub_mm_region_t r;grub_size_t n = ((size +GRUB_MM_ALIGN -1 ) >>GRUB_MM_ALIGN_LOG2 ) +1 ; +grub_size_t guarantee;int count =0 ; +grub_size_t grow;if (!grub_mm_base) goto fail;@@ -424,6 +431,15 @@ grub_memalign (grub_size_t align, grub_size_t size)if ((size + align) > ~(grub_size_t )0x100000 ) goto fail; +/* Allocation should always success if region free bytes >= guarantee. */ + guarantee = size + align; + +/* Set size flag heuristically. */ +if (guarantee <=GRUB_MM_GROW_SMALL ) + types |=GRUB_MM_SIZE_SMALL ; +else + types |=GRUB_MM_SIZE_LARGE ; + align = (align >>GRUB_MM_ALIGN_LOG2 );if (align ==0 ) align =1 ;@@ -432,6 +448,9 @@ grub_memalign (grub_size_t align, grub_size_t size)for (r = grub_mm_base; r; r = r->next) { +if ((types & ~r->types) !=0 ) +continue ; +void *p; p =grub_real_malloc (&(r->first), n, align);@@ -446,8 +465,13 @@ grub_memalign (grub_size_t align, grub_size_t size)/* Request additional pages, contiguous */ count++; +if ((types &GRUB_MM_SIZE_SMALL )) + grow =GRUB_MM_GROW_SMALL +GRUB_MM_MAX_COST ; +else + grow = guarantee +GRUB_MM_MAX_COST ; +if (grub_mm_add_region_fn !=NULL && -grub_mm_add_region_fn (size,GRUB_MM_ADD_REGION_CONSECUTIVE ) ==GRUB_ERR_NONE ) +grub_mm_add_region_fn (grow,GRUB_MM_ADD_REGION_CONSECUTIVE , types) ==GRUB_ERR_NONE ) goto again;/* fallthrough */ @@ -462,7 +486,7 @@ grub_memalign (grub_size_t align, grub_size_t size)* 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 ); +grub_mm_add_region_fn (grow,GRUB_MM_ADD_REGION_NONE , types); goto again; }@@ -483,6 +507,20 @@ grub_memalign (grub_size_t align, grub_size_t size)return 0 ; } +/* Allocate memory for plain data. */ +void * +grub_memalign (grub_size_t align,grub_size_t size) +{ +return grub_real_memalign (align, size,GRUB_MM_CLASS_DATA ); +} + +/* Allocate memory for modules. */ +void * +grub_module_memalign (grub_size_t align,grub_size_t size) +{ +return grub_real_memalign (align, size,GRUB_MM_CLASS_MODULE ); +} +/* * Allocate NMEMB instances of SIZE bytes and return the pointer, or error on * integer overflow. grub-core/kern/uboot/hw.c +1
-1@@ -41,7 +41,7 @@ grub_uboot_mm_init (void)struct sys_info *si =grub_uboot_get_sys_info ();grub_mm_init_region ((void *)grub_modules_get_end (), -GRUB_KERNEL_MACHINE_HEAP_SIZE ); +GRUB_KERNEL_MACHINE_HEAP_SIZE ,GRUB_MM_TYPE_ALL );if (si && (si->mr_no !=0 )) {grub-core/kern/xen/init.c +1
-1@@ -531,7 +531,7 @@ map_all_pages (void)grub_addr_t heap_start =grub_modules_get_end ();grub_addr_t heap_end = (grub_addr_t ) new_mfn_list; -grub_mm_init_region ((void *) heap_start, heap_end - heap_start); +grub_mm_init_region ((void *) heap_start, heap_end - heap_start,GRUB_MM_TYPE_ALL ); } grub_err_tinclude/grub/mm.h +20
-3@@ -29,14 +29,30 @@# define NULL ((void *) 0)#endif +/* An upper bound of management cost of each region, 4KB should be enough. */ +#define GRUB_MM_MAX_COST 0x1000 + +/* Region type flags. */ +#define GRUB_MM_SIZE_SMALL (1 << 0) +#define GRUB_MM_SIZE_LARGE (1 << 1) +#define GRUB_MM_SIZE_ALL (GRUB_MM_SIZE_SMALL | GRUB_MM_SIZE_LARGE) +#define GRUB_MM_CLASS_DATA (1 << 30) +#define GRUB_MM_CLASS_MODULE (1 << 31) +#define GRUB_MM_CLASS_ALL (GRUB_MM_CLASS_DATA | GRUB_MM_CLASS_MODULE) +#define GRUB_MM_TYPE_ALL (GRUB_MM_SIZE_ALL | GRUB_MM_CLASS_ALL) + +/* Heap grow granularity for region of small type. */ +#define GRUB_MM_GROW_SMALL 0x100000 +#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. +* parameter is a bitfield of `GRUB_MM_ADD_REGION` flags. The third parameter +* is a bitfield of `GRUB_MM_TYPE` flags. */ -typedef grub_err_t (*grub_mm_add_region_func_t ) (grub_size_t ,unsigned int ); +typedef grub_err_t (*grub_mm_add_region_func_t ) (grub_size_t ,unsigned int ,unsigned int );/* * Set this function pointer to enable adding memory-regions at runtime in case @@ -46,7 +62,7 @@ typedef grub_err_t (*grub_mm_add_region_func_t) (grub_size_t, unsigned int);extern grub_mm_add_region_func_t EXPORT_VAR (grub_mm_add_region_fn );#endif -void grub_mm_init_region (void *addr,grub_size_t size); +void grub_mm_init_region (void *addr,grub_size_t size,unsigned int types);void *EXPORT_FUNC (grub_calloc ) (grub_size_t nmemb,grub_size_t size);void *EXPORT_FUNC (grub_malloc ) (grub_size_t size);void *EXPORT_FUNC (grub_zalloc ) (grub_size_t size);@@ -54,6 +70,7 @@ void EXPORT_FUNC(grub_free) (void *ptr);void *EXPORT_FUNC (grub_realloc ) (void *ptr,grub_size_t size);#ifndef GRUB_MACHINE_EMU void *EXPORT_FUNC (grub_memalign ) (grub_size_t align,grub_size_t size); +void *EXPORT_FUNC (grub_module_memalign ) (grub_size_t align,grub_size_t size);#endif void grub_mm_check_real (const char *file,int line);include/grub/mm_private.h +4
-1@@ -90,8 +90,11 @@ typedef struct grub_mm_region/* How many bytes are in this region? (free and allocated) */ grub_size_t size; +/* Acceptable memory allocation types for this region. */ +unsigned int types; +/* pad to a multiple of cell size */ -char padding[3 *GRUB_CPU_SIZEOF_VOID_P ]; +void *padding[2 ]; } *grub_mm_region_t;util/misc.c +2
-1@@ -207,7 +207,8 @@ grub_dl_unref (grub_dl_t mod)/* Some functions that we don't use. */ void grub_mm_init_region (void *addr __attribute__ ((unused)), -grub_size_t size __attribute__ ((unused))) +grub_size_t size __attribute__ ((unused)), +unsigned int types __attribute__ ((unused))) { }Signature · 1 line
2.30.2
Re: [RFC PATCH v3 2/2] mm: Separate different types of allocations into different regions
Quoted 37 lines · Patrick Steinhardt
Quoted 25 lines · Zhang Boyang
This patch add type infomation to heap regions. Currently there are four
types:GRUB_MM_SIZE_SMALL,GRUB_MM_SIZE_LARGE,GRUB_MM_CLASS_DATA,GRUB_MM_CLASS_MODULE. Each heap region can have its own mask of acceptable memory allocation types.The GRUB_MM_SIZE_* types denotes the relative size of an allocation. Allocations lesser than
GRUB_MM_GROW_SMALLbelong toGRUB_MM_SIZE_SMALL, otherwise they belong toGRUB_MM_SIZE_LARGE. Thus small and large chunks can be separated into different heaps. This might help reduce heap fragmentation.The GRUB_MM_CLASS_* types denotes whether an allocation is a module segment. Module segments are allocated with
GRUB_MM_CLASS_MODULEflag set. Plain data chunks (and module metadata) are allocated withGRUB_MM_CLASS_DATAflag set. Thus module segments and plain data chunks can be separated into different heaps.On platforms with dynamic heap growth support, when heap space is exhausted, a new region will be allocated from firmware. The new region has same type flags as the allocation request itself. The size of new region will be (size + align +
GRUB_MM_MAX_COST) forGRUB_MM_SIZE_LARGErequests, and (GRUB_MM_GROW_SMALL+GRUB_MM_MAX_COST) forGRUB_MM_SIZE_SMALLrequests, withGRUB_MM_MAX_COSTdenotes the extra region management cost.Quoted 3 lines
On platforms without dynamic heap growth support, default heap will be created with
GRUB_MM_TYPE_ALL, so any type of allocation can be fulfilled by default heap.I wonder whether we can again split this patch up into at least two
parts: the first one that starts to round memory allocations, and then the second one to introduce memory types. The former would likely not be all that controversial. So by splitting this patch up we can already land the two patches to reorder disk cache invalidation and round requests, both of which should provide immediate benefit to users.
Quoted 2 lines
The introduction of memory request types can then be discussed separately.
Yeah, I agree. Though first of all I would prefer fully understand all pros and cons of memory request types in the bootloader. At this point I would be more than happy if we simplify memory allocator/relocator than add more futures to it. I think the bootloader does not need complicated allocators covering all corner cases due to its quite short live time. I prefer something which is simple and reliable. And probably fast...
Daniel