Linux kernel space memory application function Kmalloc, Kzalloc, vmalloc the difference "turn"

Source: Internet
Author: User

Transferred from: http://www.th7.cn/system/lin/201606/167750.shtml

We all know that the function of dynamically requesting memory in user space is malloc (), which is consistent with the use of various operating systems, and the corresponding user space memory deallocation function is free (). Note: The dynamic request must be released after the memory is used, otherwise it will cause a memory leak and if the memory leak occurs in kernel space, it will cause the system to crash.
So, how do you request memory in kernel space? In general we will use Kmalloc (), Kzalloc (), Vmalloc (), and so on, let us introduce the use of these functions and the differences between them.

Kmalloc ()

Function Prototypes:

void *kmalloc(size_t size, gfp_t flags);

Kmalloc () application memory is located in the physical memory map area, and is also physically contiguous, they have only a fixed offset from the real physical address, because there is a relatively simple conversion relationship, so the requested memory size is limited, can not exceed 128KB.
  
More commonly used flags (methods for allocating memory):

    • gfp_atomic -The process of allocating memory is an atomic process, and the process of allocating memory is not interrupted by (high-priority processes or interrupts);
    • Gfp_kernel --Normal allocation of memory;
    • GFP_DMA -allocates memory to the DMA controller, which requires the use of this flag (DMA requires assignment of virtual and Physical address succession).

The reference usage of flags:
|– process context, can sleep Gfp_kernel
|– process context, can not sleep gfp_atomic
| |– Interrupt Handler Gfp_atomic
| |– Soft Interrupt Gfp_atomic
| |–tasklet gfp_atomic
|– memory for DMA, can sleep GFP_DMA | Gfp_kernel
|– memory for DMA, no sleep GFP_DMA | Gfp_atomic
  
The corresponding memory deallocation functions are:

void kfree(const void *objp);
Kzalloc ()

The Kzalloc () function is very similar to Kmalloc (), with the same parameters and return values as the former is a variant of the latter, since Kzalloc () is actually an additional __gfp_zero flag. So in addition to applying for kernel memory, it also zeroed out the requested memory content.

/** * kzalloc - allocate memory. The memory is set to zero. * @size: how many bytes of memory are required. * @flags: the type of memory to allocate (see kmalloc). */static inline void *kzalloc(size_t size, gfp_t flags){    return kmalloc(size, flags | __GFP_ZERO);}

Kzalloc () corresponds to the memory release function that is also Kfree ().

Vmalloc ()

Function Prototypes:

void *vmalloc(unsigned long size);

The Vmalloc () function gives a contiguous memory area in the virtual memory space, but there is not necessarily continuous physical memory within this contiguous virtual. Since Vmalloc () does not guarantee the application to a contiguous physical memory, there is no limit to the amount of memory requested, and this function is required if you need to request a larger memory space.

The corresponding memory deallocation functions are:

void vfree(const void *addr);

Note: Vmalloc () and Vfree () can sleep and therefore cannot be called from the interrupt context.
  

Summarize

The common features of Kmalloc (), Kzalloc (), and Vmalloc () are:

    1. The memory used to request kernel space;
    2. The memory is allocated in bytes;
    3. The allocated memory is contiguous on the virtual address;

The differences between Kmalloc (), Kzalloc (), Vmalloc () are:

    1. Kzalloc is a kmalloc operation that enforces zeroing, (the following description does not differentiate between Kmalloc and Kzalloc)
    2. Kmalloc allocated memory size is limited (128KB), while Vmalloc is not limited;
    3. Kmalloc can guarantee that the allocated memory physical address is continuous, but vmalloc is not guaranteed;
    4. Kmalloc the process of allocating memory can be an atomic process (using gfp_atomic), while Vmalloc allocating memory can cause blocking;
    5. Kmalloc allocates memory less overhead, so kmalloc is faster than vmalloc;

In general, memory needs to be physically contiguous only when it is being accessed by DMA, but for performance reasons, Kmalloc () is generally used in the kernel, and Vmalloc () is used only when large chunks of memory are needed. For example, when a module is dynamically loaded into the kernel, the module is loaded into memory allocated by Vmalloc ().

Linux kernel space memory application function Kmalloc, Kzalloc, vmalloc the difference "turn"

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.