Linux memory management-high-end memory ing

Source: Internet
Author: User
Tags blank page

 

High-end memory refers to the memory with a physical address greater than 896 MB.

For such memory, the ing cannot be performed in the "kernel direct ing space.

Why?
Because the "kernel direct ing space" can only be from 3G to 4g, and can only be mapped to 1g physical memory directly, there is nothing to do with physical memory larger than 1g.

In fact, the "kernel direct ing space" cannot reach 1 GB, and we have to leave some linear space for "kernel dynamic ing space.
Therefore, Linux requires that the "direct kernel ing space" can map up to 896 MB of physical memory.

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 high-end memory ing methods:

1. ing to "kernel dynamic ing space"
This method is very simple, because when applying for memory through vmalloc (), the "kernel dynamic ing space" may retrieve pages from the high-end memory (see the implementation of vmalloc ), therefore, high-end memory may be mapped to the "kernel dynamic ing space.

2. Permanent kernel ing
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.4 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.

With kmap (), you can map a page to this space.

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.

3. Temporary ing

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.

 

Simple and simple expression of how to map high-end memory

 

-------------------------

High-end memory: The physical page of the last 128m linear address of the linear address space page_offset + 896 m to 4G <= ing => 896m or above, not directly mapped. There are three methods: non-continuous memory zone ing, permanent kernel ing, and temporary kernel ing (fixed ing)
The linear address area starting with page_offset is:
Page_offset (3G) | physical memory ing -- 8 m -- vmalloc zone -- 4 k -- vmalloc zone -- 8 K -- Permanent kernel ing -- temporary kernel ing (fixed ing) | 4G

1. Discontinuous zone ing

1.1 Each discontinuous memory zone corresponds to a descriptor of the vm_struct type. Through the next field, These descriptors are inserted into a vmlist linked list.

1.2 Types of three discontinuous zones:
Vm_alloc -- physical memory (calling alloc_page) and linear address are applied at the same time. Physical memory is of the _ gfp_highmem type (allocation order is high, normal, DMA) (it can be seen that vmalloc can map not only the _ gfp_highmem page box, but also aims to splice scattered and discontinuous page boxes into a continuous kernel logical address space ...)
Vm_map -- apply for linear zone only, and apply for physical memory separately. It is a simplified version of vm_alloc.
Vm_ioremap -- only apply for linear zone and physical memory (the physical memory here is generally high-end memory, larger than MB)

2. Permanent kernel ing

