Linux Memory Management-linux kernel high-end memory

Source: Internet
Author: User
The Linux kernel address ING model x86CPU uses a segment-page address ING model. The address in the process code is a logical address. After segment-page address ING

Linux kernel address ING model
X86 CPU
The segment-page address ING model is used.Process CodeThe address in is a logical address. After segment-page address ING, the physical memory is accessed.

For example.

Linux kernel address space Division

Usually32-bit Linux kernelAddress Space Division 0 ~ 3G is the user space, 3 ~ 4G is the kernel space. Note that the 32-bit kernel address space is divided, and the 64-bit kernel address space is divided differently.

Origin of high-end Linux kernel memory

When the kernel module code or thread accesses the memory, the memory address in the code is the logical address, corresponding to the real physical memory address, the address is requiredOne-to-oneFor example, the physical address of the logical address 0xc0000003 is 0 × 3, and the physical address of 0xc0000004 is 0 × 4 ,... ..., The relationship between the logical address and the physical address is

Physical Address = logical address? 0xC0000000

Logical address Physical memory address
0xc0000000 0 × 0
0xc0000001 0 × 1
0xc0000002 0 × 2
0xc0000003 0 × 3
... ...
0xe0000000 0 × 20000000
... ...
0 xffffffff 0 × 40000000 ??

Assume that the logic address space of the kernel is accessed from 0xc0000000 ~ 0 xffffffff, the corresponding physical memory range is 0 × 0 ~ 0 × 40000000, that is, only 1 GB physical memory can be accessed. If 8 GB physical memory is installed on the machine, the kernel can only access the first 1 GB physical memory, and the last 7 GB physical memory will not be accessible, because the kernel address space has all been mapped to the physical memory address range 0 × 0 ~ 0 × 40000000. Even if 8 GB physical memory is installed, the physical address is 0 × 40000001. how can I access the kernel? The code must have a logical memory address, 0xc0000000 ~ The address space of 0xffffffff has been used up, so you cannot access the memory after the physical address 0 × 40000000.

Obviously, the kernel address space cannot be 0xc0000000 ~ 0xfffffff is all used for simple address ING. Therefore, the x86 architecture divides the kernel address space into three parts: ZONE_DMA, ZONE_NORMAL, and ZONE_HIGHMEM. ZONE_HIGHMEM is high-end memory, which is the concept of high-end memory.


In the x86 structure, the three types of regions are as follows:

ZONE_DMA16 MB of memory

ZONE_NORMAL16 MB ~ 896 MB

ZONE_HIGHMEM896 MB ~ End

Linux kernel high-end memory

We have explained the origin of high-end memory. Linux divides the kernel address space into three parts: ZONE_DMA, ZONE_NORMAL, and ZONE_HIGHMEM. the high-end memory HIGH_MEM address space ranges from 0xF8000000 ~ 0 xFFFFFFFF (896MB ~ 1024 MB ). If the kernel isHow to use MB high-end memory address space to access all physical memory?

When the kernel wants to access the memory above MB physical address, it ranges from 0xF8000000 ~ In the range of 0xFFFFFFFF address space, find a logical address space of the corresponding size and borrow it for a while. Use this logical address space to create a physical memory mapped to the one you want to access (that is, to fill the kernel PTE page table ),Temporarily used for a while and returned after use. In this way, others can also use this address space to access other physical memory, so that they can use a limited address space to access all physical memory. For example.

For example, the kernel wants to access a 1 MB physical memory segment starting with 2 GB, that is, the physical address range is 0 × 80000000 ~ 0x800FFFFF. A 1 MB free address space is found before access. assume that the free address space is 0xF8700000 ~ 0xF87FFFFF: Use this 1 MB logical address space to map to physical address space 0 × 80000000 ~ 0x800FFFFF memory. The ing relationship is as follows:

Logical address Physical memory address
0xF8700000 0 × 80000000
0xF8700001 0 × 80000001
0xF8700002 0 × 80000002
... ...
0xF87FFFFF 0x800FFFFF

