[Linux kernel memory management] memblock allocator programming interface ③ ( memblock_remove function | memblock_remove_range function )


The memblock allocator provides the following programming interfaces:

① Add memory: memblock_add function, add the memory block area to the memblock.memorymember , 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_addfunction , and this blog started to introduce the memblock_removefunction;





1. Analysis of memblock_remove function



memblock_removeThe 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 baseThe parameter indicates the starting ;

  • phys_addr_t sizeThe parameter indicates the size ;


memblock_removeThe 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

insert image description here

In the above memblock_removefunction , 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_rangefunction ,


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_rangefunction 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_regionfunction delete the physical memory area of ​​the specified interval;

First record the index numbers of overlapping memory areas, call the memblock_remove_regionfunction , 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_rangeFunction parameter role:

  • struct memblock_type *typeThe parameter points to the memory area to delete the physical memory;
  • phys_addr_t baseThe parameter indicates the starting ;
  • phys_addr_t sizeThe parameter indicates the size ;

3. Memblock_remove_range function source code


memblock_remove_rangeThe function is defined in the linux-4.12\mm\memblock.c #689 location of the Linux kernel source code ;

memblock_remove_rangeThe 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

Guess you like

Origin blog.csdn.net/han1202012/article/details/124293877