Professional Documents
Culture Documents
Boot Time Memory Management - The Linux Kernel Documentation
Boot Time Memory Management - The Linux Kernel Documentation
Boot Time Memory Management - The Linux Kernel Documentation
Docs »
Core API Documentation »
Boot time memory management View page source
Memblock also offers a variety of APIs that control its own behaviour.
Memblock Overview
Memblock is a method of managing memory regions during the early
boot period when the usual kernel
memory allocators are not up and
running.
https://www.kernel.org/doc/html/v5.0/core-api/boot-time-mm.html[2020/3/30 17:18:55]
Boot time memory management — The Linux Kernel documentation
The early architecture setup should tell memblock what the physical
memory layout is by using
memblock_add() or
memblock_add_node() functions. The first function does not
assign the region to a
NUMA node and it is appropriate for UMA
systems. Yet, it is possible to use it on NUMA systems as well and
assign the region to a NUMA node later in the setup process using
memblock_set_node() . The
memblock_add_node()
performs such an assignment directly.
enum memblock_flags
Constants
MEMBLOCK_NONE
no special request
MEMBLOCK_HOTPLUG
hotpluggable region
MEMBLOCK_MIRROR
mirrored region
MEMBLOCK_NOMAP
don’t add to kernel direct mapping
struct memblock_region
https://www.kernel.org/doc/html/v5.0/core-api/boot-time-mm.html[2020/3/30 17:18:55]
Boot time memory management — The Linux Kernel documentation
Definition
struct memblock_region {
phys_addr_t base;
phys_addr_t size;
enum memblock_flags flags;
#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP;
int nid;
#endif;
};
Members
base
physical address of the region
size
size of the region
flags
memory region attributes
nid
NUMA node id
struct memblock_type
Definition
struct memblock_type {
unsigned long cnt;
unsigned long max;
phys_addr_t total_size;
struct memblock_region *regions;
char *name;
};
Members
cnt
number of regions
max
size of the allocated array
total_size
size of all regions
regions
array of regions
name
https://www.kernel.org/doc/html/v5.0/core-api/boot-time-mm.html[2020/3/30 17:18:55]
Boot time memory management — The Linux Kernel documentation
struct memblock
Definition
struct memblock {
bool bottom_up;
phys_addr_t current_limit;
struct memblock_type memory;
struct memblock_type reserved;
#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP;
struct memblock_type physmem;
#endif;
};
Members
bottom_up
is bottom up direction?
current_limit
physical address of the current allocation limit
memory
usabe memory regions
reserved
reserved memory regions
physmem
all physical memory
iterate through memblock areas from type_a and not included in type_b. Or just type_a if type_b is NULL.
Parameters
i
u64 used as loop variable
type_a
ptr to memblock_type to iterate
type_b
ptr to memblock_type which excludes from the iteration
nid
node selector, NUMA_NO_NODE for all nodes
flags
pick from blocks based on memory attributes
p_start
https://www.kernel.org/doc/html/v5.0/core-api/boot-time-mm.html[2020/3/30 17:18:55]
Boot time memory management — The Linux Kernel documentation
reverse iterate through memblock areas from type_a and not included in type_b. Or just type_a if type_b is
NULL.
Parameters
i
u64 used as loop variable
type_a
ptr to memblock_type to iterate
type_b
ptr to memblock_type which excludes from the iteration
nid
node selector, NUMA_NO_NODE for all nodes
flags
pick from blocks based on memory attributes
p_start
ptr to phys_addr_t for start address of the range, can be NULL
p_end
ptr to phys_addr_t for end address of the range, can be NULL
p_nid
ptr to int for nid of the range, can be NULL
Parameters
i
u64 used as loop variable
p_start
ptr to phys_addr_t for start address of the range, can be NULL
p_end
ptr to phys_addr_t for end address of the range, can be NULL
Description
https://www.kernel.org/doc/html/v5.0/core-api/boot-time-mm.html[2020/3/30 17:18:55]
Boot time memory management — The Linux Kernel documentation
Parameters
i
an integer used as loop variable
nid
node selector, MAX_NUMNODES for all nodes
p_start
ptr to ulong for start pfn of the range, can be NULL
p_end
ptr to ulong for end pfn of the range, can be NULL
p_nid
ptr to int for nid of the range, can be NULL
Description
Parameters
i
u64 used as loop variable
nid
node selector, NUMA_NO_NODE for all nodes
flags
pick from blocks based on memory attributes
p_start
ptr to phys_addr_t for start address of the range, can be NULL
p_end
ptr to phys_addr_t for end address of the range, can be NULL
p_nid
ptr to int for nid of the range, can be NULL
Description
https://www.kernel.org/doc/html/v5.0/core-api/boot-time-mm.html[2020/3/30 17:18:55]
Boot time memory management — The Linux Kernel documentation
Parameters
i
u64 used as loop variable
nid
node selector, NUMA_NO_NODE for all nodes
flags
pick from blocks based on memory attributes
p_start
ptr to phys_addr_t for start address of the range, can be NULL
p_end
ptr to phys_addr_t for end address of the range, can be NULL
p_nid
ptr to int for nid of the range, can be NULL
Description
void memblock_set_current_limit(phys_addr_t limit)
Set the current allocation limit to allow limiting allocations to what is currently accessible during boot
Parameters
phys_addr_t limit
New limit value (physical address)
Parameters
Return
https://www.kernel.org/doc/html/v5.0/core-api/boot-time-mm.html[2020/3/30 17:18:55]
Boot time memory management — The Linux Kernel documentation
Parameters
Return
Parameters
Return
Parameters
Return
https://www.kernel.org/doc/html/v5.0/core-api/boot-time-mm.html[2020/3/30 17:18:55]
Boot time memory management — The Linux Kernel documentation
Parameters
phys_addr_t start
start of candidate range
phys_addr_t end
end of candidate range, can be MEMBLOCK_ALLOC_ANYWHERE or
MEMBLOCK_ALLOC_ACCESSIBLE
phys_addr_t size
size of free area to find
phys_addr_t align
alignment of free area to find
int nid
nid of the free area to find, NUMA_NO_NODE for any node
enum memblock_flags flags
pick from blocks based on memory attributes
Description
Return
Parameters
phys_addr_t start
start of candidate range
phys_addr_t end
end of candidate range, can be MEMBLOCK_ALLOC_ANYWHERE or
MEMBLOCK_ALLOC_ACCESSIBLE
phys_addr_t size
size of free area to find
phys_addr_t align
alignment of free area to find
int nid
nid of the free area to find, NUMA_NO_NODE for any node
enum memblock_flags flags
pick from blocks based on memory attributes
Description
https://www.kernel.org/doc/html/v5.0/core-api/boot-time-mm.html[2020/3/30 17:18:55]
Boot time memory management — The Linux Kernel documentation
Return
Parameters
phys_addr_t size
size of free area to find
phys_addr_t align
alignment of free area to find
phys_addr_t start
start of candidate range
phys_addr_t end
end of candidate range, can be MEMBLOCK_ALLOC_ANYWHERE or
MEMBLOCK_ALLOC_ACCESSIBLE
int nid
nid of the free area to find, NUMA_NO_NODE for any node
enum memblock_flags flags
pick from blocks based on memory attributes
Description
Find size free area aligned to align in the specified range and node.
Return
https://www.kernel.org/doc/html/v5.0/core-api/boot-time-mm.html[2020/3/30 17:18:55]
Boot time memory management — The Linux Kernel documentation
Parameters
phys_addr_t start
start of candidate range
phys_addr_t end
end of candidate range, can be MEMBLOCK_ALLOC_ANYWHERE or
MEMBLOCK_ALLOC_ACCESSIBLE
phys_addr_t size
size of free area to find
phys_addr_t align
alignment of free area to find
Description
Return
void memblock_discard(void)
Parameters
void
no arguments
Parameters
Description
https://www.kernel.org/doc/html/v5.0/core-api/boot-time-mm.html[2020/3/30 17:18:55]
Boot time memory management — The Linux Kernel documentation
Double the size of the type regions array. If memblock is being used to
allocate memory for a new reserved
regions array and there is a previously
allocated memory range [new_area_start, new_area_start +
new_area_size]
waiting to be reserved, ensure the memory used by the new array does
not overlap.
Return
0 on success, -1 on failure.
Parameters
Description
Parameters
Description
Insert new memblock region [base, base + size) into type at idx.
type must already have extra room to
accommodate the new region.
https://www.kernel.org/doc/html/v5.0/core-api/boot-time-mm.html[2020/3/30 17:18:55]
Boot time memory management — The Linux Kernel documentation
Parameters
Description
Add new memblock region [base, base + size) into type. The new region
is allowed to overlap with existing
ones - overlaps don’t affect already
existing regions. type is guaranteed to be minimal (all neighbouring
compatible regions are merged) after the addition.
Return
Parameters
phys_addr_t base
base address of the new region
phys_addr_t size
size of the new region
int nid
nid of the new region
Description
https://www.kernel.org/doc/html/v5.0/core-api/boot-time-mm.html[2020/3/30 17:18:55]
Boot time memory management — The Linux Kernel documentation
Return
Parameters
phys_addr_t base
base address of the new region
phys_addr_t size
size of the new region
Description
Return
Parameters
Description
https://www.kernel.org/doc/html/v5.0/core-api/boot-time-mm.html[2020/3/30 17:18:55]
Boot time memory management — The Linux Kernel documentation
Walk type and ensure that regions don’t cross the boundaries defined by
[base, base + size). Crossing regions
are split at the boundaries,
which may create at most two more regions. The index of the first
region inside the
range is returned in *start_rgn and end in *end_rgn.
Return
Parameters
phys_addr_t base
phys starting address of the boot memory block
phys_addr_t size
size of the boot memory block in bytes
Description
Parameters
phys_addr_t base
base address of the region
phys_addr_t size
size of the region
int set
set or clear the flag
int flag
the flag to udpate
Description
This function isolates region [base, base + size), and sets/clears flag
Return
https://www.kernel.org/doc/html/v5.0/core-api/boot-time-mm.html[2020/3/30 17:18:55]
Boot time memory management — The Linux Kernel documentation
Parameters
phys_addr_t base
the base phys addr of the region
phys_addr_t size
the size of the region
Return
Parameters
phys_addr_t base
the base phys addr of the region
phys_addr_t size
the size of the region
Return
Parameters
phys_addr_t base
the base phys addr of the region
phys_addr_t size
the size of the region
Return
https://www.kernel.org/doc/html/v5.0/core-api/boot-time-mm.html[2020/3/30 17:18:55]
Boot time memory management — The Linux Kernel documentation
Parameters
phys_addr_t base
the base phys addr of the region
phys_addr_t size
the size of the region
Return
Parameters
phys_addr_t base
the base phys addr of the region
phys_addr_t size
the size of the region
Return
Parameters
u64 * idx
pointer to u64 loop variable
phys_addr_t * out_start
ptr to phys_addr_t for start address of the region, can be NULL
phys_addr_t * out_end
ptr to phys_addr_t for end address of the region, can be NULL
https://www.kernel.org/doc/html/v5.0/core-api/boot-time-mm.html[2020/3/30 17:18:55]
Boot time memory management — The Linux Kernel documentation
Description
Parameters
u64 * idx
pointer to u64 loop variable
int nid
node selector, NUMA_NO_NODE for all nodes
enum memblock_flags flags
pick from blocks based on memory attributes
struct memblock_type * type_a
pointer to memblock_type from where the range is taken
struct memblock_type * type_b
pointer to memblock_type which excludes memory from being taken
phys_addr_t * out_start
ptr to phys_addr_t for start address of the range, can be NULL
phys_addr_t * out_end
ptr to phys_addr_t for end address of the range, can be NULL
int * out_nid
ptr to int for nid of the range, can be NULL
Description
Find the first area from *idx which matches nid, fill the out
parameters, and update *idx for the next iteration.
The lower 32bit of
*idx contains index into type_a and the upper 32bit indexes the
areas before each region in
type_b. For example, if type_b regions
look like the following,
As both region arrays are sorted, the function advances the two indices
in lockstep and returns each
intersection.
https://www.kernel.org/doc/html/v5.0/core-api/boot-time-mm.html[2020/3/30 17:18:55]
Boot time memory management — The Linux Kernel documentation
memblock_type * type_a, struct memblock_type * type_b, phys_addr_t * out_start, phys_addr_t * out_end, int
* out_nid)
Parameters
u64 * idx
pointer to u64 loop variable
int nid
node selector, NUMA_NO_NODE for all nodes
enum memblock_flags flags
pick from blocks based on memory attributes
struct memblock_type * type_a
pointer to memblock_type from where the range is taken
struct memblock_type * type_b
pointer to memblock_type which excludes memory from being taken
phys_addr_t * out_start
ptr to phys_addr_t for start address of the range, can be NULL
phys_addr_t * out_end
ptr to phys_addr_t for end address of the range, can be NULL
int * out_nid
ptr to int for nid of the range, can be NULL
Description
Finds the next range from type_a which is not marked as unsuitable
in type_b.
Reverse of __next_mem_range() .
Parameters
phys_addr_t base
base of area to set node ID for
phys_addr_t size
size of area to set node ID for
struct memblock_type * type
memblock type to set node ID for
int nid
node ID to set
https://www.kernel.org/doc/html/v5.0/core-api/boot-time-mm.html[2020/3/30 17:18:55]
Boot time memory management — The Linux Kernel documentation
Description
Set the nid of memblock type regions in [base, base + size) to nid.
Regions which cross the area boundaries
are split as necessary.
Return
Parameters
phys_addr_t size
size of memory block to be allocated in bytes
phys_addr_t align
alignment of the region and block’s size
phys_addr_t min_addr
the lower bound of the memory region to allocate (phys address)
phys_addr_t max_addr
the upper bound of the memory region to allocate (phys address)
int nid
nid of the free area to find, NUMA_NO_NODE for any node
Description
The min_addr limit is dropped if it can not be satisfied and the allocation
will fall back to memory below
min_addr. Also, allocation may fall back
to any node in the system if the specified node can not
hold the
requested memory.
The phys address of allocated boot memory block is converted to virtual and
allocated memory is reset to 0.
Return
https://www.kernel.org/doc/html/v5.0/core-api/boot-time-mm.html[2020/3/30 17:18:55]
Boot time memory management — The Linux Kernel documentation
allocate boot memory block without zeroing memory and without panicking
Parameters
phys_addr_t size
size of memory block to be allocated in bytes
phys_addr_t align
alignment of the region and block’s size
phys_addr_t min_addr
the lower bound of the memory region from where the allocation
is preferred (phys address)
phys_addr_t max_addr
the upper bound of the memory region from where the allocation
is preferred (phys address), or
MEMBLOCK_ALLOC_ACCESSIBLE to
allocate only from memory limited by memblock.current_limit value
int nid
nid of the free area to find, NUMA_NO_NODE for any node
Description
Return
Parameters
phys_addr_t size
size of memory block to be allocated in bytes
phys_addr_t align
alignment of the region and block’s size
phys_addr_t min_addr
the lower bound of the memory region from where the allocation
is preferred (phys address)
phys_addr_t max_addr
the upper bound of the memory region from where the allocation
is preferred (phys address), or
MEMBLOCK_ALLOC_ACCESSIBLE to
allocate only from memory limited by memblock.current_limit value
https://www.kernel.org/doc/html/v5.0/core-api/boot-time-mm.html[2020/3/30 17:18:55]
Boot time memory management — The Linux Kernel documentation
int nid
nid of the free area to find, NUMA_NO_NODE for any node
Description
Return
Parameters
phys_addr_t size
size of memory block to be allocated in bytes
phys_addr_t align
alignment of the region and block’s size
phys_addr_t min_addr
the lower bound of the memory region from where the allocation
is preferred (phys address)
phys_addr_t max_addr
the upper bound of the memory region from where the allocation
is preferred (phys address), or
MEMBLOCK_ALLOC_ACCESSIBLE to
allocate only from memory limited by memblock.current_limit value
int nid
nid of the free area to find, NUMA_NO_NODE for any node
Description
Return
Parameters
https://www.kernel.org/doc/html/v5.0/core-api/boot-time-mm.html[2020/3/30 17:18:55]
Boot time memory management — The Linux Kernel documentation
phys_addr_t base
phys starting address of the boot memory block
phys_addr_t size
size of the boot memory block in bytes
Description
This is only useful when the bootmem allocator has already been torn
down, but we are still initializing the
system. Pages are released directly
to the buddy allocator, no bootmem metadata is updated because it is gone.
Parameters
phys_addr_t base
base of region to check
phys_addr_t size
size of region to check
Description
Return
Parameters
phys_addr_t base
base of region to check
phys_addr_t size
size of region to check
Description
https://www.kernel.org/doc/html/v5.0/core-api/boot-time-mm.html[2020/3/30 17:18:55]
Boot time memory management — The Linux Kernel documentation
Return
Parameters
void
no arguments
Return
Previous Next
https://www.kernel.org/doc/html/v5.0/core-api/boot-time-mm.html[2020/3/30 17:18:55]