Memory Management in the Linux kernel is not as simple as outside the kernel. The biggest difference from user space is that the kernel's internal slave space is not as easy as the user space, and it is not always easy to get the desired memory.
Page:
The most basic Memory Management Unit of the kernel is page, because the memory basic unit of MMU management is page, which maintains a page table that provides conversion from virtual addresses to physical addresses.
The kernel uses the following data structure to describe page:
Struct page {
Unsigned long flags; // The status of the page, eg if the page is dirty, is locked
Atomic_t _ count; // The references to this page
Atomic_t _ mapcount;
Unsigned long private;
Struct address_space * mapping;
Pgoff_t index;
Struct list_head LRU;
Void * virtual; // the page's virtual space
};
Note that the page Data Structure describes all the physical memory on the machine and cares about the specific data on the page. It only cares about whether the page is idle or who occupies the page.
ZONE:
The kernel divides the entire memory into three zones ).
Zone_dma-memory space for DMA, which is less than 16 Mb in x86
Zone_nomal -- 16m-896m space in x86, which is permanently mapped to the kernel address space
Zone_highmem -- dynamically mapped address space
Why partition? In some systems (such as x86), only part of the memory can be directly mapped, And the x86 memory above M is dynamically mapped, so the kernel description of the memory partition is necessary, a DMA partition is the memory space that can perform DMA operations.
Kernel page allocation interface:
The kernel provides several page-based memory allocation functions.
Struct page * alloc_pages (gfp_t gfp_mask, unsigned int order );
Allocate order 2 to the power of a consecutive page and return a pointer to the first page. Of course, in practice, we generally operate on the virtual address of the allocated memory.
Void * page_address (struct page * page );
Returns the logical address pointer of the page.
Of course, to make it easier for the kernel to provide functions that directly return virtual address pointers, these two functions are actually packaged:
Unsigned long _ get_free_pages (gfp_t gfp_mask, unsigned int order );
There is also a function for directly allocating a single page, which is actually the version with the order parameter 0:
Struct page * alloc_page (gfp_t gfp_mask );
Unsigned long _ get_free_page (gfp_t gfp_mask );
If all the pages you want to obtain are initialized to 0, the kernel provides this call:
Get_zeroed_page (gfp_mask );
It works in the same way as _ get_free_pages, except that this function initializes the kernel space to 0.
These functions have corresponding page release functions. The prototype is as follows:
Void _ free_pages (struct page * Page, unsigned int order );
Void free_pages (unsigned long ADDR, unsigned int order );
Void free_page (unsigned long ADDR );
More common allocation interfaces:
In addition to the original page-based memory allocation function, the kernel also provides a more convenient allocation interface similar to the C language interface, the most commonly used is the kmalloc function:
Void * kmalloc (size_t size, gfp_t flags );
This interface returns at least size bytes of physical continuous memory space. The usage is the same as that of the C language malloc function.
Of course, there are also functions similar to the free memory release function.
Void kfree (const void * PTR );
The kernel also provides vmalloc functions in the same usage as kmalloc. The unique vmalloc does not guarantee that the allocated memory is physically continuous.
How to choose which allocation function to use? In most cases, the memory we need to allocate does not need to be physically contiguous. In addition to the memory for peripheral operations, memory continuity is required because many peripherals do not have the concept of logical memory, he can only view and operate the physical memory. In other cases, we do not need to use continuous physical memory. However, in most cases, the kernel still uses kmalloc to allocate memory, which is basically based on efficiency considerations. Because vmalloc calls have many operations attached to the page table in order to make the Logical Address of the discontinuous physical memory consecutively, in addition to the need (a considerable memory space is required), the kmalloc function is generally used directly.
Vmalloc also has vfree functions.
Gfp_mask parameters:
The gfp_mask parameter can be set with many values. Here is the usefulness of each value (directly referenced to lkd ):
Gfp_atomic the allocation is high priority and must not sleep. This is the flag to use in interrupt handlers, in bottom halves, while holding a spinlock, and in other situations where you cannot sleep.
Gfp_nowait like gfp_atomic, doesn't that the call will not fallback on emergency memory pools. This increases the liklihood of the memory allocation failing.
Gfp_noio this allocation can block, but must not initiate disk I/O. this is the flag to use in block I/O Code when you cannot cause more disk I/O, which might lead to some unpleasant recursion.
Gfp_nofs this allocation can block and can initiate disk I/O, if it must, but it will not initiate a filesystem operation. this is the flag to use in filesystem code when you cannot start another filesystem operation.
Gfp_kernel this is a normal allocation and might block. this is the flag to use in process context code when it is safe to sleep. the kernel will do whatever it has to do to obtain the memory requested by the caller. this flag shocould be
Your default choice.
Gfp_user this is a normal allocation and might block. This flag is used to allocate memory for user-space processes.
Gfp_highuser this is an allocation from zone_highmem and might block. This flag is used to allocate memory for user-space processes.
Gfp_dma this is an allocation from zone_dma. device drivers that need DMA-able memory use this flag, usually in combination with one of the preceding flags.
Use slab cache allocation:
To avoid overhead of memory allocation and release for specific data structures, the Linux Kernel provides a powerful memory cache allocation policy, namely, slab cache allocation. (In fact, The kmalloc function is built on the slab distributor !)
Each slab contains a certain number of objects, that is, the data structure to be cached. Each slab has three states: full, partial, and empty. When you try to allocate an object from slab, the kernel first obtains the object from the slab of partial. If not, it creates a new slab from empty slab. In the kernel, the components for managing slab cache mainly include cache, slab, and object.
The cache is described in the kernel using the kmem_cache structure, which contains three linked lists: slabs_full, slabs_partial, and slabs_empty. These lists contain slab in the corresponding state. The slab's description in the kernel is as follows:
Struct slab {
Struct list_head list;/* full, partial, or empty list */
Unsigned long colouroff;/* offset for the slab coloring */
Void * s_mem;/* first object in the slab */
Unsigned int inuse;/* allocated objects in the slab */
Kmem_bufctl_t free;/* First free object, if any */
};
The following functions can create a new cache:
Struct kmem_cache * kmem_cache_create (const char * Name, size_t size,
Size_t align, unsigned long flags, void (* ctor) (void *));
The parameters are described as follows:
Const char * Name: name of the cache.
Size_t size: the size of the object to be cached in the cache.
Size_t align: the offset of the first object in slab. Generally, it can be 0. Standard alignment is used.
Unsigned long flags: You can execute some additional operations for cache allocation. If not, you can directly perform 0 operations.
Void (* ctor) (void *): the cache constructor. You can assign a function address. When a new page is added to the cache, the constructor will be executed. The value can be null.
Used to destroy a cache
Int kmem_cache_destroy (struct kmem_cache * cachu );
The kernel provides two functions to obtain and return memory from the specified cache:
Void * kmem_cache_alloc (struct kmem_cache * cache, gfp_t flags );
Void kmem_cache_free (struct kmem_cache * cache, void * objp );