Skip to content

Commit 17cc5c6

Browse files
skodali4dcrowell77
authored andcommitted
Introduce deactivated discontiguous memory allocator
This commit introduces the code for a discontiguous memory allocator to implement malloc() and free(). The allocator is not active yet, and all code still uses the contiguous allocator. (See below.) A discontiguous allocator is useful because for most cases, Hostboot allocates memory and doesn't expect the physical pages that back the memory range to be contiguous. Allowing the physical pages to be discontiguous helps us deal with memory fragmentation by allowing large allocations to be split up wherever they can fit in the physical address space. There are certain places where contiguous physical memory is required, such as by the kernel, or in communication with the SBE, which reads physical memory directly without going through page translation. These places call the contiguous-backed allocator explicitly. This allocator allocates a block of virtual memory space, and uses a "bump" allocator strategy (i.e. new allocations always come from the front of the heap, and allocations are never reused). The allocator is not activated yet because we are still experimenting with the best heap sizes and allocation strategies. JIRA: PFHB-521 Change-Id: I7e8de943e0dc348845a2fa3d9beb860bed92a99e Reviewed-on: http://rchgit01.rchland.ibm.com/gerrit1/155995 Tested-by: Jenkins OP Build CI <[email protected]> Tested-by: Jenkins Server <[email protected]> Reviewed-by: Matthew Raybuck <[email protected]> Tested-by: Jenkins Combined Simics CI <[email protected]> Tested-by: FSP CI Jenkins <[email protected]> Tested-by: Jenkins OP HW <[email protected]> Tested-by: Hostboot CI <[email protected]> Reviewed-by: Daniel M Crowell <[email protected]>
1 parent 063271d commit 17cc5c6

File tree

16 files changed

+696
-62
lines changed

16 files changed

+696
-62
lines changed

src/build/tools/hb

+1-1
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ hb_startsimics()
720720
fi
721721

722722
# For GCOV, tell simics to enable the larger backing cache.
723-
if [ "$HOSTBOOT_PROFILE" ] ; then
723+
if [[ "$HOSTBOOT_PROFILE" || "$SIMICS_MORECACHE" ]] ; then
724724
# enable more cores for 16MB backing cache
725725
export SIMICS_MORECACHE=1
726726
STARTSIMCMD+=(num_cores_per_chip=8)

src/include/stdlib.h

