Linux memory management series 4: linux memory management series

Source: Internet
Author: User

Linux memory management series 4: linux memory management series

The previous three sections focus on how to allocate kernel space memory in the kernel. The memory of the user space of the process is also controlled by the kernel. The address memory of the user space is called the process address space. Linux uses the virtual memory technology to allow the system to run multiple memories simultaneously, and the address space of each process is the size of the entire physical memory. This section focuses on how the kernel manages the process address space.

1. Address Space

The process address space is composed of the virtual memory that the process can address. The kernel allows the process to use this virtual memory address. Generally, the memory addresses of each process are independent of each other, when the memory between processes can access each other, this process is also called a thread. Although the virtual address addressing area of a process can reach 4 GB, it does not mean that the process can have access to 4 GB. We usually call the address space that the process has the permission to access as the memory area. Through the Kernel Operating Process, we can increase or decrease the memory area.

Generally, when a process accesses an Invalid Address, it returns a "segment error" and the kernel terminates the process. The memory area can contain, code segment, data segment, bss segment, process user space stack, any memory ing file, any shared memory segment, and any anonymous memory segment.

Binary memory Descriptor

The kernel defines a memory descriptor of mm_struct, which contains all information related to the process address.

Mm_users indicates the number of threads in the process, and mm_count indicates the number of processes. Only when mm_count is 0 indicates that no process uses this memory descriptor. In this case, the kernel can undo this struct. Both the Mmap and mm_rb data structures represent virtual memory area objects, but mmap stores them in the form of a linked list for element traversal, and mm_rb stores them in the form of a red/black tree for element search.

1. allocate memory Descriptor

In the process descriptor task_struct struct, the mm domain stores the memory descriptor mm_struct of the process. The fork () function uses the copy_mm () function to copy the mm_struct of the parent process, the mm_struct of the sub-process is allocated through the mm_cachu slab cache. Generally, each process has a unique mm_struct. To create a thread, set CLONE_VM Peugeot when calling clone. Therefore, there is no distinction between processes and threads from the kernel perspective. The only difference is whether the address space can be shared.

2. Cancel the memory descriptor.

When the thread exits, the kernel reduces the user count of mm_users in mm_struct. When the process exits, mm_count is 0, and mm_struct is recycled, kmem_cache_free () is called () the function recycles mm_struct to the cache of mm_cachu slab.

3. mm_struct of the kernel thread

A kernel thread is also a unique process because it is always executed in the kernel space and has no process address space. At the same time, there is no user context. Its mm domain is empty. When the kernel thread accesses the kernel space, it also needs to convert the page table into a virtual address. In this case, the kernel thread directly uses the memory descriptor of the previous CPU execution process.

Three virtual memory areas

The virtual memory area is described by the vm_area_struct struct. vm_area_struct describes an independent memory range in the memory continuous interval of the specified address space. The kernel manages each memory area as an object.

The vm_area_struct struct is as follows:

 

 

Each memory descriptor represents a certain interval of the process address space. vm_start points to the first address of the interval, and vm_end points to the next address of the end address of the interval. Each mm_struct corresponds to a unique vm_area_struct. The two processes have their own vm_area_struct when the shared memory is mapped to a file. The two threads can share a vm_area_struct, because they share an mm_struct.

The vm_flags field indicates the permissions in the current memory area. Common flags include VM_READ, VM_WRITE, and VM_EXEC, which indicate the read, write, and execute permissions on pages in the memory area. VM_SHARD indicates whether the ing in the memory area can be shared among multiple processes.

The vm_ops field represents the related operation function tables in the memory area. General operations include adding the process memory area, deleting the process memory area, and page troubleshooting.

4. Actual process address space

First run a daemon sleep 3000 &. View/proc/[pid]/maps. The result is as follows:

 

 

Then run pmap [pid] to view the following content:

 

 

This mainly includes the code segment, Data Segment and bss segment in the C library, the code segment of the Dynamic Link program, the data segment and bss segment. The code segment and Data Segment of the stack and executable objects of the process.

5. Memory region operations

The kernel provides the find_vmal () function to locate the memory region of the given memory address.

Struct vm_area_struct * find_vmal (struct mm_struct * mm, unsigned long addr)

This function is based on the given memory Descriptor and virtual address parameters. Search the specified address space for the first memory region where vm_end is greater than addr. If this region does not exist, null is returned. Otherwise, the returned vm_area_struct pointer is cached in the mmap_cache domain in the recently used memory region. Similarly, the kernel function find_vma_prev () is used to return the previous memory area of the given address.

The do_mmap () function can add a linear address space to the address space of a specified process. Its prototype is as follows: do_mmap (struct file * file, unsigned long addr, unsigned long len, unsigned long prot, unsigned long flag, unsigned long offset), file specifies the file pointer, addr indicates the start address of the idle interval, len indicates the file Memory interval ing length, and prot indicates the page access permission, flag refers to other flags in the linear area, and offset refers to the file offset length. The corresponding undo address space ing function do_unmmap ().

Six-page table

A process cannot operate the physical address of the memory, whether it is a kernel thread or a user process. Processes can only operate on linear addresses. Therefore, the kernel needs to maintain a page table for each process for address ing. In Linux, a three-level page table is generally used. The top-level page table is a global page Directory (PGD) with 10 characters in length, and the second-level page table has 10 characters in length, the last level of page table is also called 12 characters in the page offset length. The page table items are the same as the number of page tables. The page table items can be fixed to a certain page, and the specific linear address can be fixed through intra-page offset.

In the process of execution, since each memory access will access the page table, in order to improve the linear address resolution efficiency, most architectures implement the block table TLB cache, retained the ing between process linear addresses and physical addresses. In general, to resolve a linear address, you can access TLB first, and then access the process page table after adding TLB to make it invalid.

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.