This article will briefly describe the partner assignment algorithms in the Linux kernel.
Technorati Tags: partner algorithm algorithm function
The problem is that the frequent request and release of a set of contiguous page boxes of different sizes will inevitably result in a large number of free pages scattered within the block of the allocated page box, and the problem is that even if there are enough free page boxes to satisfy the request, allocating a large contiguous page box may not satisfy the request.
The partner algorithm (Buddy system) divides all the free page frames into 11 block lists, each of which contains a specific contiguous page box address space, such as a NO. 0 block list containing 2^0 contiguous page boxes, a 1th block list, each linked list element containing 2 page box-size contiguous address spaces .... In the 10th block list, each list element represents a contiguous address space of 4M. The number of elements in each list is determined during system initialization, and is dynamically changed during execution.
The partner algorithm can allocate only 2 of the Power page space at a time, such as allocating 1 pages at a time, 2 pages, 4 pages, 8 pages, ..., 1024 pages (2^10), and so on, each page is typically 4K in size, so the partner algorithm can allocate up to 4M of memory space at a time.
Core concepts and data structures
Two memory blocks, same size, address contiguous, belong to a large area. (Blocks No. 0 and 1th are partners, 2nd and 3rd blocks are partners, but 1th blocks and 2nd blocks are not partners)
Partner bitmap: A status code that describes the partner block, called the partner bit code. For example, Bit0 is the No. 0 and 1th partner bit code, if the BIT0 is 1, indicating that the two pieces of at least one has been allocated, if the bit0 is 0, two is free, not allocated.
LINUX2.6 uses different partner systems for each management area, the kernel space is divided into three zones, Dma,normal,highmem, for each zone, there is a partner algorithm for
1. Free_area array:
1: struct zone{
2: ... .
3: struct free_area Free_area[max_order];
4: ... .
5: }
struct Free_area Free_area[max_order] #MAX_ORDER default value is 11
2. Zone_mem_map Array
The Free_area array, the K-element, identifies all free blocks of size 2^k, all of which are free to be organized by the two-way circular list that the free_list points to. The Nr_free, which specifies the number of remaining blocks in the corresponding space.
The entire allocation diagram, probably as follows:
Application and recycling process
For example, I want to allocate 4 (2^2) page (16k) of memory space, the algorithm will first see whether the nr_free is empty from free_area[2], if there is free block, then allocate from it, if there is no free block, from its upper level free_area[3] (32K per block), and add the extra memory (16K) to free_area[2]. If FREE_AREA[3] is not available, then the allocation failure is reported from a higher level of application space, recursion until Free_area[max_order], if there is no space at the top level.
Release is the reverse process of the application, when a block of memory is released, first in its Free_area list for the existence of a partner, if there is no partner block, the freed block is inserted directly into the list header. If there is or a plate exists, it is removed from the list, merged into a large chunk, and then continues to find out whether the merged block has a partner presence in the larger list of links until it cannot be merged or has been merged to the maximum block 2^10.
The kernel tries to combine a pair of free blocks of size B (one that is on an existing idle list and one to be recycled) into a single block of size 2B, and if it successfully merges the freed block, it tries to merge blocks of 2b size.
The kernel uses the _rmqueue () function to find a free block in the admin area, successfully returning the page descriptor of the first assigned page box, and failing to return null.
Kernel use
The __free_pages_bulk () function releases the page box according to the policy of the partner system. It uses 3 basic input parameters:
Page: The address of the first page box descriptor contained in the freed block.
Zone: Address of the admin area descriptor.
Order: The logarithm of the block size.
Advantages and disadvantages of the partner algorithm
Advantages:
Better solution to external debris problems
When a number of memory pages need to be allocated, the memory pages used for DMA must be contiguous, and the partner algorithm is well satisfied with this requirement
As long as the requested block does not exceed 512 pages (2K), the kernel allocates contiguous pages as much as possible.
Designed for large memory allocations.
Disadvantages:
1. The requirements for consolidation are too stringent to be consolidated only by the blocks that satisfy the partnership, such as the 1th and 2nd blocks.
2. Fragmentation problem: A contiguous memory in which only one page is occupied, resulting in the entire block of memory not having a merge condition
3. Waste problem: The partner algorithm can only allocate 2 power of the memory area, when the need for 8K (2 pages), say, when the need for 9K, it will need to allocate 16K (4 pages) of memory space, but the actual use of only 9K space, the extra 7K space is wasted.
4. Efficiency of the algorithm problem: The partner algorithm involves more computation and the operation of the list and bitmap, the cost is relatively large, if each 2^n size of the partner block will be merged into the 2^ (n+1) of the linked list queue, then the 2^n size list of the block will be reduced because of the merge operation, However, the system is then immediately likely to have a demand for that size block, which must then be split from the 2^ (n+1)-sized list, so that the process of merging and splitting it immediately is inefficient.
Linux for the physical address allocation of large memory, using the partner algorithm, if it is less than a page of memory, frequent allocation and release, there are more appropriate solutions, such as Slab and Kmem_cache, which is not within the scope of this article.
Reference Link: Anatomy of a Partner algorithm (original)
Partner system Algorithms
Buddy Partner algorithm
Introduction to the Linux partner algorithm