+53-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/* */
66
/* OpenPOWER HostBoot Project */
77
/* */
8-
/* Contributors Listed Below - COPYRIGHT 2010,2021 */
8+
/* Contributors Listed Below - COPYRIGHT 2010,2023 */
99
/* [+] International Business Machines Corp. */
1010
/* */
1111
/* */
@@ -32,9 +32,59 @@ extern "C"
3232
{
3333
#endif
3434

35-
void* malloc(size_t) __attribute__((malloc));
36-
void free(void*);
35+
/**
36+
* @brief allocates memory contiguous in physical memory
37+
* @param[in] size size of allocation.
38+
* @return Pointer to beginning of allocation block
39+
*/
40+
void* contiguous_malloc(size_t)__attribute__((malloc));
41+
42+
/**
43+
* @brief allocates memory discontiguous in physical memory
44+
* @param[in] size size of allocation.
45+
* @return Pointer to beginning of allocation block
46+
*/
47+
void* discontiguous_malloc(size_t);
48+
49+
/**
50+
* @brief Uses the currently active allocator (contiguous or
51+
* discontiguous). Always uses the contiguous allocator in kernel
52+
* mode.
53+
* @param[in] size size of allocation.
54+
* @return pointer to beginning of allocation block
55+
*/
56+
void* malloc(size_t)__attribute__((malloc));
57+
58+
/**
59+
* @brief Function to free allocated block. Can be used with the
60+
* results of either contiguous or discontiguous allocator.
61+
* @param[in] ptr pointer to allocation block needed to be freed
62+
*/
63+
void free(void*);
64+
65+
/**
66+
* @brief called in istep 6.5 to switch the default allocator to
67+
* discontiguous malloc. This is the earliest point when the VMM
68+
* system is ready to handle a call mm_alloc_block.
69+
*/
70+
void activate_discontiguous_malloc_heap();
71+
72+
/**
73+
* @brief Called in exit_cache_contained to return to the
74+
* physically contiguous allocator. We do this because we
75+
* currently have limited virtual address space, and after exiting
76+
* cache, we don't have tight memory constraints.
77+
*/
78+
void deactivate_discontiguous_malloc_heap();
79+
80+
/**
81+
* @brief Can be used with a pointer from either allocator.
82+
*/
3783
void* realloc(void*, size_t);
84+
85+
/**
86+
* @brief Uses the current allocator.
87+
*/
3888
void* calloc(size_t, size_t) __attribute__((malloc));
3989

4090
/**

src/include/usr/vmmconst.h

+10
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ enum VmmRestriction : uint64_t
155155
#define VMM_VADDR_MALLOC (16 * GIGABYTE)
156156
#define VMM_MALLOC_SIZE (1 * GIGABYTE)
157157

158+
/** Userspace for malloc backed by virtual memory*/
159+
#define VMM_VADDR_MALLOC_VIRT (70 * GIGABYTE)
160+
161+
// Increasing this number will make more page table entries need to be
162+
// allocated; divide this number by 1024 to get the number of bytes
163+
// needed for the page tables (e.g. 128 mb heap = 128 kb of page
164+
// tables).
165+
#define VMM_MALLOC_VIRT_SIZE (128 * MEGABYTE)
166+
#define VMM_MALLOC_EXTENDED_SIZE (2 * GIGABYTE)
167+
158168
/**
159169
* Other Constants
160170
*/

src/kernel/heapmgr.C

+15-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/* */
66
/* OpenPOWER HostBoot Project */
77
/* */
8-
/* Contributors Listed Below - COPYRIGHT 2010,2020 */
8+
/* Contributors Listed Below - COPYRIGHT 2010,2023 */
99
/* [+] International Business Machines Corp. */
1010
/* */
1111
/* */
@@ -33,6 +33,8 @@
3333
#include <usr/debugpointers.H>
3434
#include <arch/magic.H>
3535
#include <usr/vmmconst.h>
36+
#include <kernel/misc.H>
37+
#include <stdlib.h>
3638

3739
#ifdef HOSTBOOT_DEBUG
3840
#define SMALL_HEAP_PAGES_TRACKED 64
@@ -264,16 +266,21 @@ void* _enforceBigFence(void* const i_pAddr)
264266

265267
void * HeapManager::allocate(size_t i_sz)
266268
{
269+
267270
HeapManager& hmgr = Singleton<HeapManager>::instance();
268271
size_t overhead = 0;
269272

270273
#ifdef CONFIG_MALLOC_FENCING
271274
overhead = offsetof(fence_t,data) + sizeof(CHECK::END);
272275
#endif
273-
276+
// do not want huge allocations when in kernel mode (huge
277+
// allocations use mm_alloc_block); kernel memory accesses don't
278+
// go through page translation.
274279
if( (i_sz + overhead > MAX_BIG_ALLOC_SIZE)
275-
&& (i_sz + overhead <= HC_SLOT_SIZE) )
280+
&& (i_sz + overhead <= HC_SLOT_SIZE)
281+
&& !(KernelMisc::in_kernel_mode()))
276282
{
283+
277284
printkd("allocateHuge=%ld [%d]\n", i_sz, task_gettid());
278285
void* ptr = hmgr._allocateHuge(i_sz);
279286
if( ptr )
@@ -859,7 +866,8 @@ void* HeapManager::_allocateBig(size_t i_sz)
859866
}
860867
if(!bc)
861868
{
862-
bc = new big_chunk_t(v,pages);
869+
bc = (big_chunk_t*) contiguous_malloc(sizeof(big_chunk_t));
870+
bc = new (bc) big_chunk_t(v,pages);
863871
big_chunk_stack.push(bc);
864872
}
865873

@@ -975,8 +983,8 @@ void* HeapManager::_allocateHuge(size_t i_sz)
975983
addr >= VMM_VADDR_MALLOC;
976984
addr -= HC_SLOT_SIZE )
977985
{
978-
huge_chunk_t* hc =
979-
new huge_chunk_t(reinterpret_cast<void*>(addr),0);
986+
auto hc = static_cast<huge_chunk_t*>(contiguous_malloc(sizeof(huge_chunk_t)));
987+
hc = new (hc) huge_chunk_t(reinterpret_cast<void*>(addr),0);
980988
huge_chunk_stack.push(hc);
981989
}
982990

@@ -1019,6 +1027,7 @@ void* HeapManager::_allocateHuge(size_t i_sz)
10191027
int rc = mm_set_permission(hc->addr,
10201028
pages*PAGESIZE,
10211029
WRITABLE | ALLOCATE_FROM_ZERO );
1030+
10221031
if(rc != 0)
10231032
{
10241033
printk( "_allocateHuge> mm_set_permission failed for requested size=%ld!!\n", i_sz );
@@ -1137,4 +1146,3 @@ void* HeapManager::_reallocHuge(void* i_ptr, size_t i_sz)
11371146

11381147
return i_ptr;
11391148
}
1140-

src/kernel/ptmgr.C

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/* */
66
/* OpenPOWER HostBoot Project */
77
/* */
8-
/* Contributors Listed Below - COPYRIGHT 2011,2022 */
8+
/* Contributors Listed Below - COPYRIGHT 2011,2023 */
99
/* [+] International Business Machines Corp. */
1010
/* */
1111
/* */
@@ -328,7 +328,7 @@ PageTableManager::PageTableManager( bool i_userSpace )
328328
uint64_t l_hrmor = 0;
329329
if( i_userSpace )
330330
{
331-
ivTABLE = new char[getSize()];
331+
ivTABLE = static_cast<char*>(contiguous_malloc(getSize()));
332332
printk( "** PageTableManager running in USER_SPACE : ivTABLE = %p**\n", ivTABLE );
333333
l_hrmor = cpu_spr_value(CPU_SPR_HRMOR);
334334
}
@@ -1287,4 +1287,3 @@ void PageTableManager::_flush( void )
12871287
++pte;
12881288
}
12891289
}
1290-

0 commit comments

Comments
 (0)