get_free_page and his friends

If a module needs to allocate large blocks of memory, it is often best to use a page-oriented technology requests the entire page has other advantages that are described in Chapter 15.

 

Assigned page, the following functions are available: get_zeroed_page (unsigned int flags);

It returns a pointer to the new page and the page is filled with zeros.

 

    get_free_page(unsigned int flags);

 

Similar get_zeroed_page, but it did not clear the page.

 

    get_free_pages(unsigned int flags, unsigned int order);

 

Allocates and returns a pointer to the first byte of a memory area pointer memory area may be several (continuous physical) page length, but not cleared.

 

flags parameter the same as the usage kmalloc; often used GFP_KERNEL or GFP_ATOMIC, possibly with

    GFP_DMA flag (to ISA DMA operation may be used in memory) or may be used when GFP_HIGHMEM

 

When high memory. [ 29 ] 29 order is the base 2 logarithm of the number of pages or release your request (i.e., log2N). For example, if you want a page order is 0, that is if you ask 8 3. If the order is too large (the size of the contiguous area that is not available), the page allocation failure. get_order function, which uses an integer parameter can be used to extract order (it must be a power of 2) from one size to the host platform. maximum allowed order of 10 or 11 (corresponding to a 1024 or 2048), depending on the system. However, the order is assigned a 10-in addition to a lot of memory just started chance of success of the system is small.

 

If you're curious, / proc / buddyinfo tell you that system, each memory area of ​​each order how many blocks are available.

 

When a program runs out of these pages, it can use one of the following function to release their first function is a fall back to the second function macro:

 

void free_page(unsigned long addr);

void free_pages(unsigned long addr, unsigned long order);

 

If you try to release and the number of pages you assign different pages, memory map turmoil, the system has trouble later time.

 

It is worth to emphasize, get_free_pages and other functions can be called at any time, follow the same rules kmalloc we see. These functions can not allocate memory in some cases, especially when using GFP_ATOMIC. Thus, calling these allocation functions of the program you must be prepared to handle an allocation failure.

 

Although kmalloc (GFP_KERNEL) sometimes fails when there is no memory available, the kernel allocation request to suit Thus, for example easy, you can make a computer squeezed off by a large amount of data through the scull response assignment much reduced memory system;. System start crawling when it tries to swap out as much memory to satisfy the request because kmalloc each resource being devoured growing equipment, computers will soon be unable to say with; at this point, you can not even start a new process to try to deal with this problem. we do not explain the issue in scull, because it's just an example module and is not a true multi-user tool into the system. as a programmer, you must be careful, because a module is privileged code and may open new security vulnerabilities in the system (most likely a denial of service vulnerability seems just described.)

Guess you like

Origin www.cnblogs.com/fanweisheng/p/11142111.html