Memory allocation is a more complex and important part of Linux, similar to SSD drivers: the mapping of physical and virtual addresses. The following summarizes the most recently seen about memory allocations and their own understanding of the content;
1. Consistent memory access and non-uniform memory access
From the deep Linux device driver kernel mechanism
Simply stated, UMA (consistent memory access uniform memories Access) can be very good to see all the CPU access memory distance is the same (in fact, through the bus to the memory speed and distance are the same) so called consistent memory access;
It is obvious that NUMA on the right is a non-uniform memory access, memory node 0 is CPU0 local memory (although other CPUs can also be accessed, but no CPU0 is high), so each CPU access memory node will first select the local memory node, and then consider the other memory nodes;
2. Physical memory and virtual address
From the deep Linux device driver kernel mechanism
First of all, you can see that the left Mem_map array is the page structure element, the middle is the actual physical memory, the right is the virtual address range;
The relationship between them is probably like this, mem_map the elements in the array and the physical memory page, the page pointer points to a physical page in the middle of the physical memory, the relationship is for the system to facilitate the management of memory;
and the physical memory page and the right virtual address range mapping, this is to operate the physical memory, the general right of the virtual address is used by the CPU (the software is also used on the virtual address, but the address is ultimately CPU use). the CPU and the MMU are used between the virtual address, out of the MMU after the conversion to the actual physical memory address, generally two places to use: 1, on the bus, 2, physical memory (can be understood as memory bar);
Here's an analogy to the SSD-driven principle: The FTL array holds PBA (LUN, block, page, offset), and the array subscript LBA,PBA is the actual physical address on the SSD, and LBA is the logical address used by the upper-level software; they are mapped by an FTL array. This can be achieved by modifying the array of PBA to achieve SSD like hard disk, such as overwrite the performance of the hard disk;
Back to the memory allocation, you should see from the above figure that physical memory is divided into three areas: ZONE_DMA, Zone_normal, zone_highmem;mem_map arrays, and physical memory pages all one by one mappings, and also form three regions, while during system initialization, The physical memory area in the ZONE_DMA and Zone_normal respectively and the right virtual address "physical page Direct Mapping area" (This virtual address area name is a bit strange, is actually the kernel address of the 0~896m address range) to establish a linear mapping, so that the corresponding page directory and page table If you have questions about this virtual address range assignment on the right, you can look at: http://blog.csdn.net/yuzhihui_no1/article/details/46982231, but these are all in a 32-bit system, The 64-bit system can be said to be completely different, but this concept will be better;
At this time, if the kernel memory ZONE_DMA and Zone_normal region allocation, you can directly return the virtual address (0~896m) the limit of the virtual address is not fixed , if you need to allocate the physical memory Zone_highmem, it is more complicated. The simple steps are : 1, find a free page in the physical memory Zone_highmem, 2, the dynamic mapping area or fixed map area in the virtual address on the right is assigned a virtual address, 3, set up the page directory and the page table, make the physical page and the virtual address map;
3. Physical page Allocation interface function
The first is: Alloc_pages (gfp_mask, order) macro, (it is not very strange not to write Kmalloc, Vmalloc, it is because they are more complex, but also more important, of course, the main thing is that they are not allocated physical pages. There is a lot of space to analyze the two of them); The parameter gfp_mask is a mask parameter that controls the allocation behavior (which area is allocated, whether the allocation is blocked, etc.); Order is a physical page that represents an order of 2 allocation;
Alloc_pages (gfp_mask, order); Call Alloc_pages_node (); Call __alloc_pages (); At the end of the __alloc_pages (),alloc_pages () and __alloc_pages () can allocate physical pages from the Zone_highmem area . Mainly see Gfp_mask assigned area; note Alloc_pages () is a macro is not a function;
__get_free_pages (Gfp_mask, order), this allocation function first is to determine the Gfp_mask, if specified as the Zone_highmem allocation area, the failure exits; otherwise, the alloc_pages () function is called; the __get_free_pages () function can only allocate the physical pages of the ZONE_DMA and zone_normal areas, which means that there is no need to modify the page table ;
Simple allocation of physical page interface functions of these two, the others are variants, according to Gfp_mask, and order to implement the VARIANT function;
Note: this is a function interface that allocates multiple contiguous physical pages , and the Kmalloc, vmalloc functions do not necessarily allocate memory for the entire page size;
Reprint Address: http://blog.csdn.net/yuzhihui_no1/article/details/47284329
If the explanation is not correct, please correct me, thank you!!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Linux kernel memory allocation (i. BASIC concept)