Linux kernel memory allocation (three, virtual memory management)

Source: Internet
Author: User

Before analyzing virtual memory management, look at the detailed allocation of Linux kernel memory I was stuck in this place, and the kernel memory classification is not very clear. I extract one of the following:


Kernel memory address

===========================================================================================================

In the memory management of Linux, the user uses the 0~3GB address space, and the kernel only uses the address space of the 3GB~4GB interval, a total of 1GB; non-connected
The physical mapping of the continuation space is located between the 3GB~4GB, as shown in

0GB 3GB 4GB
And about how the 1GB is allocated in kernel space, see:

Typically, spaces larger than 896M in kernel space are referred to as high-end memory in kernel space. The kernel can map page frames to high-end with three different mechanisms
Memory: Permanent kernel mappings, temporary kernel mappings, and non-contiguous memory allocations. The non-contiguous memory allocations that will be discussed in this article.
From the end of the physical memory and the non-contiguous memory area, a range of 8MB is inserted, which is a safe zone,
The purpose is to "capture" illegal access to noncontiguous zones. For the same reason, a safe zone of 4KB size is also inserted in other discontinuous intervals. And each
The size of the noncontiguous area is a multiple of 4KB. Such as:

The linear address space for non-contiguous memory is from Vmalloc_start~vmalloc_end, a total of 128MB size. When the kernel needs to use the function of the Vmalloc class
When a non-contiguous memory allocation is made, a vm_struct structure is applied to describe the corresponding Vmalloc area, and if multiple vmalloc memory areas are allocated, the
The interval between the adjacent two vmalloc zones is at least 4KB, which is at least one page box size page--size. Such as.

===============================================================================================================

Here is the emphasis: The above illustration shows only the virtual address, and the actual physical address is the DMA and the regular address and the high-end address;

Linux kernel memory is probably the above diagram, where 8MB is for security, to prevent cross-border access (read a lot of books, so to speak), this 8MB virtual address does not do any mapping (this is only a virtual address, no actual physical address wasted)

As can be seen from the above diagram, the front 896MB (other architectures can not be divided into 896MB) is what we call the kernel logical address (remember the kernel logical address, if the logical address should refer to the x86 schema in the virtual address is not included in the segment address part, that is, the offset part of the paragraph) This part of the memory address has been at the time of system initialization and physical page mapping, and is a one by one mapping, we generally use this part of the memory address (Kmalloc function use is that part). This memory is very efficient, because you do not need to do other mapping and modify the page table can be used directly. This blog is the analysis of virtual memory address mapping, mainly vmalloc function and ioremap function;


Vmalloc function

The Vmalloc function is a memory allocation function that is often used by the driver module, which returns the virtual address of a continuous (in fact, this is also questionable, because the above Vmalloc virtual address area has a 4k split address, if the Vmalloc assigned virtual address is large, then the middle of a 4kb split address? ), but there is no guarantee that the mapped physical address is contiguous. It mainly on the above Vmalloc_start to vmalloc_end this memory operation, the return of the virtual address is this part.

In most cases, it is discouraged to use Vmalloc to request memory, for reasons: 1, the use of memory through the VMALLOC function is not efficient (because to do the mapping, to determine which are free pages and other operations), 2, some architectures to Vmalloc used memory address is very small, The Vmalloc call may fail because there is no idle address, 3, can not guarantee that the physical address is continuous, for some drivers this is flawed; In summary, it is best not to use the code containing VMALLOC as the main line code of the kernel.

The following is probably the prototype of the VMALLOC function:

void *vmalloc (unsigned long size);

The implementation of this function has 3 steps: 1, in the Vmalloc region to allocate a contiguous virtual memory address, 2, through the partner system to obtain physical pages, 3, through the operation of the page table, the virtual address obtained in 1 is mapped to 2 in the physical page;

Attention:

1, the above diagram we can see that each vmalloc virtual address has a 4KB partition area (its role is to prevent cross-border, forming an empty, when the exception occurs when crossing the bounds), so the Vmalloc function, when the size is aligned after the size of the 4KB (a page size);

2, in the allocation of physical pages, will be from the high-end address (the above illustration is only the virtual address, physical memory allocation can see the Linux kernel memory allocation (one, the basic concept) of the physical page and virtual address mapping) allocation, GFP is: Gfb_kernel | _gfp_highmem; indicates that the function may sleep, assigning a physical address from a high-end physical page. The general physical page is used for kmalloc; the Vmalloc function assigns a high-end physical page using the Alloc_page function or the Alloc_pages_node function to allocate an entire page, multiple calls to the allocation function to complete the allocation of all physical pages, There is no guarantee that all physical pages will be contiguous.

3, the virtual address mapping does not map the additional 4k of the split address, the 2nd step will not be allocated to this 4k virtual split address mapping of the physical page.

Here is a map of Vmalloc, from the deep Linux device driver kernel mechanism



Medium: Two virtual page addresses from the Vmalloc region are mapped to high-end pages of physical addresses, where high-end memory is not contiguous, and the last page of the virtual address is not mapped, which is an additional 4k split page;

Addresses allocated with Vmalloc cannot be used outside the microprocessor because they are only meaningful on the memory management unit of the processor. The correct place to use the Vmalloc function is when allocating a chunk of contiguous memory that exists only in the software for buffering.


Ioremap function

Function prototype: void __iomem *ioremap (unsigned long phys_addr, size_t size), where __iomem simply identifies the address returned as the IO type This function is used to map the memory between the VMALLOC regions to the device I/O address space, which is very similar to the implementation of the Vmalloc function, where vmalloc is allocated to the physical page through the partner system, while the IOREMAP function takes advantage of the I/O space of the device. Instead of the system physical page; For other operations you can see: Access I/O memory and I/O port devices

The Ioremap function is more useful for mapping (physical) PCI buffer addresses to (virtual) kernel space. The memory of the IOREMAP function mapping needs to be freed with the IOUNMAP function;


Comparison of Vmalloc and Kmalloc

Kmalloc function:

1. The resulting memory retains the last used data and does not initialize the requested memory (the Zmalloc function initializes the requested memory);

2, the returned logical address (actually also the virtual address) and the mapped physical page are contiguous, may sleep when the function is called;

3, the Kmalloc function and the memory address returned by the __get_free_pages function are virtual addresses, the actual physical address needs to be converted through the MMU (in fact, the MMU is through the page table mechanism to convert);

4, the Kmalloc function and the __get_free_pages function use the virtual address range and the physical memory is one by one corresponding, there may be a constant offset, these two functions do not need to modify the page table;

5, Kmalloc function application memory size is limited, generally based on the structure of the decision;


Vmalloc functions and Ioremap functions:

1, the use of low efficiency, physical pages do not guarantee continuous, virtual address guarantee continuous;

2, the Vmalloc function and the IOREMAP function use the address range is completely fictitious, each allocation must through to the page table operation to establish the mapping relation;

3, Vmalloc function is generally used to allocate large chunks of memory, and the returned address can not be used outside the microprocessor;

Reprint Address: http://blog.csdn.net/yuzhihui_no1/article/details/47429411


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linux kernel memory allocation (three, virtual memory management)

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.