Linux memory allocation method summary

Source: Internet
Author: User

Memory ing structure:

1.32-bit address lines address 4G memory space, of which 0-3G is exclusive to the user program, and 3g-4G is occupied by the kernel.

2. struct page: each 4 kb page generates a corresponding struct page Structure during initialization of the entire physical environment. This page structure uniquely represents this physical memory page and is stored in the mem_map global array.

3. segment ing: first, select the sub-cs as the index based on the code segment, and select the corresponding segment descriptor from the segment description table with the GDT value as the starting address. Then, based on the base address and length of the segment descriptor, verify the permission information. Cs: The 32-bit offset in the offset directly accumulates with the base address of the current segment to obtain the final access address.

0-ing between 0-3G and mem_map:
Because the segment ing in linux is in the flat mode, the logic address to the linear address remains unchanged. Each user process has a separate page Directory table (pdt), which is stored at the runtime in S3. (Page Directory) + TOP 10 digits => base address of the page table + middle 10 digits => page table entry + last 12 digits => physical page address

Ing between 3g-4g and mem_map:
There are three types: low-end memory, general memory, and high-end memory.
Low-end memory: 3G-3G + 16 M for DMA _ pa linear ing
Normal memory: 3G + 16 M-3G + 896 M _ pa linear ing (if the physical memory is <896 M, the demarcation point is 3G + actual memory)
High-end memory: 3G + 896-4G Dynamic Allocation

4. High-end memory (assuming 3G + 896 is the address of high-end memory)
Purpose: Access physical memory space other than 1 GB.
Linear addresses are divided into three segments: vmalloc, kmap, and kmap_atomic segments (for different memory allocation methods)


The structure of the memory allocation function is divided into the following parts:
A. Partner algorithm (the most original page-oriented distribution method)
Alloc_pages interface:
Struct page * alloc_page (unsigned int gfp_mask) -- allocates a page of physical memory and returns the page Structure pointer of the page's physical memory.
Struct page * alloc_pages (unsigned int gfp_mask, unsigned int order) -- allocates consecutive physical pages and returns the page Structure pointer of the allocated first physical page.
<Release function :__ free_page (s)>

Defined in the kernel: # define alloc_page (gfp_mask) alloc_pages (gfp_mask, 0)
All are called _ alloc_pages.
MAX_ORDER 11 and the maximum number of pages allocated to it is 2 ^ 10 (4 M ).
The allocated page cannot be used directly. You need to obtain the virtual address of the page:
Void * page_address (struct page * page );
Low-end memory ing: __va (unsigned long) (page-mem_map) <12)
High-end memory to ing mode: struct page_address_map allocates a dynamic structure to manage high-end memory. (The kernel cannot access the virtual address below 3g of vma.) The specific ing is executed by kmap/kmap_atomic.

Get_free_page interface: (two-step substitution function of alloc_pages Interface)
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 from the alloc_page (s) series is that you cannot apply for high-end memory because it returns a linear address, and the high-end memory requires additional ing for access.

B. slab High-speed cache (multiple memories of the same size are allocated repeatedly) Note: Use less
Kmem_cache_t * xx_cache;
Create: xx_cache = kmem_cache_create ("name", sizeof (struct xx), SLAB_HWCACHE_ALIGN, NULL, NULL );
Allocation: kmem_cache_alloc (xx_cache, GFP_KERNEL );
Release: kmem_cache_free (xx_cache, addr );
Memory Pool
Mempool is not used.

C. kmalloc (the most common allocation Interface) Note: It must be smaller than KB
GFP_ATOMIC is not sleep and used for interrupt processing.
GFP_KERNEL will sleep, which is usually used
GFP_USER will sleep
_ GFP_DMA allocate DMA memory
Kmalloc/kfree

D. vmalloc/vfree
Vmalloc uses virtual space reserved by high-end memory to collect discontinuous physical memory pages caused by memory fragments. It is used for non-continuous physical memory allocation.
You can use kmalloc when the memory is not allocated and there is no need for continuous physical memory. (Search from high-end memory first)

E. ioremap ()/iounmap ()
Ioremap () maps the physical address areas of the device register and memory to the virtual area of the kernel. The returned value is the virtual address of the kernel. The linear address range used is also in the vmmlloc segment.
Note:
Vmalloc () and alloc_pages (_ GFP_HIGHMEM) + kmap (); the former is not continuous, and the latter can only map one high-end Memory Page
_ Get_free_pages and alloc_pages (NORMAL) + page_address (); they are exactly the same
The kernel address uses _ va/_ pa for direct ing between medium and low memory.
High-end memory ing using kmap/kmap_atomic


My personal summary is as follows:
A. Use kmalloc for <kb general memory allocation
Sleep allowed: GFP_KERNEL
Sleep not allowed: GFP_ATOMIC
B. Use get_free_pages to obtain the Part Page and directly return the virtual address (<4 M) (or alloc_pages + page_address) When memory allocation is greater than kb)
C. B failed,
If you want to allocate high-end memory: alloc_pages (_ GFP_HIGHMEM) + kmap (only one page can be mapped)
If memory continuity is not required: Use vmalloc to allocate large pages in a logically continuous manner. (not recommended)/the allocation speed is slow and the access speed is slow.
D. frequently create and destroy many large data structures using slab.
E. High-end memory ing:
Sleep allowed: kmap (permanent ing)
Sleep not allowed: kmap_atomic (temporary ing) overwrites the previous ing (not recommended)

Related Article

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.