Kmalloc/kfree, vmalloc/vfree function usage and Difference
1. kmalloc
1> kmalloc memory allocation is similar to that of malloc. Unless blocked, kmalloc runs very fast and does not clear space.
<
Tiger Description: after applying for a function using kmalloc, it must be cleared.
Use the memset () function to clear the applied memory.
>
2> kamlloc function prototype:
# Include <Linux/slab. h>
Void * kmalloc (size_t size, int flags );
(1) The first parameter is the size of the block to be allocated.
(2) The second parameter is the allocation sign (flags), which provides multiple kmalloc behaviors.
(3) The third most commonly used gfp_kernel;
A. indicates memory allocation (get_free_pages is always called in the end to realize the actual allocation; this is the origin of the green code prefix). It indicates that the process running in the kernel space is executed. Gfp_kernel allows kmalloc to allow the current process to sleep and wait when idle memory is allocated. Therefore, the assignment function must be reentrant. If the current process should not sleep outside the process context, such as interrupt handler, tasklet, and kernel timer, the driver should use gfp_atomic.
B. gfp_atomic
Used to allocate memory from code other than interrupt processing and process context. never sleep.
C. gfp_kernel
Normal kernel memory allocation. May be sleep.
D. gfp_user
Used to allocate memory for user space pages; it may sleep.
E. gfp_highuser
Like gfp_user, but allocated from high-end memory, if there is. There is a subsection description in the high-end.
F. gfp_nofs, gfp_noio
This flag function is like gfp_kernel, but they are limited to what the kernel can do to satisfy the request. A gfp_nofs allocation does not allow any file system calls, while gfp_noio does not allow any I/O initialization. they are mainly used in the file system and Virtual Memory code, which allows a sleep allocation, but recursive file system calls will be a bad note.
The allocation flags listed above can be the phase or parameters of the following flags, which change how these distributions are performed:
_ Gfp_dma
This flag requires that it be allocated in the memory zone that can be DMA. The exact meaning is platform-dependent and will be explained in the following sections.
_ Gfp_highmem
This indicator indicates that the allocated memory can be located in the high-end memory.
_ Gfp_cold
Normally, the memory distributor tries its best to return the "buffered hot" Page-pages that may be found in the processor buffer. on the contrary, this flag requests a "cold" page, which is not used for a period of time. it is useful to perform DMA read on the allocation page, and it is useless in the processor buffer. for a complete discussion of how to allocate DMA cache, see "Direct Memory Access" in Chapter 1st.
_ Gfp_nowarn
This rarely used flag prevents the kernel from issuing a warning (using printk) when a allocation fails.
_ Gfp_high
This flag identifies a high-priority request that is allowed to consume or even be retained by the kernel to the final memory page of the emergency.
_ Gfp_repeat
_ Gfp_nofail
_ Gfp_noretry
These flags modify how a distributor acts when it has difficulty meeting an assignment. _ gfp_repeat indicates "try more" by retrying -- but the allocation may still fail. the _ gfp_nofail flag tells the distributor not to fail. It tries its best to meet the requirements. it is strongly not recommended to use _ gfp_nofail; there may never be a valid reason to use it in a device driver. finally, _ gfp_noretry tells the allocator to immediately discard the requested memory.
Ø memory segments
The use of _ gfp_dma and _ gfp_highmem is related to the platform. Linux divides the memory into three segments: memory that can be used for DMA, general memory, and high-end memory. On the X86 platform, the DMA segment of the ISA Device is the first 16 MB of memory, but the PCI device does not have this limit.
The mechanism behind the memory area is implemented in mm/page_alloc.c, while the initialization of the memory area is usually in mm/init. C of the arch directory tree in specific platform files.
3> usage of kamlloc:
Linux processes memory allocation by creating a fixed-size memory object pool. the allocation request is processed in this way, entering a pool of large enough objects and submitting the entire memory block to the requester. one thing drivers should remember is that the kernel can only allocate some predefined, fixed-size byte arrays.
If you request an arbitrary amount of memory, you may get a little more than your request, up to 2 times. similarly, programmers should remember that the minimum allocation that kmalloc can handle is 32 or 64 bytes, depending on the page size used by the system. there is an upper limit on the size of memory blocks that can be allocated by kmalloc. this restriction varies with system and Kernel configuration options. if your code is completely portable, it cannot be expected to allocate anything greater than 128
KB. If you need more than a few Kb, but there is a better way to get the memory than kmalloc. Dynamically open up memory in the device driver or kernel module, instead of using malloc, but kmalloc
, Vmalloc, or use get_free_pages to directly apply for a page. Kfree, vfree, or free_pages are used to release the memory. the kmalloc function returns a virtual address (linear address ). the special feature of kmalloc is that the memory allocated by kmalloc is physically continuous, which is very important for devices that require DMA. however, the memory allocated with vmalloc is linear and the physical address is not necessarily consecutive and cannot be directly used for DMA.
Note that kmalloc can only be opened up to-16, and 16 bytes are occupied by the page descriptor structure. For more information about kmalloc usage, see KHg.
Memory-mapped I/O Ports, registers, or RAM (such as memory) of hardware devices generally occupy address space above f0000000. Cannot be accessed directly in the driver. You need to use the kernel function vremap to obtain the address after re ing.
In addition, many hardware require a large continuous memory for DMA transmission. This memory must remain in the memory and cannot be exchanged to files. However, kmalloc can only open up a maximum of 32xpage_size memory. Generally, page_size = 4kb, that is, kb memory.
3. Differences between kmalloc and vmalloc
• Both vmalloc () and kmalloc () can be used to allocate memory.
• The Memory allocated by kmalloc () is 3 GB ~ High_memory: The ing between the kernel space and the physical memory is one-to-one.
• Vmalloc_start ~ exists in the vmalloc () allocation ~ Between 4 GB, the non-contiguous inner zone ing to the physical memory may also be non-consecutive
• Call kmalloc () in the kernel space to allocate continuous physical space, while call vmalloc () to allocate non-physical continuous space.
• The address in the kernel space allocated by kmalloc () is called the kernel logic address.
• The addresses in the kernel space allocated by vmalloc () are called Kernel virtual addresses.
• The kernel page table must be updated during vmalloc () allocation.
Summary:
1. kmalloc and vmalloc allocate the kernel memory, while malloc allocates the user memory.
2. kmalloc ensures that the allocated memory is physically continuous, and kmalloc () is allocated with memory above 0xbfffffffff-0xffffffff. The driver generally uses it to allocate Ds, it is more suitable for devices with similar drivers;
3. vmalloc ensures the continuity in the virtual address space, vmalloc () is in the physical address non-consecutive, and the virtual address continuous area. The starting position is determined by vmallol_start, it is generally used as the distribution of swap areas and modules.
3. The size of kmalloc can be allocated is limited. The size of vmalloc and malloc can be allocated is relatively large (because vmalloc can also process swap space ).
4. The memory must be physically contiguous only when it is accessed by DMA. vmalloc is slower than kmalloc.
5. The correct use of vmalloc is to allocate a large block of continuous memory that exists only in the software and is used to buffer the memory area. It cannot be used outside of the microprocessor.
6. kmalloc (GFP-KERNEL) is called in vmalloc, so it cannot be applied to atomic context.
7. kmalloc and kfree manage the memory allocated in the kernel segment, which is the actual physical memory block with known real addresses.
8. vmalloc corresponds to vfree and allocates continuous virtual memory, but not necessarily physically consecutive.
9. kmalloc memory allocation is based on slab. Therefore, some features of slab include coloring and alignment, which provides better performance. The physical and logical addresses are continuous.
Link: http://blog.csdn.net/tigerjb/article/details/6412881