Differences between kmalloc, vmalloc, and malloc

Source: Internet
Author: User

To put it simply:

  1. Kmalloc and vmalloc are allocated with kernel memory, while malloc is allocated with user memory.
  2. Kmalloc ensures that the allocated content is physically continuous. vmalloc ensures that the virtual address space is continuous, and malloc does not guarantee anything (this is self-guessed and not necessarily correct)
  3. The size of kmalloc can be allocated is limited, and the size of vmalloc and malloc can be allocated is relatively large.
  4. The memory must be physically consecutive only when it is accessed by DMA.
  5. Vmalloc is slower than kmalloc

 

Detailed explanation:

Linux provides a complex Storage Management System for processors that provide MMU (Storage Manager, which assists the operating system in memory management and hardware support such as virtual/real address translation, the memory that the process can access reaches 4 GB.

The 4 GB memory space of a process is divided into two parts: user space and kernel space. The user space address distribution ranges from 0 to 3 GB (page_offset, in 0x86, It is equal to 0xc0000000), and 3 GB to 4 GB is the kernel space.

In the kernel space, the address range from 3G to vmalloc_start is the physical memory ing area (this area contains the kernel image, the physical page box table mem_map, and so on ), for example, if the memory of the VMWare virtual system we use is 160 MB, then 3G ~ The 3G + M memory should be mapped to the physical memory. After the physical memory ing area, it is the vmalloc area. For a M system, the vmalloc_start location should be around 3G + M (an 8 m gap exists between the physical memory ing zone and vmalloc_start to prevent the gap ), the location of vmalloc_end is close to 4 GB (the system will retain a kb area for dedicated page ing)

The memory requested by kmalloc and get_free_page is located in the physical memory ing area and is physically continuous. They have only a fixed offset with the real physical address, so there is a simple conversion relationship, virt_to_phys () can be used to convert the kernel virtual address to the physical address:
# DEFINE _ Pa (x) (unsigned long) (x)-page_offset)
Extern inline unsigned long comment _to_phys (volatile void * address)
{
Return _ Pa (Address );
}
The above conversion process is to subtract the virtual address 3G (page_offset = 0xc000000 ).

The corresponding function is phys_to_virt (), which converts the physical address of the kernel to a virtual address:
# DEFINE _ VA (x) (void *) (unsigned long) (x) + page_offset ))
Extern inline void * phys_to_virt (unsigned long address)
{
Return _ VA (Address );
}
Both pai_to_phys () and phys_to_virt () are defined in include/asm-i386/IO. h.

The memory applied for by vmalloc is located in vmalloc_start ~ There is no simple Conversion Relationship Between vmalloc_end and physical addresses. Although they are logically continuous, they do not require continuity physically.

The following program is used to demonstrate the differences between kmalloc, get_free_page, and vmalloc:
# Include <Linux/module. h>
# Include <Linux/slab. h>
# Include <Linux/vmalloc. h>
Module_license ("GPL ");
Unsigned char * pagemem;
Unsigned char * kmallocmem;
Unsigned char * vmallocmem;

Int _ init mem_module_init (void)
{
// It is best to check whether the application is successful for each memory Application
// The following section does not check the Demo code.
Pagemem = (unsigned char *) get_free_page (0 );
Printk ("<1> pagemem ADDR = % x", pagemem );

Kmallocmem = (unsigned char *) kmalloc (100, 0 );
Printk ("<1> kmallocmem ADDR = % x", kmallocmem );

Vmallocmem = (unsigned char *) vmalloc (1000000 );
Printk ("<1> vmallocmem ADDR = % x", vmallocmem );

Return 0;
}

Void _ exit mem_module_exit (void)
{
Free_page (pagemem );
Kfree (kmallocmem );
Vfree (vmallocmem );
}

Module_init (mem_module_init );
Module_exit (mem_module_exit );

Our system has 121 MB of memory space. Once we run the above program, we find that the address of pagemem is 0xc7997000 (about 3G + M) the kmallocmem address is at 0xc9bc1380 (about 3G + 155 m) and the vmallocmem address is at 0xcabeb000 (about 3G + 171 m), which conforms to the memory layout described above.

Implementation principle of http://blog.csdn.net/daniel_ice/article/details/6834316 vmallc

Http://blog.csdn.net/macrossdzh/article/details/5958368

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.