When the kernel has accessed 0x80000000 ~ After 0x800FFFFF physical memory, replace 0xF8700000 ~ 0xF87FFFFF kernel linear space released. In this way, other processes or codes can also use 0xF8700000 ~ 0xF87FFFFF this address accesses other physical memory.

As described above, we can knowBasic idea of high-end memory: Create a temporary address ING by using an address space and release it after use. this address space can be used cyclically to access all the physical memory.

If a kernel process or module keeps occupying a certain logical address space, what should I do? If this is the case, the high-end memory address space of the kernel becomes increasingly tight. If all the memory is occupied and not released, no physical memory ING is established and cannot be accessed.

There are some office buildings in Tsim Sha Tsui, Hong Kong with few restrooms and locks. If the customer wants to go to the bathroom, he can take the key to the front desk and return the key to the front desk. In this way, although there is only one restroom, it can meet the needs of all customers to go to the restroom. If a customer keeps occupying the restroom and the key is not returned, other customers will not be able to access the restroom. The idea of Linux kernel high-end memory management is similar.


Division of Linux kernel high-end memory
The kernel divides the high-end memory into three parts: VMALLOC_START ~ VMALLOC_END, KMAP_BASE ~ FIXADDR_START and FIXADDR_START ~ 4G.


For high-end memory, you can obtain the corresponding page through alloc_page () or other functions. but to access the actual physical memory, you have to convert the page into a linear address (why? Think about how MMU accesses the physical memory). That is to say, we need to find a linear space for the pages corresponding to the high-end memory. this process is called high-end memory ING.

There are three methods for high-end memory ING:
ING to "kernel dynamic ing space" (noncontiguous memory allocation)
This method is very simple, because when we use vmalloc () to apply for memory in the "kernel dynamic ing space", we may obtain the page from the high-end memory (see the implementation of vmalloc ), therefore, high-end memory may be mapped to "kernel dynamic ing space.

Permanent kernel mapping)
If alloc_page () is used to obtain the page corresponding to the high-end memory, how can we find a linear space for it?
The kernel sets aside a linear space from PKMAP_BASE to FIXADDR_START, which is used to map high-end memory. On the 2.6 kernel, the address range is 4G-8 M to 4G-4 M. This space is called "kernel permanent ing space" or "permanent kernel ing space ". This space uses the same page directory table as other spaces. for the kernel, it is swapper_pg_dir. for common processes, it points to the table through the 33rd register. Generally, the space is 4 MB, so only one page table is required. the kernel searches for this page table through pkmap_page_table. You can map a page to this space through kmap. Because the space is 4 MB, up to 1024 pages can be mapped at the same time. Therefore, for unused pages, and should be released from this space (that is, the ing relationship is removed), through kunmap (), you can release the linear address of a page from this space.

Temporary Kering (temporary kernel mapping)
The kernel reserves some linear space between FIXADDR_START and FIXADDR_TOP for special requirements. This space is called a "fixed ing space". some of this space is used for temporary ing of high-end memory.

This space has the following features:
(1) each CPU occupies one space
(2) the space occupied by each CPU is divided into multiple small spaces. each small space is one page, and each small space is used for one purpose, these goals are defined in km_type in kmap_types.h.

To perform a temporary ing, you need to specify the ing purpose. based on the ing purpose, you can find the corresponding small space and use the address of the space as the ing address. This means that a temporary ING will overwrite the previous ING. You can use kmap_atomic () to implement temporary ING.

The high-end memory ING can be shown in the following figure:

The system user process can access a maximum of 3 GB, and the kernel code can access all the physical memory.

64-bit system user processes can access a maximum of 512 GB, and kernel code can access all physical memory.

4. what is the relationship between high-end memory and physical addresses, logical addresses, and linear addresses?

High-end memory only andPhysical AddressIt has a relationship with linear and logical addresses.

5. Why not allocate all the address spaces to the kernel?

If all the address spaces are allocated to the memory, how does the user process use the memory? How can we ensure that the kernel memory usage does not conflict with the user process?

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.