Linux high-end memory

Source: Internet
Author: User
Article Title: Linux high-end memory. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

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.

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.