Three-page alloc and free, Buddy algorithm and CMA

Whole catalog

1. Basic principles of Linux virtual memory, MMU, and paging
2. OOM scoring factor, oom_adj and oom_score
three pages of alloc and free, Buddy algorithm and CMA
four page_fault, memory IO interaction, VSS, LRU
five DMA and Cache consistency

=================================================================================

Page alloc and free

Reprinted from the in- depth explanation of memory management-analysis of alloc_pages and free_page (block diagram)
alloc_pages is used for the allocation of continuous physical memory. Its implementation is as shown in the figure below.
Insert picture description here
From this flowchart, the function is called step by step to buffered_rmqueue, and alloc_pages is the page allocator. Many modules and drivers in the system can directly call it to apply for memory, and buffered_rmqueue is the implementation of the underlying partner system algorithm. Its flow chart is as follows:
Insert picture description here
This block diagram introduces the implementation of buffered_rmqueue. From its naming, it can be seen that it has a buffer function. Why do you want to do this? We know that the buddy system algorithm is somewhat complicated For a high degree, if you have to execute an algorithm to scan the memory in the system every time you apply, it will be time-consuming, so add per cpu cache. When we apply for a single page, we will get it from the cache first, which will improve Efficiency, at the same time as a per cpu buffer, will also improve CPU efficiency, because there is no need to switch the cache frequently, which improves the probability of cache hits.
Free page is the interface to release the page, and its implementation is as follows: the
Insert picture description here
release process is the reverse process of the application. When a single page is released, it will be released to the per cpu high-speed buffer management first. If the memory size in the cache exceeds a set value , Then the cache will also release part of it to the partner system, and the partner system releases the memory to the corresponding free free_area by executing __free_one_page cyclically.

=============================================================

Buddy algorithm

Insert picture description here
Insert picture description here
The specific content of buddy algorithm can be found in Linux memory management-buddy algorithm .

=============================================================

Contiguous Memory Allocator (CMA)

CMA, Contiguous Memory Allocator, is a module in the memory management subsystem, responsible for the allocation of continuous physical addresses. Generally, the system will configure a segment of continuous memory from the entire memory for CMA during the startup process, and then other modules of the kernel can allocate continuous memory through the CMA interface API. The core of CMA is not to design sophisticated algorithms to manage memory blocks with continuous addresses. In fact, its bottom layer still relies on memory management mechanisms such as the kernel partner system, or CMA is in other kernel modules that require continuous memory blocks (such as DMA mapping A middle layer module between the framework) and the memory management module. The main functions include:
1. Parse the parameters in the DTS or command line to determine the area of ​​the CMA memory. This area is defined as the CMA area.
2. Provide cma_alloc and cma_release two interface functions for allocating and releasing CMA pages.
3. Record and track the status of each page in the CMA area.
4. Call the partner system interface for real memory allocation.
CMA usually allocates memory to the application layer for use. When DMA applies for continuous memory, page copy is performed, free pages are searched for in the memory, and pages requested by the application layer in the DMA zone are copied to other free pages, and then they can be sorted out A piece of continuous physical memory.
You can view the study notes of Wowo Technology's CMA module .

Guess you like

Origin blog.csdn.net/baidu_38410526/article/details/104107436