Linux kernel memory management (ZONE_DMA zone_normal Zone_highmem) __linux

Source: Internet
Author: User

Linux operating systems and drivers running in the kernel space, the application running in user space, the two can not simply use the pointer to pass data, because Linux uses the virtual memory mechanism, user space data may be swapped out, when the kernel space using user space pointers, the corresponding data may not be in memory.

Linux Kernel address space partitioning

Typically, the 32-bit Linux kernel address space is divided into 0~3g for user space and 3~4g for kernel space. Note that this is a 32-bit kernel address space partition, and 64-bit kernel address space partitioning is different.


1, x86 Physical address space layout:



The space below the top of the physical address space is occupied by the I/O memory mappings of the PCI devices, and their size and layout are determined by the PCI specification. 640k~1m This address space is occupied by the BIOS and VGA adapters.

When initialized, the Linux system creates a Page object for each physical page based on the actual physical memory size, and all the Page objects form a mem_map array.

Further, for different purposes, the Linux kernel divides all the physical pages into the 3-class memory admin zone, as shown in Zone_dma,zone_normal,zone_highmem, respectively.

The scope of the ZONE_DMA is 0~16m, and the physical pages of the zone are specifically intended for DMA use by the I/O device. The reason why you need to manage DMA physical pages separately is because DMA uses physical addresses to access memory, does not go through MMU, and requires contiguous buffers, so in order to be able to provide a physically contiguous buffer, a section of the physical address space must be specifically partitioned for DMA.

The scope of the Zone_normal is 16m~896m, and the physical pages of the area are directly available to the kernel.

The range of Zone_highmem is the end of 896m~, which is the high-end memory and the kernel cannot be used directly.


2, Linux virtual address kernel space distribution

Under kernel image, 16M of kernel space is used for DMA operations. The high-end 128M address in kernel space consists of 3 parts, namely Vmalloc area, persistent kernel mapping zone, temporary kernel mapping area.

Because of the direct mapping relationship between zone_normal and kernel linear space, the kernel places frequently used data such as kernel code, GDT, IDT, PGD, mem_map array, etc. in Zone_normal. And the User Data, page table (PT) and other infrequently used data in Zone_ Highmem, only to access this data to establish a mapping relationship (Kmap ()). For example, when the kernel wants to access the I/O device storage space, it uses ioremap () to map the Mmio zone memory located at the high end of the physical address to the Vmalloc area of the kernel space, and then disconnect the mapping relationship when it is finished.


3, Linux virtual address user space distribution

The code area of a user process typically starts with the 0x08048000 of the virtual address space, which is to facilitate checking of null pointers. Above the code area is the data area, uninitialized data area, heap area, stack area, and parameters, Global environment variables.

4, the relationship between Linux virtual address and Physical address mapping


Linux divides the 4G linear address space into 2 parts, 0~3g for user space,3g~4g for kernel spaces.

Because the paging mechanism is turned on, the kernel must first establish a mapping relationship and then access it through a virtual address, if it wants to access the physical address space. In order to be able to access all the physical address space, it is clearly impossible to map all the physical address space into the 1G kernel linear space. As a result, the kernel maps the 0~896m physical address space to its own linear address space, so that it can access the physical pages in ZONE_DMA and zone_normal at any time, and the kernel's remaining 128M linear address space is insufficient to fully map all Zone_ Highmem,linux takes a dynamic mapping approach, which maps the physical pages in Zone_highmem to the last 128M linear address space of kernel spaces on demand, releasing the mapping relationship after use for other physical page mappings. Although this is a problem of efficiency, the kernel can normally access all the physical address space.


5, the Buddyinfo understanding

The Cat/proc/buddyinfo display is as follows:

Node 0, Zone DMA 0 4 5 4 4 3 ...

Node 0, Zone Normal 1 0 0 1 101 8 ...

Node 0, Zone highmem 2 0 0 1 1 0 ...

Where node represents the number of nodes in a NUMA environment, where only one node 0;zone represents the area under each node, typically DMA, normal, and hignmem three regions; The following column represents the free page block for each order in the partner system. For example, for the second column of Zone DMA (starting at 0), the number of free pages is 5*2^4 and the available memory is 5*2^4*page_size.


The calculation method is:

The number of *2^ columns in the column *page_size the number of columns is calculated from 0, that is, the first column is the number of the current column *2^0*page_size



Problems:

1, User space (process) is the concept of high-end memory.

The user process does not have a high-end memory concept. High-end memory exists only in kernel space. User processes can access up to 3G of physical memory at most, while kernel processes have access to all physical memory.


2, the 64-bit kernel has high-end memory.

In reality, the 64-bit Linux kernel does not have high-end memory because the 64-bit kernel can support more than 512GB of memory. If the machine is installed with more physical memory than the kernel address space, there is a high level of memory.


3. How much physical memory the user process can access. How much physical memory the kernel code can access.

The 32-bit system user process has a maximum access to 3GB, and kernel code can access all physical memory.

The 64-bit system user process has the largest access to more than 512GB, and kernel code can access all physical memory.


4, high-end memory and physical address, logical address, linear address relationship.

High-end memory is only related to the logical address, and the logical address, physical address is not directly related.


5, why not all the address space allocated to the kernel.

If all the address space is given to memory, then how does the user process use memory? How to ensure that the kernel uses memory and user processes do not conflict.

Related Article

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.