2.1 permanent memory ing allows long-term ing. Use a specialized page table of swapper_pg_dir in the main kernel page table.
Pkmap_page_table: a special page table. The number of items in the table on the page is generated by last_pkmap (512 or 1024.
Page_address_htable: Address
Pkmap_count: array containing last_pkmap counters.
Pkmap_base: The linear address of the page table starts from pkmap_base.

2.2 If all last_pkmap items are used up, set the current process to task_uninterruptible and call schedule ()

3. Temporary memory ing

3.1 can be used inside the interrupt handler function and the deletable function without blocking. Because the temporary memory ing is part of the fixed memory ing, an address is fixed for a kernel component.

3.2 each CPU has its own set of 13 windows (a linear address and page table items.
Enum km_type {
Km_bounce_read,
Km_skb_sunrpc_data,
Km_skb_data_softirq,
Km_user0,
Km_user1,
Km_bio_src_irq,
Km_bio_dst_irq,
Km_pte0,
Km_pte1,
Km_irq0,
Km_irq1,
Km_softirq0,
Km_softirq1,
Km_type_nr
};

Fixed linear addresses of all fixed Mappings
Enum fixed_addresses {
Fix_hole,
Fix_vsyscall,
....
# Ifdef config_highmem
Fix_kmap_begin,/* Reserved PTE's for temporary kernel mappings */
Fix_kmap_end = fix_kmap_begin + (km_type_nr * nr_cpus)-1,
# Endif
.......
_ End_of_permanent_fixed_addresses,
/* Temporary boot-time mappings, used before ioremap () is functional */
# Define nr_fix_btmaps 16
Fix_btmap_end = _ end_of_permanent_fixed_addresses,
Fix_btmap_begin = fix_btmap_end + nr_fix_btmaps-1,
Fix_wp_test,
_ End_of_fixed_addresses
};

3.3 note that the fixed_addresses address is inverted from top to bottom, and the fix_hole address is equal to 0xfffff000, which is a hole
# DEFINE _ fix_to_virt (x) (fixaddr_top-(x) <page_shift ))
# DEFINE _ fixaddr_top 0xfffff000

-------------------------

Vmalloc_reserve and 896 m

In Linux, the virtual address space to the physical address space is usually fixed and consecutive.

Assume that the machine memory is 512 MB,
From 3G to 3G + 512 M is a continuous fixed shooting area. Zone_dma, zone_normal is in this region. Fixed-shot vaddr can be directly used (get a free page, then use pfn_to_virt () and other macro definitions to convert to vaddr) or allocated using kmalloc. Such vaddr physical pages are continuous. The obtained address must also be in the fixed shooting area.

If the memory is insufficient and the continuous areas cannot be met, it is necessary to call vmalloc for allocation, because it can combine physical discontinuous space for allocation, so it can better meet the allocation requirements. Vmalloc can be mapped to a high-end page or a bottom page. Vmalloc is only used to provide logical continuous addresses...

However, the vaddr allocated by vmalloc must not overlap with the vaddr in the fixed shooting area. Because the shadow from vaddr to the physical page can only be unique at the same time. Therefore, the vaddr obtained by vmalloc must be 3G + 512 m or above. That is, from vmalloc_start. Vmalloc_start is 8-16 m (2 * vmalloc_offset) greater than the maximum vaddr address in the continuous fixed shooting area. There is a ghost formula in

# Define vmalloc_offset 8*1024
# Define vmalloc_start (high_memory-2 * vmalloc_offset )&~ VMALLOC_OFFSET-1)

High_memory is the highest position in the fixed shooting area.

What do I do when I open 8-16 m? To capture out-of-bounds mm_fault.
Similarly, a blank page (empty) is left in the vaddr space obtained by vmalloc for the same purpose as the empty open space above. Your vmalloc (100) is twice, and the two addresses are 8 k Apart.
If continuous allocation has no holes, for example
P1 = vmalloc (4096 );
P2 = vmalloc (4096 );
If P1 is out of range to P2, it will not be mm_falut. It is not easy to debug.

The following describes vmalloc_reserve and 896m problems.

The above assumes that the machine physical M case. If the machine has 1 GB physical memory, how is it good? Does vmalloc () vaddr need to be allocated above 3G + 1G + 8 m holes? Exceeds the addressing space.
At this time, the vmalloc_reserve 128 M retained under 4 GB will come in handy.
In other words, if the physical memory exceeds 896 m, high_memory can only be 3G + 896. Vmalloc_resreve m should be reserved for vmalloc at the maximum of the addressable space.

Therefore, this m vaddr space is used for vmalloc exceeding M physically. If the physical connection is only 512 m, it is generally unavailable. Because vmalloc_start is very low. It will be used if there are too many vmalloc instances.

High_memory is set in arch/i386/kernel and mm initialization. The value is calculated based on the physical memory size and vmalloc_reserve.

Therefore, the linear address of the M kernel is only used to reflect the incorrect physical memory of more than 1 GB. If the physical memory is 2 GB, vmalloc smaller than 1 GB will also be shot in that space. In short, the kernel's high-end linear address is used to access memory resources other than the kernel's fixed ing.

It can be seen that vmalloc can be used

Show_vmalloc ()
{
Struct vm_struct ** P, * TMP;

For (P = & vmlist; (TMP = * P); P = & TMP-> next ){
Printk ("% P % d/N", TMP, TMP-> ADDR, TMP-> size

}
}

Of course, the user space can use high-end memory, which is normal. The kernel uses high-end memory when allocating memory that is not frequently used (if any ), for example, some data structures in the kernel are frequently used, and some user data is not frequently used.

When you start an application, you need memory, and each application has a 3G linear address. When you map these addresses to a page table, you can directly use the high-end memory.

It also needs to be corrected that the M linear address is not only used in these places. If you want to load a device, and the device needs to map its memory to the kernel, it also needs to use this linear address space, otherwise the kernel will not be able to access the memory space on the device.

In short, the kernel's high-end linear address is used to access memory resources other than the kernel's fixed ing.

In fact, high-end memory is a concept proposed for a special linear space in the kernel, which is different from the actual physical memory. When a process uses the memory, a page missing exception is triggered. It is important to map the physical pages to the user process. There is no high-end Memory concept in user space.

 

 

-----------------------------------------------------------------

The following discussion is only available on the i386 platform. Generally, typical scenarios are considered.

  1. The Linux kernel manages the physical memory of the entire system through the mem_map array of the struct page type. In the system, the partner system allocation algorithm records physical memory allocation and recovery operations by operating on this array. Do not confuse the system's high-end memory, low-end memory, and other concepts here. The classification of high-end and low-end memory mainly lies in distinguishing whether physical memory addresses can be directly mapped to Kernel linear address spaces.

We know that the Linux kernel address space is 1 GB (the user space is 0 ~ 3 GB, kernel space 3 GB ~ 4g, which is the most common method). Therefore, if we use all the 1g linear address space to directly map physical memory one by one, all processes (threads) in the kernel state) the total physical memory that can be used is only 1 GB. To enable all processes in the kernel state to use more physical memory, linux adopts a flexible form: it divides 1g Kernel linear address space into several parts. The first part is the first 896 m of 1g. This part is the kernel linear space and the physical memory 0 ~ 896m one-to-one ing (the difference is a constant of 0xc0000000). The linear space of the last M is used to dynamically map all the remaining physical memory. Because the dynamic ing method is different, the last M is divided into several parts. If you are interested, you can view the relevant information. Here, the physical memory corresponding to the above M linear space is the so-called low-end physical memory, and the remaining physical memory is the high-end physical memory.

We can see from the above high-end and low-end physical memory names that the high-end and low-end physical memory have nothing to do with the specific memory allocation algorithm. They are all controlled by the mem_map array, then, the partner distribution system manages the application.

 

  1. Process and Memory Allocation

First, you must understand the concept that all the addresses used in the process are virtual addresses. in Linux, this virtual address is a so-called linear address. In Linux, a process can run in the user and kernel States. (In typical configuration cases) when a process runs in the user State, it uses a linear address that can only be between 0 and ~ Within 3G, when a process runs in the kernel state, it uses a linear address range of 3G ~ 4G.

To convert a linear address to a physical address, each process has its own private page Directory and page table. When creating a process page Directory in Linux ~ 767) Clear and copy the 768th to 1023 items of the kernel page Directory table (swapper_pg_dir) to the 768th to 1023 items of the Process page Directory table. Since the kernel only maps the first 896 MB of physical memory during initialization, we can know that the kernel and directory table can only ensure effective ing among the first 768th items at the beginning of the first 224 items. From this we can know that all processes share their Kernel linear address space.

When a process encounters a page failure in the kernel space, this occurs mainly when a linear address is used to access the dynamic ing area of the kernel space. In its processing program, we need to use the page Directory (swapper_pg_dir) of process 0 to synchronize the kernel page Directory of the process, in fact, it is to copy the kernel page Directory of process 0 to the current process (the kernel page table is shared with process 0, so there is no need to copy it ). If the kernel page directory at the address of process 0 does not exist, an error occurs. For more information about the code, see the source code of vmalloc.

When a process runs in the user State and needs to request memory space, the kernel first allocates the needed linear address space in its user linear space, then, the partner allocates the system physical memory, maps the allocated physical memory to the user space linear address, and modifies the page Directory items and page table items of the process to write these mappings.

 

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.