This is the sixth article in the<Linux kernel memory management>series
Part I Simple sorting of knowledge points for kernel memory management process
Part II Introduced the data structure of the kernel
Part III This article describes the memory processing from the first line of code loading to the C code jump.
Chapter IV Overview of memory processing in initialization C code
Chapter 5 (I) and Chapter 5 (Next) Introduces Memblock and partner system allocator
In order to avoid obscurity, this article and the following mainly use charts+text descriptions, and try to avoid involving too much code. Focus will be on:
background Architecture and ideas technological process Special treatment and reasons
preface
-
SLOB allocator is designed to meet the special requirements of embedded device memory management -
SLUB is based on SLAB, which can well meet the needs of various platforms, use memory more effectively, and enhance the ease of debugging.
-
The partner system is managed in pages, and the size of each page is generally 4096 bytes. When the kernel program applies for memory, it often does not just apply for a multiple of the page size. If we allocate by page, the system memory will soon be exhausted. -
For these reasons, memory must be managed in smaller units. This needs to consider the memory fragmentation caused by frequent memory allocation and release. At the same time, it is necessary to consider how to use CPU cache more effectively, and try to avoid the static caused by accessing the same memory area.
Kernel Configuration
|
|
KConfig related knowledge can be referred to KConfig Language
framework
-
The largest management unit of the memory management system is Node, which is divided into multiple memory zones( ToDo: Buddy System in the previous chapter and supplementary pictures in the second chapter )。 -
During the page allocation (remember the page allocation? Please refer to Chapter 5 (I) and Chapter 5 (Next) yes Buddy System ), which memory zone to allocate memory from according to the parameters passed in. -
The allocation of SLAB requires the kmem_cache management structure, and the memory required by these management structures is also from the kmem_cache. Here the kernel Made a very clever design : -
The initial management objects of the slab required to create the kmem_cache slab are boot_kmem_cache and boot_kmem_cache_node. They are decorated by the __init attribute, indicating that they will be placed in the. init.data section and released in the second half of kernel initialization. -
During the initialization of the SLAB system, the global SLAB objects kmem_cache and kmem_cache_node are allocated from the SLAB pointed to by boot_kmem_cache and boot_kmem_cache_node. -
Then copy the contents of boot_kmem_cache and boot_kmem_cache_node to kmem_cache and kmem_cache_node. -
So far, the global SLAB objects kmem_cache and kmem_cache_node are used for SLUB management.
-
-
The memory of kmalloc is also allocated early in kernel initialization. In essence, SLAB objects with sizes of 2, 4, 8,... are created.
source file
|
|
---|---|
|
|
|
|
|
|
|
|
|
|
data structure
-
Kmem_cache represents a SLAB object -
Kmem_cache_cpu stores the local resources of the SLAB object in the CPU. Here __percpu The decoration indicates that this is a Per CPU object (each CPU has a copy) -
Kmem_cache_node is an array. Each array member represents the SLAB object in each Memory node Memory resources for.
management style
-
Each SLUB management structure has multiple cpu local slabs and node slabs. -
When SLUB was first established, there was only a corresponding management structure. -
When SLUB allocates memory -
If no page is available in the object at this time, allocate the page from the partner system, hang it on the cpu local slab, and return a required memory. -
If there are pages available in the object at this time, memory is allocated from them. -
If the current kmem_cache_cpu has no available pages (the freelist and partial pages of kmem_cache_cpu are full), allocate memory from the partial of kmem_cache_node
-
This processing can ensure that the cache area of the CPU is always allocated first, and improve the access speed of resources.
-
Free memory: the memory will be released to the page where the memory is located first. The release occurs in the following cases:
|
|
---|---|
|
|
|
|
|
|
|
|
The main purpose of setting the threshold is to avoid SLAB occupying too many memory pages, which may cause other objects in the system unable to get memory when they want to allocate memory.
summary
-
Classification of SLAB distributors -
Architecture of SLUB allocator -
Logic principle of SLUB allocator