Common memory allocation functions in Linux kernel (2)

Source: Internet
Author: User
The common memory allocation function _ get_free_pages unsigned long _ get_free_pages (gfp_t gfp_mask, unsigned int order) _ get_free_pages is the most primitive memory allocation method, obtain the original page from the partner system. The returned value is the starting address of the first page. _ Get_free_pages encapsulates the alloc_pages function in implementation. Code For analysis, the alloc_pages function will allocate 1 <symbol struct kmem_cache * kmem_cache_create (const char * Name, size_t size, size_t align, unsigned long flags, void (* ctor) (void *, struct kmem_cache *, unsigned long), void (* dtor) (void *, struct kmem_cache *, unsigned long) void * kmem_cache_alloc (struct kmem_cache * C, gfp_t flags) kmem_cache_create/kmem_cache_alloc is a memory allocation method based on the slab distributor. It is suitable for repeatedly allocating and releasing memory blocks of the same size. First, use kmem_cache_create to create a cache region, and then use kmem_cache_alloc to obtain a new memory block from the cache region. The maximum memory that kmem_cache_alloc can allocate at a time is determined by mm/slab. the max_obj_order macro definition in the C file. In the default 2.6.18 kernel version, this macro is defined as 5. Therefore, a maximum of 1 <5*4 kb, that is, kb, can be applied at a time.
Continuous physical memory. By analyzing the kernel source code, it is found that when the size parameter of the kmem_cache_create function is greater than kb, a bug () is called (). The test results verify the analysis results. The kernel crashes when the kmem_cache_create allocation exceeds KB of memory. Linux training kmalloc void * kmalloc (size_t size, gfp_t flags) kmalloc is the most common memory allocation method in the kernel. It is implemented by calling the kmem_cache_alloc function. The maximum memory size that kmalloc can apply for at a time is determined by the content of include/Linux/kmalloc_size.h. In the default 2.6.18 kernel version, kmalloc can apply for a maximum of 131702b continuous physical memory of kb. Test results show that if you try to use the kmalloc function to allocate memory greater than kb, the compilation fails. Vmalloc void * vmalloc (unsigned Long SIZE) the previous memory allocation methods are physically continuous, ensuring a low average access time. However, in some scenarios, requests to the memory area are not very frequent, and a high Memory Access time is acceptable. In this case, a linear continuous and physically discontinuous address can be allocated, the advantage is that a large memory size can be allocated at a time. Vmalloc has no explicit limit on the memory size that can be allocated at a time. Exercise caution when using the vmalloc function for performance considerations. During the test, a maximum of 1 GB space can be allocated at a time. Linux Kernel Some memory distribution dma_alloc_coherent void * dma_alloc_coherent (struct device * Dev, size_t size, ma_addr_t * dma_handle, gfp_t green) DMA is a hardware mechanism, allows the peripheral device and the primary storage to directly transmit Io data without the involvement of the CPU. The DMA mechanism can greatly improve the throughput of communication with the device. The DMA Operation involves high-speed CPU slow-down and corresponding memory data consistency. The data consistency must be ensured. In the x86_64 architecture, the hardware has successfully solved this problem, the implementation of the dma_alloc_coherent and _ get_free_pages functions is not much different. The former actually calls the _ alloc_pages function to allocate memory. Therefore, the size limit for one memory allocation is the same as that for the latter. The memory allocated by _ get_free_pages can also be used for DMA operations. The test results show that the maximum memory that the dma_alloc_coherent function can allocate at a time is 4 MB.

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.