A summary of the Linux memory allocation method--a good summary __linux

Source: Internet
Author: User
Tags data structures

Turn from: http://blog.chinaunix.net/uid-22028680-id-3968085.html

Turn from: http://www.cnblogs.com/wenhuisun/archive/2013/05/15/3079722.html
Memory-Mapped structure:

The 1.32-bit address line addresses 4G of memory space, where 0-3g is unique to the user program and 3g-4g is possessed by the kernel.

2.struct page: When the entire physical memory is initialized, each 4KB page generates a corresponding struct page structure that uniquely represents the physical memory page and is stored in the Mem_map global array.

3. Segment mapping: First, according to the code segment to select the CS index, the GDT value as the starting address of the Segment Description table to select the corresponding segment descriptor, and then according to the paragraph descriptor base, this paragraph length, permission information, etc. for verification, after the successful verification. The 32-bit offset in the cs:offset is added directly to the base address of this segment, and the final access addresses are obtained.

How 0-3g and Mem_map are mapped:
Because the segment mapping used in Linux is flat mode, there is no change from the logical address to the linear address.  From paragraph to page, each user process has a single page catalog table (PDT), which is stored in the CR3 at run time. CR3 (page catalog) + Top 10 => Page Table base + 10-bit => Page table entry + 12-bit => physical page address

How 3g-4g and Mem_map are mapped:
Divided into three types: low memory/normal memory/high-end memory.
Low-end Memory: 3g-3g+16m for DMA __PA linear mappings
Normal Memory: 3g+16m-3g+896m __PA Linear mapping (if physical memory <896m, then the dividing point is 3g+ actual memory)
High-end Memory: 3g+896-4g using dynamic distribution method

4. High-end memory (assuming 3g+896 is a high-end memory address)
Function: Access to physical memory space other than 1G.
Linear address is divided into three segments: Vmalloc segment/kmap segment/kmap_atomic segment (for with different memory allocation method)


The structure of the memory allocation function is mainly divided into the following sections:
A. Partner algorithms (most original page-oriented distribution)
Alloc_pages Interface:
struct page * alloc_page (unsigned int gfp_mask)--allocates a page of physical memory and returns the page structure pointer to the physical memory of the pages.
struct page * alloc_pages (unsigned int gfp_mask, unsigned int order)-Assigns a contiguous physical page and returns the page structure pointer for the first physical page assigned.
< release function: __free_page (s) >

Kernel definition: #define ALLOC_PAGE (Gfp_mask) alloc_pages (gfp_mask, 0)
The final call is __alloc_pages.
Where Max_order 11, and the maximum number of pages allocated to 2^10 (that is, 4M).
After the allocation page is not directly used, you need to get the corresponding virtual address of the page:
void *page_address (struct page *page);
Low-end Memory mapping: __va (unsigned long) (PAGE-MEM_MAP) << 12)
High-end memory to map: struct PAGE_ADDRESS_MAP Allocates a dynamic structure to manage high-end memory. (The kernel is a virtual address that does not have access to the VMA 3G) the specific mapping is performed by Kmap/kmap_atomic.

Get_free_page interface: (Alloc_pages interface Two-step substitution function)
unsigned long get_free_page (unsigned int gfp_mask)
unsigned long __get_free_page (unsigned int gfp_mask)
Unsigned long __get_free_pages (Unsigned int gfp_mask, Unsigned int order)
< release function:free_page>
The biggest difference with the Alloc_page (s) series is the inability to request high-end memory because it returns to a linear address, and high-end memory is required for additional mapping to be accessible.

B.slab Cache (repeatedly allocating many of the same size memory) Note: less use
kmem_cache_t* Xx_cache;
Created: Xx_cache = kmem_cache_create ("name", sizeof (struct xx), slab_hwcache_align, NULL, NULL);
Distribution: Kmem_cache_alloc (Xx_cache, Gfp_kernel);
Release: Kmem_cache_free (Xx_cache, addr);
Memory pool
Mempool is not used.

C.kmalloc (most commonly used allocation interface) Note: must be less than 128KB
Gfp_atomic no sleep, for interrupt handling, etc.
Gfp_kernel will hibernate, general condition Use this tag
Gfp_user will sleep
__GFP_DMA Allocating DMA Memory
Kmalloc/kfree

D.vmalloc/vfree
Vmalloc uses high memory reserved virtual space to collect fragmented physical memory pages caused by memory fragmentation and is used for discontinuous physical memory allocations.
This can be used when the Kmalloc is not allocated memory and there is no contiguous requirement for physical memory. (preferred to find in high memory)

E.ioremap ()/iounmap ()
The function of Ioremap () is to map the physical address area of the device register and memory to the kernel virtual zone, and return the virtual address of the kernel. The linear address interval used is also in the Vmmlloc segment
Note:
Vmalloc () and Alloc_pages (_GFP_HIGHMEM) +kmap (); The former is discontinuous, and the latter can only map a high-end memory page
__get_free_pages and Alloc_pages (NORMAL) +page_address ();
Kernel address through __VA/__PA direct mapping of low memory
High-end memory is mapped using a kmap/kmap_atomic approach


The personal summary is as follows:
A. Use Kmalloc when &LT;128KB general memory allocations
Allow sleep: Gfp_kernel
Sleep not allowed: gfp_atomic
B. In >128kb memory allocation, use Get_free_pages to get a piece of page, directly return to the virtual address (<4m) (or alloc_pages + page_address)
C.b failed,
If you want to allocate high-end memory: Alloc_pages (_GFP_HIGHMEM) +kmap (only one page can be mapped)
If memory is not required: use Vmalloc to distribute logically contiguous chunks of pages. (not recommended)/slow to allocate and slow to access.
D. Frequent creation and destruction of many large data structures, using slab.
E. High-end memory mapping:
Allow sleep: kmap (permanent mapping)
Sleep not allowed: kmap_atomic (temporary mapping) overwrites previous to mappings (not recommended)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.