I recently read a paper, and I think it is better to explain the linux memory management, so I made some notes.
User space:
1. Use the malloc () function to allocate memory on the process stack.
2. the Malloc () function actually calls the brk () System Call to allocate memory. The function is used to expand the page size or reduce the heap space, but the allocation is still in bytes, therefore, the malloc () function can dynamically allocate the memory size.
Physical memory management:
1. Paging mechanism (reducing external fragments)
2. In order to allocate continuous memory and reduce the TLB refreshing rate, the "partner System" is used to manage the physical memory (page ).
3. The partner system provides two functions for allocating physical memory: get_free_page () and get_free_pages (). The former only allocates a single page, and the latter allocates 2n physical pages.
4. in linux, each page of physical memory is described by struct page. All physical pages are stored in a Global Array in mem_map []. All physical pages can be found through this array.
Slab:
1. It is actually a memory pool allocated in advance, and the memory inside is reused to avoid overhead caused by frequent creation and destruction.
2. Built on the partner system, the hardware cache can be used to improve access speed.
3. The physical pages raised by the partner system are divided into a large number of small memory blocks for allocation, to meet the kernel's frequent allocation of small memory smaller than one page (reduce internal fragments ).
4. The kmem_cache_alloc () and kmem_cache_free () functions allocate and destroy memory for dedicated kernel objects (fixed size and struct.
5. kmalloc () allocates small memory blocks. The memory allocation granularity is more flexible, and 32-bit memory can be allocated ~ Memory in the range of 131072 (128 K); the space allocated by the kmalloc () function is called the kernel logic address. Because it is continuously allocated, and the first address is certain, therefore, you only need to subtract a fixed offset (page_offset) from the physical address ing. The corresponding page table belongs to the kernel page table (swapper_pg_dir) and has been created during system initialization.
6. Slab not only reserves the memory for the above two types of memory allocation, but also prepares some memory for the DMA.
Kernel non-continuous memory allocation (Vmalloc ):
1. Integrate non-contiguous memory blocks (pages) into large memory blocks for allocation (completely eliminating external fragments ).
2. drawing on the virtual memory allocated by user space, the memory is logically continuous, but in fact the physical memory is not necessarily continuous; similar to the user process, the kernel also has a mm_struct structure named init_mm to describe the kernel address space. The page table entry PDG = swapper_pg_dir contains the ing relationship between the system kernel space.
3. A large amount of memory space can be allocated, which can be nearly 1 GB (several hundred MB) at the maximum. However, the kernel virtual address needs to be re-mapped to update the kernel table, so the allocation efficiency is somewhat lower.
Comparison:
1. the kernel Virtual Memory allocated by vmalloc () and get_free_page ()/kmalloc () is located in different intervals. The continuous memory allocated by get_free_page () and kmalloc () is located in the physical ing area, and the memory allocated by vmalloc () is the high address area after the physical ing area. Shows the location distribution:
2.The main difference between kmalloc () and kmem_cache_alloc () is that the former can allocate dynamic memory, while the latter cannot. It can only allocate a fixed size of memory. If the memory size (constant) is fixed, the kmalloc () function calls the kmem_cache_alloc () function for implementation, so there is basically no difference. If the memory size changes, you can only select kmalloc. In general, kmem_cache_alloc () is more efficient than kmalloc.
Appendix 1 user space and kernel space memory ing schematic