Linux kernel memory allocation (three, virtual memory management)

Source: Internet
Author: User

Before analyzing the virtual memory management, we should look at the specific allocation of Linux kernel memory. I started to be trapped in this place. The classification of kernel memory 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. The kernel only uses the address space of the 3GB~4GB interval. Total 1GB. The physical mapping of non-connected
spaces is located between 3GB~4GB. For example,

0gb                                                             3gb                    4GB
and about how this 1GB is allocated in kernel space, see more:

Typically, a space larger than 896M in kernel space is called high-end memory in kernel space. The kernel can map page frames to high-end
Memory: Permanent kernel mappings, transient kernel mappings, and non-contiguous memory allocations in three different mechanisms. The non-contiguous memory allocations that will be discussed in this article.
       Inserts a 8MB interval between the end of the physical memory and the noncontiguous memory area, which is a safe zone,
to "capture" illegal access to non-contiguous areas. For the same reason. A safe zone of size 4KB is also inserted in other discontinuous intervals. The size of each
noncontiguous area is a multiple of 4KB. For example,

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, that
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: The above illustration shows a virtual address, but the actual physical address is a DMA and a regular address and a high-end address;

The Linux kernel memory is probably the above illustration. 8MB is said to be safe. Prevent cross-border access (read a lot of books, so to speak), is that this 8MB virtual address does not do whatever mapping (this is the virtual address.) No actual waste of physical address)

As can be seen from the above diagram, the front 896MB (other architectures can not be cut at 896MB) is what we call the kernel logical address (remember that is the kernel logical address. Suppose that the logical address should mean that the virtual address in the x86 schema does not include the segment address part, that is, the offset part of the paragraph, 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 is the part of the use). This memory is very efficient, because you do not need to do other mapping and change 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 drive modules. The virtual address returned by the function is contiguous (in fact this is also questionable.) Since the virtual address area of the above Vmalloc has a 4k cut address, it is assumed that the virtual address assigned by Vmalloc is very large. So is there a 4kb cutting address in the middle? ), 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 not encouraged to use Vmalloc to request memory, for reasons: 1, the use of memory through the VMALLOC function is not efficient (because you want to do the mapping, to infer which is the spare page and other operations). 2, some architectures to vmalloc use of memory address is very small. The Vmalloc call may fail because there is no spare address, 3, can not guarantee that the physical address is continuous, for some drivers this is a mishap; It is best not to use code that includes Vmalloc as the mainline code for 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. Mapping the virtual address obtained in 1 to a physical page in 2;

Attention:

1, the above diagram we can see that each vmalloc virtual address has 4kb of cutting area (its role is to prevent cross-border. Creates an empty hole that produces an exception when the Vmalloc function is implemented, and then adds a 4kb size (the size of a page) after the size is aligned.

2, when allocating physical pages, will be from the high-end address (the above illustration of 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: 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, This will not guarantee that the entire physical page must be contiguous.

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

The following is a map of the vmalloc. Image from "Deep Linux device driver kernel mechanism"



Medium: Two virtual page addresses allocated from the Vmalloc region are mapped to high-end pages of physical addresses. High-end memory is not contiguous, the last page of the virtual address is not mapped, that is, an additional 4k cutting page.

Addresses allocated with Vmalloc cannot be used outside of 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 large contiguous chunk of memory that exists only in the software for buffering.


Ioremap function

Function prototypes: void __iomem *ioremap (unsigned long phys_addr, size_t size), where __iomem is simply an address that identifies the address returned as an IO type This function is used to map the memory between the VMALLOC regions to the device I/O address space, which is similar to the implementation of the Vmalloc function, where Vmalloc is assigned to a physical page through a partner system. The Ioremap function uses the I/O space of the device instead of the system physical page; As for other operations: access to I/O memory and I/oport devices

Many other ioremap functions are used to map (physical) PCI buffer addresses to (virtual) kernel space. The memory of the IOREMAP function mapping needs to be freed with the IOUNMAP function;


Vmalloc and Kmalloc Compare

Kmalloc function:

1. The resulting memory retains the last used data, and the memory that was requested incorrectly is initialized (the Zmalloc function initializes the requested memory).

2. The returned logical address (in fact, the virtual address) and the mapped physical page are contiguous. The function may be dormant when it is called;

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

4, the Kmalloc function and the __get_free_pages function use a virtual address range corresponding to the physical memory is one by one, there may be a constant offset. These two functions do not need to change 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 address range used by the Vmalloc function and the Ioremap function is completely virtual. Each assignment is to establish a mapping relationship through the operation of the page table;

3, Vmalloc function is generally used to allocate large chunks of memory. And the returned address cannot be used outside the microprocessor;

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


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.