get_free_page Linux device drivers

get_free_page

If the module needs to allocate large blocks of memory, the page will be used for dispensing a number of advantages;

 

Distribution page can use the following functions:

1 unsigned long get_zeroed_page(gfp_t gfp_mask)

Returns a pointer to the new page and page zeroing;

1 unsigned long __get_free_page(gfp_t gfp)

Returns a pointer to the new page, the page is not clear;

1 unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)

Several distribution (physically contiguous) pages, and returns a pointer to the first byte of the memory area, the page is not cleared; wherein the order is the number of pages to be allocated to the logarithm base 2, for example, 0 indicates a page 3 shows the eight pages; get_order function using an integer parameter returns order value depending on the size of the host platform, the maximum allowable order value is 10 or 11 (corresponding to 1024 or 2048 pages), depending on the architecture ;

 

When the program does not require the use of pages, one can use the following function to release them;

1 void free_page(unsigned long addr)
2 
3 void free_pages(unsigned long addr, unsigned int order)

If you try to release and the previously assigned a variable number of pages, memory map will be destroyed, then the system will go wrong;

 

And that they meet the same rules kmalloc, get_free_pages and other functions can be called at any time; in some cases function allocates memory fails, in particular sub-ah GFP_ATOMIC used when it is necessary to provide appropriate treatment in the allocation of error;

Although kmalloc (GFP_KERNEL) sometimes fails when there is no free memory, but the kernel will satisfy the memory allocation request as much as possible; therefore, allocating too much memory, the response system performance easily down; kmalloc allocation system to meet the request while trying to change when the memory pages as much as possible, will slow down; even unable to solve this problem and create a new process;

Allocation strategy based on the advantages of the actual page is not speed, but in a more efficient use of memory; page-allocated memory space is not wasted, and the reasons kmalloc function will allocate efforts wasted a certain amount of memory; and when assigned when the page is fully our own, through appropriate adjustments to the page table merging them into a linear region, for example, allow a user process to mmap these were single unrelated memory area;

alloc_pages

struct page is used to describe the data structure of a single core memory page; kernel page structures in many places, especially in high memory where required (high memory corresponding to the address is not the same in the kernel space);

Linux core code page allocator called alloc_pages_node function;

1 struct page *alloc_pages_node(int nid, gfp_t gfp_mask,
2                         unsigned int order)

This function has two variants, in most cases we use these macros;

1 #define alloc_pages(gfp_mask, order)
2 #define alloc_page(gfp_mask)

alloc_pages_node requires three arguments, NID is the ID number of the NUMA node, which indicates to the allocated memory, mask is the usual GFP_ allocation flag, order to allocated memory size; return value of the function points to a page structure ( You may return a plurality of pages) of the pointer that describes the allocated memory; or return NULL on failure;

alloc_pages simplified by allocating memory in a NUMA node on the current alloc_pages_node function, the return value will numa_node_id nid parameter called alloc_pages_node function, further, apparently ignored order alloc_page function parameters assigned only a single page;

To release the page assigned by the above approach, we should use the following functions:

1 #define __free_page(page) __free_pages((page), 0)
2 void __free_pages(struct page *page, unsigned int order)
3 void free_hot_cold_page(struct page *page, bool cold)

When know the contents of a page to tell whether the cache in the processor, you should use the kernel and free_hot_cold_page to communicate this information helps memory allocator optimize memory usage;

 

Guess you like

Origin www.cnblogs.com/wanpengcoder/p/11761063.html