*************************************************************************************************************** ************
Easywave Time: 2013.02.06
Category: Linux kernel driver Source Analysis statement: reproduced, please keep the link
Note: If there is an error, please correct me. These are the journal articles I studied ...
*************************************************************************************************************** ************
In my previous blog < based on ft5x06 embedded Linux Capacitive touch screen driver >, I saw such a kernel memory allocation function Kzalloc, so the detailed understanding of this function, but also convenient for their own convenience, memory is not as good as the notes. A person's memory is limited. It's better to write it down. The following code:
As can be seen from the above function, it is ultimately called the Kmalloc function, for the KMALLOC function we should not be very unfamiliar. The original Kzalloc function added the flag bit __gfp_zero.
The Kzalloc function explains in detail:
#include <linux/slab.h> void *kmalloc (size_t size, int flags);
The first argument to Kmalloc is the size of the block to be allocated. The 2nd parameter, assigning a flag, is very interesting because it controls the behavior of the Kmalloc in several ways. The most commonly used flag, gfp_kernel, means this assignment (internally eventually by calling __get_free_pages, which is the source of the GFP_ prefix) generation The table runs in kernel-space processes. In other words, this means that the calling function represents a process that executes a system call. Using GFP_KENRL means that Kmalloc can allow the current process to sleep with less memory to wait for a page. A function that uses Gfp_kernel to allocate memory must, therefore, be reentrant and not run in an atomic context. When the current process sleeps, the kernel takes the correct action to locate some free memory, either by refreshing the cache to disk or swapping out the memory of a user process.
Gfp_kernel is not always the correct distribution mark used; Sometimes Kmalloc is called from outside the context of a process. For example, this type of invocation can occur in interrupt handling, tasklet, and kernel timers. In this case, the current process should not be set to sleep, and the driver should use a gfp_atomic flag instead. The kernel normally tries to keep some free pages in order to satisfy the distribution of atoms. When using Gfp_atomic, Kmalloc is able to use even the last free page. If this last free page does not exist, however, the allocation fails.
Other signs used to replace or add Gfp_kernel and gfp_atomic, although 2 of them cover most device-driven needs. All the flags are defined in <LINUX/GFP.H>, and each flag is prefixed with a double underscore, such as __GFP_DMA. In addition, there are symbols that represent often used combinations of flags; These lack prefixes and are sometimes referred to as assigning priorities. The latter includes:
Gfp_atomic
Used to allocate memory from the interrupt processing and other code outside the process context. Never sleeps.
Gfp_kernel
Normal allocation of kernel memory. May sleep.
Gfp_user
Used to allocate memory for user space pages; It may sleep.
Gfp_highuser
Like Gfp_user, but from high-end memory allocations, if any. The next subsection description is present in the high-end.
Gfp_noio
Gfp_nofs
This flag functions like gfp_kernel, but they increase the limit to what the kernel can do to satisfy requests. A gfp_nofs assignment does not allow any file system calls, and Gfp_noio does not allow any I/O initialization at all. They are primarily used in file systems and virtual memory codes, where a sleep allocation is allowed, but recursive file system calls can be a bad note.
These allocation flags listed above can be as parameters of the following flags, and these flags change how these allocations proceed:
__gfp_dma
This flag is required to be allocated in a memory area capable of DMA. The exact meaning is platform-dependent and is explained in the following chapters.
__gfp_highmem
This flag indicates that the allocated memory can be located in high-end memory.
__gfp_cold
Normally, the memory allocator tries to return to the "Buffered Hot" page-pages that may be found in the processor buffer. Instead, this flag requests a "cold" page, which has not been used for some time. It is useful for allocating pages for DMA reading, and it is useless to appear in the processor buffers.
__gfp_nowarn
This rarely used flag prevents the kernel from issuing a warning (using PRINTK) when an assignment is not met.
__gfp_high
This flag identifies a high-priority request, which is allowed to consume even the last memory pages that the kernel retains to the emergency state.
__gfp_repeat
__gfp_nofail
__gfp_noretry
These flags modify how the allocator moves when it has difficulty satisfying an assignment. __gfp_repeat means "try harder" by repeating the attempt – but the allocation may still fail. The __gfp_nofail flag tells the allocator not to fail; It tries its best to meet the requirements. The use of __gfp_nofail is strongly not recommended; There may never be a valid reason to use it in a device driver. Finally, __gfp_noretry tells the allocator to discard the requested memory immediately if it is not.
As you can see from the picture below:
Kzalloc functions in Linux kernel