Article directory
The memblock allocator provides the following programming interfaces:
① Add memory: memblock_add
function, add the memory block area to the memblock.memory
member , that is, insert a piece of available physical memory;
② Delete memory: memblock_remove
function, delete the memory block area;
③ Allocate memory: memblock_alloc
function, apply for memory allocation;
④ Release memory: memblock_free
function, release the memory allocated before;
In the previous blog, the memblock_add
function , and this blog started to introduce the memblock_remove
function;
1. Analysis of memblock_remove function
memblock_remove
The function of the function is to delete a piece of "available physical memory area " from the "available physical memory area";
The function has 2 22 parameters:
-
phys_addr_t base
The parameter indicates the starting ; -
phys_addr_t size
The parameter indicates the size ;
memblock_remove
The function is defined in linux-4.12\mm\memblock.c #704 , and the function source code is as follows:
int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
{
return memblock_remove_range(&memblock.memory, base, size);
}
Source code path: linux-4.12\mm\memblock.c #511
In the above memblock_remove
function , the function is called memblock_remove_range
;
2. Analysis of memblock_remove_range function
1. The execution flow of the memblock_remove_range function
In the memblock_remove_range
function ,
First, calculate the termination address of the physical memory area to be deleted,
If the memory area to be deleted may overlap with the memory block in the memory area,
Call the memblock_isolate_range
function to separate the overlapping part from the memory area;
ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
if (ret)
return ret;
Source code path: linux-4.12\mm\memblock.c #695
Then, call the memblock_remove_region
function delete the physical memory area of the specified interval;
First record the index numbers of overlapping memory areas, call the memblock_remove_region
function , and delete the memory areas corresponding to these index numbers;
for (i = end_rgn - 1; i >= start_rgn; i--)
memblock_remove_region(type, i);
Source code path: linux-4.12\mm\memblock.c #699
2. Introduction to the parameters of the memblock_remove_range function
memblock_remove_range
Function parameter role:
struct memblock_type *type
The parameter points to the memory area to delete the physical memory;phys_addr_t base
The parameter indicates the starting ;phys_addr_t size
The parameter indicates the size ;
3. Memblock_remove_range function source code
memblock_remove_range
The function is defined in the linux-4.12\mm\memblock.c #689 location of the Linux kernel source code ;
memblock_remove_range
The function source code is as follows:
static int __init_memblock memblock_remove_range(struct memblock_type *type,
phys_addr_t base, phys_addr_t size)
{
int start_rgn, end_rgn;
int i, ret;
ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
if (ret)
return ret;
for (i = end_rgn - 1; i >= start_rgn; i--)
memblock_remove_region(type, i);
return 0;
}
Source code path: linux-4.12\mm\memblock.c #689